unpdf 0.8.0 → 0.10.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 +35 -37
- package/dist/index.cjs +13 -5
- package/dist/index.d.cts +11 -9
- package/dist/index.d.mts +11 -9
- package/dist/index.d.ts +11 -9
- package/dist/index.mjs +13 -5
- package/dist/pdfjs.d.mts +3 -1
- package/dist/pdfjs.d.ts +3 -1
- package/dist/pdfjs.mjs +1 -1
- package/package.json +4 -1
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#
|
|
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
|
-
###
|
|
58
|
+
### Access the PDF.js API
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
This will return the resolved PDF.js module and gives full access to the PDF.js API, like:
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
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 {
|
|
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,36 @@ 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 method, define the PDF.js module
|
|
91
|
+
// if you need another PDF.js build
|
|
92
|
+
import { configureUnPDF } from "unpdf";
|
|
93
|
+
|
|
94
|
+
configureUnPDF({
|
|
95
|
+
// Use the official PDF.js build (make sure to install it first)
|
|
96
|
+
pdfjs: () => import("pdfjs-dist"),
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// Now, you can use the other methods
|
|
100
|
+
// …
|
|
101
|
+
```
|
|
102
|
+
|
|
105
103
|
## Config
|
|
106
104
|
|
|
107
105
|
```ts
|
|
108
106
|
interface UnPDFConfiguration {
|
|
109
107
|
/**
|
|
110
|
-
* By default, UnPDF will use the latest version of PDF.js
|
|
111
|
-
*
|
|
112
|
-
*
|
|
108
|
+
* By default, UnPDF will use the latest version of PDF.js compiled for
|
|
109
|
+
* serverless environments. If you want to use a different version, you can
|
|
110
|
+
* provide a custom resolver function.
|
|
113
111
|
*
|
|
114
112
|
* @example
|
|
115
|
-
* // Use the
|
|
116
|
-
* () => import('pdfjs-dist
|
|
113
|
+
* // Use the official PDF.js build (make sure to install it first)
|
|
114
|
+
* () => import('pdfjs-dist')
|
|
117
115
|
*/
|
|
118
116
|
pdfjs?: () => Promise<PDFJS>;
|
|
119
117
|
}
|
|
@@ -121,12 +119,12 @@ interface UnPDFConfiguration {
|
|
|
121
119
|
|
|
122
120
|
## Methods
|
|
123
121
|
|
|
124
|
-
### `
|
|
122
|
+
### `configureUnPDF`
|
|
125
123
|
|
|
126
124
|
Define a custom PDF.js module, like the legacy build. Make sure to call this method before using any other methods.
|
|
127
125
|
|
|
128
126
|
```ts
|
|
129
|
-
function
|
|
127
|
+
function configureUnPDF(config: UnPDFConfiguration): Promise<void>;
|
|
130
128
|
```
|
|
131
129
|
|
|
132
130
|
### `getResolvedPDFJS`
|
|
@@ -175,9 +173,9 @@ In order to use this method, you have to meet the following requirements:
|
|
|
175
173
|
**Example**
|
|
176
174
|
|
|
177
175
|
```ts
|
|
178
|
-
import {
|
|
176
|
+
import { configureUnPDF, renderPageAsImage } from "unpdf";
|
|
179
177
|
|
|
180
|
-
|
|
178
|
+
configureUnPDF({
|
|
181
179
|
// Use the official PDF.js build
|
|
182
180
|
pdfjs: () => import("pdfjs-dist"),
|
|
183
181
|
});
|
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;
|
|
@@ -25,6 +27,10 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
|
25
27
|
if (pdfjsResolver) {
|
|
26
28
|
try {
|
|
27
29
|
const _import = await pdfjsResolver();
|
|
30
|
+
if ("resolvePDFJS" in _import) {
|
|
31
|
+
resolvedModule = await _import.resolvePDFJS();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
28
34
|
resolvedModule = _import.default || _import;
|
|
29
35
|
return;
|
|
30
36
|
} catch {
|
|
@@ -34,7 +40,8 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
|
34
40
|
}
|
|
35
41
|
}
|
|
36
42
|
try {
|
|
37
|
-
|
|
43
|
+
const { resolvePDFJS } = await import('unpdf/pdfjs');
|
|
44
|
+
resolvedModule = await resolvePDFJS();
|
|
38
45
|
} catch {
|
|
39
46
|
throw new Error(
|
|
40
47
|
"PDF.js is not available. Please add the package as a dependency."
|
|
@@ -135,7 +142,8 @@ async function createIsomorphicCanvasFactory(canvas) {
|
|
|
135
142
|
canvas2.width = width;
|
|
136
143
|
canvas2.height = height;
|
|
137
144
|
return canvas2;
|
|
138
|
-
}
|
|
145
|
+
}
|
|
146
|
+
if (isNode) {
|
|
139
147
|
if (!_canvas) {
|
|
140
148
|
throw new Error('Failed to resolve "canvas" package.');
|
|
141
149
|
}
|
|
@@ -170,13 +178,14 @@ async function createIsomorphicCanvasFactory(canvas) {
|
|
|
170
178
|
};
|
|
171
179
|
}
|
|
172
180
|
|
|
173
|
-
async function
|
|
181
|
+
async function configureUnPDF(options) {
|
|
174
182
|
const { pdfjs } = { ...options };
|
|
175
183
|
if (pdfjs) {
|
|
176
184
|
await resolvePDFJSImports(pdfjs, { force: true });
|
|
177
185
|
}
|
|
178
186
|
}
|
|
179
187
|
|
|
188
|
+
const defineUnPDFConfig = configureUnPDF;
|
|
180
189
|
const getMeta = async (...args) => {
|
|
181
190
|
await resolvePDFJSImports();
|
|
182
191
|
return await getMeta$1(...args);
|
|
@@ -185,7 +194,6 @@ const extractText = async (...args) => {
|
|
|
185
194
|
await resolvePDFJSImports();
|
|
186
195
|
return await extractText$1(...args);
|
|
187
196
|
};
|
|
188
|
-
const extractPDFText = extractText;
|
|
189
197
|
const extractImages = async (...args) => {
|
|
190
198
|
await resolvePDFJSImports();
|
|
191
199
|
return await extractImages$1(...args);
|
|
@@ -195,9 +203,9 @@ const renderPageAsImage = async (...args) => {
|
|
|
195
203
|
return await renderPageAsImage$1(...args);
|
|
196
204
|
};
|
|
197
205
|
|
|
206
|
+
exports.configureUnPDF = configureUnPDF;
|
|
198
207
|
exports.defineUnPDFConfig = defineUnPDFConfig;
|
|
199
208
|
exports.extractImages = extractImages;
|
|
200
|
-
exports.extractPDFText = extractPDFText;
|
|
201
209
|
exports.extractText = extractText;
|
|
202
210
|
exports.getDocumentProxy = getDocumentProxy;
|
|
203
211
|
exports.getMeta = getMeta;
|
package/dist/index.d.cts
CHANGED
|
@@ -26,24 +26,25 @@ 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
|
|
30
|
-
*
|
|
31
|
-
*
|
|
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
|
|
35
|
-
* () => import('pdfjs-dist
|
|
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
|
}
|
|
39
39
|
|
|
40
|
-
declare function
|
|
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,
|
|
63
|
+
export { configureUnPDF, defineUnPDFConfig, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
|
package/dist/index.d.mts
CHANGED
|
@@ -26,24 +26,25 @@ 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
|
|
30
|
-
*
|
|
31
|
-
*
|
|
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
|
|
35
|
-
* () => import('pdfjs-dist
|
|
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
|
}
|
|
39
39
|
|
|
40
|
-
declare function
|
|
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,
|
|
63
|
+
export { configureUnPDF, defineUnPDFConfig, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
|
package/dist/index.d.ts
CHANGED
|
@@ -26,24 +26,25 @@ 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
|
|
30
|
-
*
|
|
31
|
-
*
|
|
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
|
|
35
|
-
* () => import('pdfjs-dist
|
|
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
|
}
|
|
39
39
|
|
|
40
|
-
declare function
|
|
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,
|
|
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;
|
|
@@ -23,6 +25,10 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
|
23
25
|
if (pdfjsResolver) {
|
|
24
26
|
try {
|
|
25
27
|
const _import = await pdfjsResolver();
|
|
28
|
+
if ("resolvePDFJS" in _import) {
|
|
29
|
+
resolvedModule = await _import.resolvePDFJS();
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
26
32
|
resolvedModule = _import.default || _import;
|
|
27
33
|
return;
|
|
28
34
|
} catch {
|
|
@@ -32,7 +38,8 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
|
32
38
|
}
|
|
33
39
|
}
|
|
34
40
|
try {
|
|
35
|
-
|
|
41
|
+
const { resolvePDFJS } = await import('unpdf/pdfjs');
|
|
42
|
+
resolvedModule = await resolvePDFJS();
|
|
36
43
|
} catch {
|
|
37
44
|
throw new Error(
|
|
38
45
|
"PDF.js is not available. Please add the package as a dependency."
|
|
@@ -133,7 +140,8 @@ async function createIsomorphicCanvasFactory(canvas) {
|
|
|
133
140
|
canvas2.width = width;
|
|
134
141
|
canvas2.height = height;
|
|
135
142
|
return canvas2;
|
|
136
|
-
}
|
|
143
|
+
}
|
|
144
|
+
if (isNode) {
|
|
137
145
|
if (!_canvas) {
|
|
138
146
|
throw new Error('Failed to resolve "canvas" package.');
|
|
139
147
|
}
|
|
@@ -168,13 +176,14 @@ async function createIsomorphicCanvasFactory(canvas) {
|
|
|
168
176
|
};
|
|
169
177
|
}
|
|
170
178
|
|
|
171
|
-
async function
|
|
179
|
+
async function configureUnPDF(options) {
|
|
172
180
|
const { pdfjs } = { ...options };
|
|
173
181
|
if (pdfjs) {
|
|
174
182
|
await resolvePDFJSImports(pdfjs, { force: true });
|
|
175
183
|
}
|
|
176
184
|
}
|
|
177
185
|
|
|
186
|
+
const defineUnPDFConfig = configureUnPDF;
|
|
178
187
|
const getMeta = async (...args) => {
|
|
179
188
|
await resolvePDFJSImports();
|
|
180
189
|
return await getMeta$1(...args);
|
|
@@ -183,7 +192,6 @@ const extractText = async (...args) => {
|
|
|
183
192
|
await resolvePDFJSImports();
|
|
184
193
|
return await extractText$1(...args);
|
|
185
194
|
};
|
|
186
|
-
const extractPDFText = extractText;
|
|
187
195
|
const extractImages = async (...args) => {
|
|
188
196
|
await resolvePDFJSImports();
|
|
189
197
|
return await extractImages$1(...args);
|
|
@@ -193,4 +201,4 @@ const renderPageAsImage = async (...args) => {
|
|
|
193
201
|
return await renderPageAsImage$1(...args);
|
|
194
202
|
};
|
|
195
203
|
|
|
196
|
-
export { defineUnPDFConfig, extractImages,
|
|
204
|
+
export { configureUnPDF, defineUnPDFConfig, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
|
package/dist/pdfjs.d.mts
CHANGED
package/dist/pdfjs.d.ts
CHANGED