unpdf 0.9.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -87,10 +87,11 @@ for (let i = 1; i <= doc.numPages; i++) {
87
87
  Generally speaking, you don't need to worry about the PDF.js build. `unpdf` ships with a serverless build of the latest PDF.js version. However, if you want to use the official PDF.js version or the legacy build, you can define a custom PDF.js module.
88
88
 
89
89
  ```ts
90
- // Before using any other methods, define the PDF.js module
91
- import { defineUnPDFConfig } from "unpdf";
90
+ // Before using any other method, define the PDF.js module
91
+ // if you need another PDF.js build
92
+ import { configureUnPDF } from "unpdf";
92
93
 
93
- defineUnPDFConfig({
94
+ configureUnPDF({
94
95
  // Use the official PDF.js build (make sure to install it first)
95
96
  pdfjs: () => import("pdfjs-dist"),
96
97
  });
@@ -118,12 +119,12 @@ interface UnPDFConfiguration {
118
119
 
119
120
  ## Methods
120
121
 
121
- ### `defineUnPDFConfig`
122
+ ### `configureUnPDF`
122
123
 
123
124
  Define a custom PDF.js module, like the legacy build. Make sure to call this method before using any other methods.
124
125
 
125
126
  ```ts
126
- function defineUnPDFConfig(config: UnPDFConfiguration): Promise<void>;
127
+ function configureUnPDF(config: UnPDFConfiguration): Promise<void>;
127
128
  ```
128
129
 
129
130
  ### `getResolvedPDFJS`
@@ -172,9 +173,9 @@ In order to use this method, you have to meet the following requirements:
172
173
  **Example**
173
174
 
174
175
  ```ts
175
- import { defineUnPDFConfig, renderPageAsImage } from "unpdf";
176
+ import { configureUnPDF, renderPageAsImage } from "unpdf";
176
177
 
177
- defineUnPDFConfig({
178
+ configureUnPDF({
178
179
  // Use the official PDF.js build
179
180
  pdfjs: () => import("pdfjs-dist"),
180
181
  });
@@ -193,7 +194,7 @@ await writeFile("dummy-page-1.png", Buffer.from(result));
193
194
 
194
195
  ```ts
195
196
  declare function renderPageAsImage(
196
- data: BinaryData | PDFDocumentProxy,
197
+ data: BinaryData,
197
198
  pageNumber: number,
198
199
  options?: {
199
200
  canvas?: () => Promise<typeof import("canvas")>;
package/dist/index.cjs CHANGED
@@ -8,6 +8,8 @@ async function getDocumentProxy(data, options = {}) {
8
8
  const pdf = await getDocument({
9
9
  data,
10
10
  isEvalSupported: false,
11
+ // See: https://github.com/mozilla/pdf.js/issues/4244#issuecomment-1479534301
12
+ useSystemFonts: true,
11
13
  ...options
12
14
  }).promise;
13
15
  return pdf;
@@ -24,12 +26,10 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
24
26
  }
25
27
  if (pdfjsResolver) {
26
28
  try {
27
- const _import = await pdfjsResolver();
28
- if ("resolvePDFJS" in _import) {
29
- resolvedModule = await _import.resolvePDFJS();
30
- return;
29
+ resolvedModule = await interopDefault(pdfjsResolver());
30
+ if (resolvedModule && "resolvePDFJS" in resolvedModule) {
31
+ resolvedModule = await resolvedModule.resolvePDFJS();
31
32
  }
32
- resolvedModule = _import.default || _import;
33
33
  return;
34
34
  } catch {
35
35
  throw new Error(
@@ -49,6 +49,10 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
49
49
  function isPDFDocumentProxy(data) {
50
50
  return typeof data === "object" && data !== null && "_pdfInfo" in data;
51
51
  }
52
+ async function interopDefault(m) {
53
+ const resolved = await m;
54
+ return resolved.default || resolved;
55
+ }
52
56
 
53
57
  async function getMeta$1(data) {
54
58
  const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
@@ -104,7 +108,8 @@ async function extractImages$1(data, pageNumber) {
104
108
  return images;
105
109
  }
106
110
  async function renderPageAsImage$1(data, pageNumber, options = {}) {
107
- const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
111
+ const canvasFactory = await createIsomorphicCanvasFactory(options.canvas);
112
+ const pdf = await getDocumentProxy(data, { canvasFactory });
108
113
  const page = await pdf.getPage(pageNumber);
109
114
  if (pageNumber < 1 || pageNumber > pdf.numPages) {
110
115
  throw new Error(
@@ -121,7 +126,6 @@ async function renderPageAsImage$1(data, pageNumber, options = {}) {
121
126
  if (outputScale !== 1 && outputScale > 0) {
122
127
  viewport = page.getViewport({ scale: outputScale });
123
128
  }
124
- const canvasFactory = await createIsomorphicCanvasFactory(options.canvas);
125
129
  const ctx = canvasFactory.create(viewport.width, viewport.height);
126
130
  await page.render({
127
131
  canvasContext: ctx.context,
@@ -132,7 +136,7 @@ async function renderPageAsImage$1(data, pageNumber, options = {}) {
132
136
  return await response.arrayBuffer();
133
137
  }
134
138
  async function createIsomorphicCanvasFactory(canvas) {
135
- const _canvas = await canvas?.();
139
+ const _canvas = canvas ? await interopDefault(canvas()) : void 0;
136
140
  return {
137
141
  _createCanvas(width, height) {
138
142
  if (isBrowser) {
@@ -140,7 +144,8 @@ async function createIsomorphicCanvasFactory(canvas) {
140
144
  canvas2.width = width;
141
145
  canvas2.height = height;
142
146
  return canvas2;
143
- } else if (isNode) {
147
+ }
148
+ if (isNode) {
144
149
  if (!_canvas) {
145
150
  throw new Error('Failed to resolve "canvas" package.');
146
151
  }
@@ -175,13 +180,14 @@ async function createIsomorphicCanvasFactory(canvas) {
175
180
  };
176
181
  }
177
182
 
178
- async function defineUnPDFConfig(options) {
183
+ async function configureUnPDF(options) {
179
184
  const { pdfjs } = { ...options };
180
185
  if (pdfjs) {
181
186
  await resolvePDFJSImports(pdfjs, { force: true });
182
187
  }
183
188
  }
184
189
 
190
+ const defineUnPDFConfig = configureUnPDF;
185
191
  const getMeta = async (...args) => {
186
192
  await resolvePDFJSImports();
187
193
  return await getMeta$1(...args);
@@ -190,7 +196,6 @@ const extractText = async (...args) => {
190
196
  await resolvePDFJSImports();
191
197
  return await extractText$1(...args);
192
198
  };
193
- const extractPDFText = extractText;
194
199
  const extractImages = async (...args) => {
195
200
  await resolvePDFJSImports();
196
201
  return await extractImages$1(...args);
@@ -200,9 +205,9 @@ const renderPageAsImage = async (...args) => {
200
205
  return await renderPageAsImage$1(...args);
201
206
  };
202
207
 
208
+ exports.configureUnPDF = configureUnPDF;
203
209
  exports.defineUnPDFConfig = defineUnPDFConfig;
204
210
  exports.extractImages = extractImages;
205
- exports.extractPDFText = extractPDFText;
206
211
  exports.extractText = extractText;
207
212
  exports.getDocumentProxy = getDocumentProxy;
208
213
  exports.getMeta = getMeta;
package/dist/index.d.cts CHANGED
@@ -15,7 +15,7 @@ declare function extractText$1(data: BinaryData | PDFDocumentProxy, options?: {
15
15
  }>;
16
16
 
17
17
  declare function extractImages$1(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
18
- declare function renderPageAsImage$1(data: BinaryData | PDFDocumentProxy, pageNumber: number, options?: {
18
+ declare function renderPageAsImage$1(data: BinaryData, pageNumber: number, options?: {
19
19
  canvas?: () => Promise<typeof canvas>;
20
20
  /** @default 1 */
21
21
  scale?: number;
@@ -37,13 +37,14 @@ interface UnPDFConfiguration {
37
37
  pdfjs?: () => Promise<any>;
38
38
  }
39
39
 
40
- declare function defineUnPDFConfig(options: UnPDFConfiguration): Promise<void>;
40
+ declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
41
41
 
42
42
  /**
43
43
  * Returns a PDFDocumentProxy instance from a given binary data.
44
44
  *
45
45
  * Applies the following defaults:
46
46
  * - `isEvalSupported: false`
47
+ * - `useSystemFonts: true`
47
48
  */
48
49
  declare function getDocumentProxy(data: BinaryData, options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
49
50
  declare function getResolvedPDFJS(): Promise<PDFJS>;
@@ -51,11 +52,12 @@ declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<any>, { force
51
52
  force?: boolean | undefined;
52
53
  }): Promise<void>;
53
54
 
55
+ /** @deprecated Use `configureUnPDF` instead */
56
+ declare const defineUnPDFConfig: typeof configureUnPDF;
57
+
54
58
  declare const getMeta: typeof getMeta$1;
55
59
  declare const extractText: typeof extractText$1;
56
- /** @deprecated Use `extractText` instead. */
57
- declare const extractPDFText: typeof extractText$1;
58
60
  declare const extractImages: typeof extractImages$1;
59
61
  declare const renderPageAsImage: typeof renderPageAsImage$1;
60
62
 
61
- export { defineUnPDFConfig, extractImages, extractPDFText, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
63
+ export { configureUnPDF, defineUnPDFConfig, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
package/dist/index.d.mts CHANGED
@@ -15,7 +15,7 @@ declare function extractText$1(data: BinaryData | PDFDocumentProxy, options?: {
15
15
  }>;
16
16
 
17
17
  declare function extractImages$1(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
18
- declare function renderPageAsImage$1(data: BinaryData | PDFDocumentProxy, pageNumber: number, options?: {
18
+ declare function renderPageAsImage$1(data: BinaryData, pageNumber: number, options?: {
19
19
  canvas?: () => Promise<typeof canvas>;
20
20
  /** @default 1 */
21
21
  scale?: number;
@@ -37,13 +37,14 @@ interface UnPDFConfiguration {
37
37
  pdfjs?: () => Promise<any>;
38
38
  }
39
39
 
40
- declare function defineUnPDFConfig(options: UnPDFConfiguration): Promise<void>;
40
+ declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
41
41
 
42
42
  /**
43
43
  * Returns a PDFDocumentProxy instance from a given binary data.
44
44
  *
45
45
  * Applies the following defaults:
46
46
  * - `isEvalSupported: false`
47
+ * - `useSystemFonts: true`
47
48
  */
48
49
  declare function getDocumentProxy(data: BinaryData, options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
49
50
  declare function getResolvedPDFJS(): Promise<PDFJS>;
@@ -51,11 +52,12 @@ declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<any>, { force
51
52
  force?: boolean | undefined;
52
53
  }): Promise<void>;
53
54
 
55
+ /** @deprecated Use `configureUnPDF` instead */
56
+ declare const defineUnPDFConfig: typeof configureUnPDF;
57
+
54
58
  declare const getMeta: typeof getMeta$1;
55
59
  declare const extractText: typeof extractText$1;
56
- /** @deprecated Use `extractText` instead. */
57
- declare const extractPDFText: typeof extractText$1;
58
60
  declare const extractImages: typeof extractImages$1;
59
61
  declare const renderPageAsImage: typeof renderPageAsImage$1;
60
62
 
61
- export { defineUnPDFConfig, extractImages, extractPDFText, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
63
+ export { configureUnPDF, defineUnPDFConfig, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
package/dist/index.d.ts CHANGED
@@ -15,7 +15,7 @@ declare function extractText$1(data: BinaryData | PDFDocumentProxy, options?: {
15
15
  }>;
16
16
 
17
17
  declare function extractImages$1(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
18
- declare function renderPageAsImage$1(data: BinaryData | PDFDocumentProxy, pageNumber: number, options?: {
18
+ declare function renderPageAsImage$1(data: BinaryData, pageNumber: number, options?: {
19
19
  canvas?: () => Promise<typeof canvas>;
20
20
  /** @default 1 */
21
21
  scale?: number;
@@ -37,13 +37,14 @@ interface UnPDFConfiguration {
37
37
  pdfjs?: () => Promise<any>;
38
38
  }
39
39
 
40
- declare function defineUnPDFConfig(options: UnPDFConfiguration): Promise<void>;
40
+ declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
41
41
 
42
42
  /**
43
43
  * Returns a PDFDocumentProxy instance from a given binary data.
44
44
  *
45
45
  * Applies the following defaults:
46
46
  * - `isEvalSupported: false`
47
+ * - `useSystemFonts: true`
47
48
  */
48
49
  declare function getDocumentProxy(data: BinaryData, options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
49
50
  declare function getResolvedPDFJS(): Promise<PDFJS>;
@@ -51,11 +52,12 @@ declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<any>, { force
51
52
  force?: boolean | undefined;
52
53
  }): Promise<void>;
53
54
 
55
+ /** @deprecated Use `configureUnPDF` instead */
56
+ declare const defineUnPDFConfig: typeof configureUnPDF;
57
+
54
58
  declare const getMeta: typeof getMeta$1;
55
59
  declare const extractText: typeof extractText$1;
56
- /** @deprecated Use `extractText` instead. */
57
- declare const extractPDFText: typeof extractText$1;
58
60
  declare const extractImages: typeof extractImages$1;
59
61
  declare const renderPageAsImage: typeof renderPageAsImage$1;
60
62
 
61
- export { defineUnPDFConfig, extractImages, extractPDFText, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
63
+ export { configureUnPDF, defineUnPDFConfig, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
package/dist/index.mjs CHANGED
@@ -6,6 +6,8 @@ async function getDocumentProxy(data, options = {}) {
6
6
  const pdf = await getDocument({
7
7
  data,
8
8
  isEvalSupported: false,
9
+ // See: https://github.com/mozilla/pdf.js/issues/4244#issuecomment-1479534301
10
+ useSystemFonts: true,
9
11
  ...options
10
12
  }).promise;
11
13
  return pdf;
@@ -22,12 +24,10 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
22
24
  }
23
25
  if (pdfjsResolver) {
24
26
  try {
25
- const _import = await pdfjsResolver();
26
- if ("resolvePDFJS" in _import) {
27
- resolvedModule = await _import.resolvePDFJS();
28
- return;
27
+ resolvedModule = await interopDefault(pdfjsResolver());
28
+ if (resolvedModule && "resolvePDFJS" in resolvedModule) {
29
+ resolvedModule = await resolvedModule.resolvePDFJS();
29
30
  }
30
- resolvedModule = _import.default || _import;
31
31
  return;
32
32
  } catch {
33
33
  throw new Error(
@@ -47,6 +47,10 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
47
47
  function isPDFDocumentProxy(data) {
48
48
  return typeof data === "object" && data !== null && "_pdfInfo" in data;
49
49
  }
50
+ async function interopDefault(m) {
51
+ const resolved = await m;
52
+ return resolved.default || resolved;
53
+ }
50
54
 
51
55
  async function getMeta$1(data) {
52
56
  const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
@@ -102,7 +106,8 @@ async function extractImages$1(data, pageNumber) {
102
106
  return images;
103
107
  }
104
108
  async function renderPageAsImage$1(data, pageNumber, options = {}) {
105
- const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
109
+ const canvasFactory = await createIsomorphicCanvasFactory(options.canvas);
110
+ const pdf = await getDocumentProxy(data, { canvasFactory });
106
111
  const page = await pdf.getPage(pageNumber);
107
112
  if (pageNumber < 1 || pageNumber > pdf.numPages) {
108
113
  throw new Error(
@@ -119,7 +124,6 @@ async function renderPageAsImage$1(data, pageNumber, options = {}) {
119
124
  if (outputScale !== 1 && outputScale > 0) {
120
125
  viewport = page.getViewport({ scale: outputScale });
121
126
  }
122
- const canvasFactory = await createIsomorphicCanvasFactory(options.canvas);
123
127
  const ctx = canvasFactory.create(viewport.width, viewport.height);
124
128
  await page.render({
125
129
  canvasContext: ctx.context,
@@ -130,7 +134,7 @@ async function renderPageAsImage$1(data, pageNumber, options = {}) {
130
134
  return await response.arrayBuffer();
131
135
  }
132
136
  async function createIsomorphicCanvasFactory(canvas) {
133
- const _canvas = await canvas?.();
137
+ const _canvas = canvas ? await interopDefault(canvas()) : void 0;
134
138
  return {
135
139
  _createCanvas(width, height) {
136
140
  if (isBrowser) {
@@ -138,7 +142,8 @@ async function createIsomorphicCanvasFactory(canvas) {
138
142
  canvas2.width = width;
139
143
  canvas2.height = height;
140
144
  return canvas2;
141
- } else if (isNode) {
145
+ }
146
+ if (isNode) {
142
147
  if (!_canvas) {
143
148
  throw new Error('Failed to resolve "canvas" package.');
144
149
  }
@@ -173,13 +178,14 @@ async function createIsomorphicCanvasFactory(canvas) {
173
178
  };
174
179
  }
175
180
 
176
- async function defineUnPDFConfig(options) {
181
+ async function configureUnPDF(options) {
177
182
  const { pdfjs } = { ...options };
178
183
  if (pdfjs) {
179
184
  await resolvePDFJSImports(pdfjs, { force: true });
180
185
  }
181
186
  }
182
187
 
188
+ const defineUnPDFConfig = configureUnPDF;
183
189
  const getMeta = async (...args) => {
184
190
  await resolvePDFJSImports();
185
191
  return await getMeta$1(...args);
@@ -188,7 +194,6 @@ const extractText = async (...args) => {
188
194
  await resolvePDFJSImports();
189
195
  return await extractText$1(...args);
190
196
  };
191
- const extractPDFText = extractText;
192
197
  const extractImages = async (...args) => {
193
198
  await resolvePDFJSImports();
194
199
  return await extractImages$1(...args);
@@ -198,4 +203,4 @@ const renderPageAsImage = async (...args) => {
198
203
  return await renderPageAsImage$1(...args);
199
204
  };
200
205
 
201
- export { defineUnPDFConfig, extractImages, extractPDFText, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
206
+ export { configureUnPDF, defineUnPDFConfig, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };