unpdf 0.8.0 → 0.9.0

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
@@ -9,7 +9,7 @@ This library is also intended as a modern alternative to the unmaintained but st
9
9
  ## Features
10
10
 
11
11
  - 🏗️ Works in Node.js, browser and workers
12
- - 🪭 Includes serverless build of PDF.js ([`unpdf/pdfjs`](./package.json#L45))
12
+ - 🪭 Includes serverless build of PDF.js ([`unpdf/pdfjs`](./package.json#L34))
13
13
  - 💬 Extract text and images from PDFs
14
14
  - 🧱 Opt-in to legacy PDF.js build
15
15
  - 💨 Zero dependencies
@@ -55,40 +55,20 @@ const pdf = await getDocumentProxy(new Uint8Array(pdf));
55
55
  const { totalPages, text } = await extractText(pdf, { mergePages: true });
56
56
  ```
57
57
 
58
- ### Use Legacy Or Custom PDF.js Build
58
+ ### Access the PDF.js API
59
59
 
60
- Generally, 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.
60
+ This will return the resolved PDF.js module and gives full access to the PDF.js API, like:
61
61
 
62
- ```ts
63
- // Before using any other methods, define the PDF.js module
64
- import { defineUnPDFConfig } from "unpdf";
65
-
66
- defineUnPDFConfig({
67
- // Use the legacy build
68
- pdfjs: () => import("pdfjs-dist/legacy/build/pdf.js"),
69
- });
70
-
71
- // Now, you can use the other methods
72
- // …
73
- ```
62
+ - `getDocument`
63
+ - `version`
64
+ - … and all other methods
74
65
 
75
- ### Access the PDF.js Module
76
-
77
- This will return the resolved PDF.js module. If no build is defined, the serverless build bundled with `unpdf` will be initialized.
66
+ Especially useful for platforms like 🦕 Deno or if you want to use the PDF.js API directly. If no custom build was defined beforehand, the serverless build bundled with `unpdf` will be initialized.
78
67
 
79
68
  ```ts
80
69
  import { getResolvedPDFJS } from "unpdf";
81
70
 
82
- const { version } = await getResolvedPDFJS();
83
- ```
84
-
85
- ### Use Serverless PDF.js Build In 🦕 Deno
86
-
87
- Instead of using the methods provided by `unpdf`, you can directly import the serverless PDF.js build in Deno. This is useful if you want to use the PDF.js API directly.
88
-
89
- ```ts
90
- import { getDocument } from "https://esm.sh/unpdf/pdfjs";
91
-
71
+ const { getDocument } = await getResolvedPDFJS();
92
72
  const data = Deno.readFileSync("dummy.pdf");
93
73
  const doc = await getDocument(data).promise;
94
74
 
@@ -102,18 +82,35 @@ for (let i = 1; i <= doc.numPages; i++) {
102
82
  }
103
83
  ```
104
84
 
85
+ ### Use Official or Legacy PDF.js Build
86
+
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
+
89
+ ```ts
90
+ // Before using any other methods, define the PDF.js module
91
+ import { defineUnPDFConfig } from "unpdf";
92
+
93
+ defineUnPDFConfig({
94
+ // Use the official PDF.js build (make sure to install it first)
95
+ pdfjs: () => import("pdfjs-dist"),
96
+ });
97
+
98
+ // Now, you can use the other methods
99
+ // …
100
+ ```
101
+
105
102
  ## Config
106
103
 
107
104
  ```ts
108
105
  interface UnPDFConfiguration {
109
106
  /**
110
- * By default, UnPDF will use the latest version of PDF.js. If you want to
111
- * use an older version or the legacy build, set a promise that resolves to
112
- * the PDF.js module.
107
+ * By default, UnPDF will use the latest version of PDF.js compiled for
108
+ * serverless environments. If you want to use a different version, you can
109
+ * provide a custom resolver function.
113
110
  *
114
111
  * @example
115
- * // Use the legacy build
116
- * () => import('pdfjs-dist/legacy/build/pdf.js')
112
+ * // Use the official PDF.js build (make sure to install it first)
113
+ * () => import('pdfjs-dist')
117
114
  */
118
115
  pdfjs?: () => Promise<PDFJS>;
119
116
  }
package/dist/index.cjs CHANGED
@@ -25,6 +25,10 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
25
25
  if (pdfjsResolver) {
26
26
  try {
27
27
  const _import = await pdfjsResolver();
28
+ if ("resolvePDFJS" in _import) {
29
+ resolvedModule = await _import.resolvePDFJS();
30
+ return;
31
+ }
28
32
  resolvedModule = _import.default || _import;
29
33
  return;
30
34
  } catch {
@@ -34,7 +38,8 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
34
38
  }
35
39
  }
36
40
  try {
37
- resolvedModule = await import('unpdf/pdfjs');
41
+ const { resolvePDFJS } = await import('unpdf/pdfjs');
42
+ resolvedModule = await resolvePDFJS();
38
43
  } catch {
39
44
  throw new Error(
40
45
  "PDF.js is not available. Please add the package as a dependency."
package/dist/index.d.cts CHANGED
@@ -26,13 +26,13 @@ declare function renderPageAsImage$1(data: BinaryData | PDFDocumentProxy, pageNu
26
26
  type PDFJS = typeof _PDFJS;
27
27
  interface UnPDFConfiguration {
28
28
  /**
29
- * By default, UnPDF will use the latest version of PDF.js. If you want to
30
- * use an older version or the legacy build, set a promise that resolves to
31
- * the PDF.js module.
29
+ * By default, UnPDF will use the latest version of PDF.js compiled for
30
+ * serverless environments. If you want to use a different version, you can
31
+ * provide a custom resolver function.
32
32
  *
33
33
  * @example
34
- * // Use the legacy build
35
- * () => import('pdfjs-dist/legacy/build/pdf.js')
34
+ * // Use the official PDF.js build (make sure to install it first)
35
+ * () => import('pdfjs-dist')
36
36
  */
37
37
  pdfjs?: () => Promise<any>;
38
38
  }
package/dist/index.d.mts CHANGED
@@ -26,13 +26,13 @@ declare function renderPageAsImage$1(data: BinaryData | PDFDocumentProxy, pageNu
26
26
  type PDFJS = typeof _PDFJS;
27
27
  interface UnPDFConfiguration {
28
28
  /**
29
- * By default, UnPDF will use the latest version of PDF.js. If you want to
30
- * use an older version or the legacy build, set a promise that resolves to
31
- * the PDF.js module.
29
+ * By default, UnPDF will use the latest version of PDF.js compiled for
30
+ * serverless environments. If you want to use a different version, you can
31
+ * provide a custom resolver function.
32
32
  *
33
33
  * @example
34
- * // Use the legacy build
35
- * () => import('pdfjs-dist/legacy/build/pdf.js')
34
+ * // Use the official PDF.js build (make sure to install it first)
35
+ * () => import('pdfjs-dist')
36
36
  */
37
37
  pdfjs?: () => Promise<any>;
38
38
  }
package/dist/index.d.ts CHANGED
@@ -26,13 +26,13 @@ declare function renderPageAsImage$1(data: BinaryData | PDFDocumentProxy, pageNu
26
26
  type PDFJS = typeof _PDFJS;
27
27
  interface UnPDFConfiguration {
28
28
  /**
29
- * By default, UnPDF will use the latest version of PDF.js. If you want to
30
- * use an older version or the legacy build, set a promise that resolves to
31
- * the PDF.js module.
29
+ * By default, UnPDF will use the latest version of PDF.js compiled for
30
+ * serverless environments. If you want to use a different version, you can
31
+ * provide a custom resolver function.
32
32
  *
33
33
  * @example
34
- * // Use the legacy build
35
- * () => import('pdfjs-dist/legacy/build/pdf.js')
34
+ * // Use the official PDF.js build (make sure to install it first)
35
+ * () => import('pdfjs-dist')
36
36
  */
37
37
  pdfjs?: () => Promise<any>;
38
38
  }
package/dist/index.mjs CHANGED
@@ -23,6 +23,10 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
23
23
  if (pdfjsResolver) {
24
24
  try {
25
25
  const _import = await pdfjsResolver();
26
+ if ("resolvePDFJS" in _import) {
27
+ resolvedModule = await _import.resolvePDFJS();
28
+ return;
29
+ }
26
30
  resolvedModule = _import.default || _import;
27
31
  return;
28
32
  } catch {
@@ -32,7 +36,8 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
32
36
  }
33
37
  }
34
38
  try {
35
- resolvedModule = await import('unpdf/pdfjs');
39
+ const { resolvePDFJS } = await import('unpdf/pdfjs');
40
+ resolvedModule = await resolvePDFJS();
36
41
  } catch {
37
42
  throw new Error(
38
43
  "PDF.js is not available. Please add the package as a dependency."
package/dist/pdfjs.d.mts CHANGED
@@ -1 +1,3 @@
1
- export * from './types/src/pdf.d.ts'
1
+ import * as PDFJS from './types/src/pdf'
2
+ declare function resolvePDFJS(): Promise<typeof PDFJS>
3
+ export { resolvePDFJS }
package/dist/pdfjs.d.ts CHANGED
@@ -1 +1,3 @@
1
- export * from './types/src/pdf.d.ts'
1
+ import * as PDFJS from './types/src/pdf'
2
+ declare function resolvePDFJS(): Promise<typeof PDFJS>
3
+ export { resolvePDFJS }