unpdf 1.5.0 → 1.5.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
@@ -272,6 +272,16 @@ In order to use this method, make sure to meet the following requirements:
272
272
  - Use the official PDF.js build (see [Official or Legacy PDF.js Build](#official-or-legacy-pdfjs-build)).
273
273
  - Install the [`@napi-rs/canvas`](https://github.com/Brooooooklyn/canvas) package if you are using Node.js. This package is required to render the PDF page as an image.
274
274
 
275
+ > [!TIP]
276
+ > In Node.js, `getDocumentProxy` automatically sets `disableFontFace: true` and resolves `standardFontDataUrl` from your local `pdfjs-dist` package for correct font rendering. To customize this behavior, pass your own options:
277
+ >
278
+ > ```ts
279
+ > const pdf = await getDocumentProxy(buffer, {
280
+ > disableFontFace: false,
281
+ > standardFontDataUrl: 'https://unpkg.com/pdfjs-dist@latest/standard_fonts/',
282
+ > })
283
+ > ```
284
+
275
285
  **Type Declaration**
276
286
 
277
287
  ```ts
package/dist/index.cjs CHANGED
@@ -1,15 +1,73 @@
1
1
  'use strict';
2
2
 
3
+ function polyfillDOMMatrix() {
4
+ if (typeof globalThis.DOMMatrix !== "undefined")
5
+ return;
6
+ globalThis.DOMMatrix = class DOMMatrix {
7
+ a;
8
+ b;
9
+ c;
10
+ d;
11
+ e;
12
+ f;
13
+ constructor(init) {
14
+ if (Array.isArray(init) && init.length === 6) {
15
+ this.a = init[0];
16
+ this.b = init[1];
17
+ this.c = init[2];
18
+ this.d = init[3];
19
+ this.e = init[4];
20
+ this.f = init[5];
21
+ } else {
22
+ this.a = 1;
23
+ this.b = 0;
24
+ this.c = 0;
25
+ this.d = 1;
26
+ this.e = 0;
27
+ this.f = 0;
28
+ }
29
+ }
30
+ translateSelf(tx, ty = 0) {
31
+ this.e = this.a * tx + this.c * ty + this.e;
32
+ this.f = this.b * tx + this.d * ty + this.f;
33
+ return this;
34
+ }
35
+ scaleSelf(sx, sy = sx) {
36
+ this.a *= sx;
37
+ this.b *= sx;
38
+ this.c *= sy;
39
+ this.d *= sy;
40
+ return this;
41
+ }
42
+ };
43
+ }
44
+
45
+ function stubBrowserGlobals() {
46
+ polyfillDOMMatrix();
47
+ }
48
+
3
49
  let resolvedModule;
4
50
  const isNode = globalThis.process?.release?.name === "node";
5
51
  const isBrowser = typeof window !== "undefined";
6
52
  async function getDocumentProxy(data, options = {}) {
7
53
  const { getDocument } = await getResolvedPDFJS();
54
+ let nodeDefaults = {};
55
+ if (isNode) {
56
+ try {
57
+ const base = undefined("pdfjs-dist/package.json");
58
+ nodeDefaults = {
59
+ disableFontFace: true,
60
+ standardFontDataUrl: new URL("./standard_fonts/", base).href
61
+ };
62
+ } catch {
63
+ }
64
+ }
8
65
  const pdf = await getDocument({
9
66
  data,
10
67
  isEvalSupported: false,
11
68
  // See: https://github.com/mozilla/pdf.js/issues/4244#issuecomment-1479534301
12
69
  useSystemFonts: true,
70
+ ...nodeDefaults,
13
71
  ...options
14
72
  }).promise;
15
73
  return pdf;
@@ -24,6 +82,7 @@ async function resolvePDFJSImport(pdfjsResolver, { reload = false } = {}) {
24
82
  if (resolvedModule && !reload) {
25
83
  return;
26
84
  }
85
+ stubBrowserGlobals();
27
86
  if (pdfjsResolver) {
28
87
  try {
29
88
  resolvedModule = await interopDefault(pdfjsResolver());
@@ -122,8 +181,7 @@ async function resolveCanvasModule(canvasImport) {
122
181
  function injectCanvasConstructors() {
123
182
  if (!resolvedCanvasModule)
124
183
  return;
125
- if (typeof globalThis.DOMMatrix === "undefined")
126
- globalThis.DOMMatrix = resolvedCanvasModule.DOMMatrix;
184
+ globalThis.DOMMatrix = resolvedCanvasModule.DOMMatrix;
127
185
  if (typeof globalThis.ImageData === "undefined")
128
186
  globalThis.ImageData = resolvedCanvasModule.ImageData;
129
187
  if (typeof globalThis.Path2D === "undefined")
package/dist/index.d.cts CHANGED
@@ -136,6 +136,10 @@ declare function configureUnPDF(options: {
136
136
  * Applies the following defaults:
137
137
  * - `isEvalSupported: false`
138
138
  * - `useSystemFonts: true`
139
+ *
140
+ * In Node.js environments, additionally applies:
141
+ * - `disableFontFace: true`
142
+ * - `standardFontDataUrl` resolved from the local `pdfjs-dist` package
139
143
  */
140
144
  declare function getDocumentProxy(data: DocumentInitParameters['data'], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
141
145
  declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
package/dist/index.d.mts CHANGED
@@ -136,6 +136,10 @@ declare function configureUnPDF(options: {
136
136
  * Applies the following defaults:
137
137
  * - `isEvalSupported: false`
138
138
  * - `useSystemFonts: true`
139
+ *
140
+ * In Node.js environments, additionally applies:
141
+ * - `disableFontFace: true`
142
+ * - `standardFontDataUrl` resolved from the local `pdfjs-dist` package
139
143
  */
140
144
  declare function getDocumentProxy(data: DocumentInitParameters['data'], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
141
145
  declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
package/dist/index.d.ts CHANGED
@@ -136,6 +136,10 @@ declare function configureUnPDF(options: {
136
136
  * Applies the following defaults:
137
137
  * - `isEvalSupported: false`
138
138
  * - `useSystemFonts: true`
139
+ *
140
+ * In Node.js environments, additionally applies:
141
+ * - `disableFontFace: true`
142
+ * - `standardFontDataUrl` resolved from the local `pdfjs-dist` package
139
143
  */
140
144
  declare function getDocumentProxy(data: DocumentInitParameters['data'], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
141
145
  declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
package/dist/index.mjs CHANGED
@@ -1,13 +1,71 @@
1
+ function polyfillDOMMatrix() {
2
+ if (typeof globalThis.DOMMatrix !== "undefined")
3
+ return;
4
+ globalThis.DOMMatrix = class DOMMatrix {
5
+ a;
6
+ b;
7
+ c;
8
+ d;
9
+ e;
10
+ f;
11
+ constructor(init) {
12
+ if (Array.isArray(init) && init.length === 6) {
13
+ this.a = init[0];
14
+ this.b = init[1];
15
+ this.c = init[2];
16
+ this.d = init[3];
17
+ this.e = init[4];
18
+ this.f = init[5];
19
+ } else {
20
+ this.a = 1;
21
+ this.b = 0;
22
+ this.c = 0;
23
+ this.d = 1;
24
+ this.e = 0;
25
+ this.f = 0;
26
+ }
27
+ }
28
+ translateSelf(tx, ty = 0) {
29
+ this.e = this.a * tx + this.c * ty + this.e;
30
+ this.f = this.b * tx + this.d * ty + this.f;
31
+ return this;
32
+ }
33
+ scaleSelf(sx, sy = sx) {
34
+ this.a *= sx;
35
+ this.b *= sx;
36
+ this.c *= sy;
37
+ this.d *= sy;
38
+ return this;
39
+ }
40
+ };
41
+ }
42
+
43
+ function stubBrowserGlobals() {
44
+ polyfillDOMMatrix();
45
+ }
46
+
1
47
  let resolvedModule;
2
48
  const isNode = globalThis.process?.release?.name === "node";
3
49
  const isBrowser = typeof window !== "undefined";
4
50
  async function getDocumentProxy(data, options = {}) {
5
51
  const { getDocument } = await getResolvedPDFJS();
52
+ let nodeDefaults = {};
53
+ if (isNode) {
54
+ try {
55
+ const base = import.meta.resolve("pdfjs-dist/package.json");
56
+ nodeDefaults = {
57
+ disableFontFace: true,
58
+ standardFontDataUrl: new URL("./standard_fonts/", base).href
59
+ };
60
+ } catch {
61
+ }
62
+ }
6
63
  const pdf = await getDocument({
7
64
  data,
8
65
  isEvalSupported: false,
9
66
  // See: https://github.com/mozilla/pdf.js/issues/4244#issuecomment-1479534301
10
67
  useSystemFonts: true,
68
+ ...nodeDefaults,
11
69
  ...options
12
70
  }).promise;
13
71
  return pdf;
@@ -22,6 +80,7 @@ async function resolvePDFJSImport(pdfjsResolver, { reload = false } = {}) {
22
80
  if (resolvedModule && !reload) {
23
81
  return;
24
82
  }
83
+ stubBrowserGlobals();
25
84
  if (pdfjsResolver) {
26
85
  try {
27
86
  resolvedModule = await interopDefault(pdfjsResolver());
@@ -120,8 +179,7 @@ async function resolveCanvasModule(canvasImport) {
120
179
  function injectCanvasConstructors() {
121
180
  if (!resolvedCanvasModule)
122
181
  return;
123
- if (typeof globalThis.DOMMatrix === "undefined")
124
- globalThis.DOMMatrix = resolvedCanvasModule.DOMMatrix;
182
+ globalThis.DOMMatrix = resolvedCanvasModule.DOMMatrix;
125
183
  if (typeof globalThis.ImageData === "undefined")
126
184
  globalThis.ImageData = resolvedCanvasModule.ImageData;
127
185
  if (typeof globalThis.Path2D === "undefined")