unpdf 0.6.1 → 0.7.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 +61 -15
- package/dist/index.cjs +114 -22
- package/dist/index.d.cts +20 -10
- package/dist/index.d.mts +20 -10
- package/dist/index.d.ts +20 -10
- package/dist/index.mjs +111 -21
- package/dist/pdfjs.mjs +1 -1
- package/package.json +24 -4
package/README.md
CHANGED
|
@@ -34,25 +34,25 @@ yarn add unpdf
|
|
|
34
34
|
### Extract Text From PDF
|
|
35
35
|
|
|
36
36
|
```ts
|
|
37
|
-
import {
|
|
37
|
+
import { extractText, getDocumentProxy } from "unpdf";
|
|
38
38
|
|
|
39
39
|
// Fetch a PDF file from the web
|
|
40
|
-
const
|
|
40
|
+
const buffer = await fetch(
|
|
41
41
|
"https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
|
|
42
42
|
).then((res) => res.arrayBuffer());
|
|
43
43
|
|
|
44
44
|
// Or load it from the filesystem
|
|
45
|
-
const
|
|
45
|
+
const buffer = await readFile("./dummy.pdf");
|
|
46
46
|
|
|
47
|
-
//
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
});
|
|
47
|
+
// Load PDF from buffer
|
|
48
|
+
const pdf = await getDocumentProxy(new Uint8Array(pdf));
|
|
49
|
+
// Extract text from PDF
|
|
50
|
+
const { totalPages, text } = await extractText(pdf, { mergePages: true });
|
|
51
51
|
```
|
|
52
52
|
|
|
53
53
|
### Use Legacy Or Custom PDF.js Build
|
|
54
54
|
|
|
55
|
-
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
|
|
55
|
+
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.
|
|
56
56
|
|
|
57
57
|
```ts
|
|
58
58
|
// Before using any other methods, define the PDF.js module
|
|
@@ -132,19 +132,21 @@ Returns the resolved PDF.js module. If no build is defined, the latest version w
|
|
|
132
132
|
function getResolvedPDFJS(): Promise<PDFJS>;
|
|
133
133
|
```
|
|
134
134
|
|
|
135
|
-
### `
|
|
135
|
+
### `getMeta`
|
|
136
136
|
|
|
137
137
|
```ts
|
|
138
|
-
function
|
|
138
|
+
function getMeta(data: BinaryData | PDFDocumentProxy): Promise<{
|
|
139
139
|
info: Record<string, any>;
|
|
140
140
|
metadata: Record<string, any>;
|
|
141
141
|
}>;
|
|
142
142
|
```
|
|
143
143
|
|
|
144
|
-
### `
|
|
144
|
+
### `extractText`
|
|
145
|
+
|
|
146
|
+
Extracts all text from a PDF. If `mergePages` is set to `true`, the text of all pages will be merged into a single string. Otherwise, an array of strings for each page will be returned.
|
|
145
147
|
|
|
146
148
|
```ts
|
|
147
|
-
function
|
|
149
|
+
function extractText(
|
|
148
150
|
data: BinaryData | PDFDocumentProxy,
|
|
149
151
|
{ mergePages }?: { mergePages?: boolean },
|
|
150
152
|
): Promise<{
|
|
@@ -153,13 +155,57 @@ function extractPDFText(
|
|
|
153
155
|
}>;
|
|
154
156
|
```
|
|
155
157
|
|
|
156
|
-
### `
|
|
158
|
+
### `renderPageAsImage`
|
|
159
|
+
|
|
160
|
+
> [!NOTE]
|
|
161
|
+
> This method will only work in Node.js and browser environments.
|
|
162
|
+
|
|
163
|
+
To render a PDF page as an image, you can use the `renderPageAsImage` method. This method will return an `ArrayBuffer` of the rendered image.
|
|
164
|
+
|
|
165
|
+
In order to use this method, you have to meet the following requirements:
|
|
166
|
+
|
|
167
|
+
- Use the official PDF.js build
|
|
168
|
+
- Install the [`canvas`](https://www.npmjs.com/package/canvas) package in Node.js environments
|
|
169
|
+
|
|
170
|
+
**Example**
|
|
171
|
+
|
|
172
|
+
```ts
|
|
173
|
+
import { defineUnPDFConfig, renderPageAsImage } from "unpdf";
|
|
174
|
+
|
|
175
|
+
defineUnPDFConfig({
|
|
176
|
+
// Use the official PDF.js build
|
|
177
|
+
pdfjs: () => import("pdfjs-dist"),
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
const pdf = await readFile("./dummy.pdf");
|
|
181
|
+
const buffer = new Uint8Array(pdf);
|
|
182
|
+
|
|
183
|
+
const result = await renderPageAsImage(buffer, 1);
|
|
184
|
+
await writeFile("dummy-page-1.png", Buffer.from(result));
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
**Type Declaration**
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
declare function renderPageAsImage(
|
|
191
|
+
data: BinaryData | PDFDocumentProxy,
|
|
192
|
+
pageNumber: number,
|
|
193
|
+
options?: {
|
|
194
|
+
/** @default 1 */
|
|
195
|
+
scale?: number;
|
|
196
|
+
width?: number;
|
|
197
|
+
height?: number;
|
|
198
|
+
},
|
|
199
|
+
): Promise<ArrayBuffer>;
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### `extractImages`
|
|
157
203
|
|
|
158
204
|
```ts
|
|
159
|
-
function
|
|
205
|
+
function extractImages(
|
|
160
206
|
data: BinaryData | PDFDocumentProxy,
|
|
161
207
|
pageNumber: number,
|
|
162
|
-
): Promise<
|
|
208
|
+
): Promise<Uint8ClampedArray[]>;
|
|
163
209
|
```
|
|
164
210
|
|
|
165
211
|
## FAQ
|
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
let resolvedModule;
|
|
4
|
+
const isNode = globalThis.process?.release?.name === "node";
|
|
5
|
+
const isBrowser = typeof window !== "undefined";
|
|
4
6
|
async function getDocumentProxy(data, options = {}) {
|
|
5
7
|
const { getDocument } = await getResolvedPDFJS();
|
|
6
8
|
const pdf = await getDocument({
|
|
@@ -10,12 +12,6 @@ async function getDocumentProxy(data, options = {}) {
|
|
|
10
12
|
}).promise;
|
|
11
13
|
return pdf;
|
|
12
14
|
}
|
|
13
|
-
async function defineUnPDFConfig(options) {
|
|
14
|
-
const { pdfjs } = { ...options };
|
|
15
|
-
if (pdfjs) {
|
|
16
|
-
await resolvePDFJSImports(pdfjs, { force: true });
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
15
|
async function getResolvedPDFJS() {
|
|
20
16
|
if (!resolvedModule) {
|
|
21
17
|
await resolvePDFJSImports();
|
|
@@ -49,7 +45,7 @@ function isPDFDocumentProxy(data) {
|
|
|
49
45
|
return typeof data === "object" && data !== null && "_pdfInfo" in data;
|
|
50
46
|
}
|
|
51
47
|
|
|
52
|
-
async function
|
|
48
|
+
async function getMeta$1(data) {
|
|
53
49
|
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
54
50
|
const meta = await pdf.getMetadata();
|
|
55
51
|
return {
|
|
@@ -58,7 +54,7 @@ async function getPDFMeta$1(data) {
|
|
|
58
54
|
};
|
|
59
55
|
}
|
|
60
56
|
|
|
61
|
-
async function
|
|
57
|
+
async function extractText$1(data, options = {}) {
|
|
62
58
|
const { mergePages = false } = { ...options };
|
|
63
59
|
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
64
60
|
const texts = await Promise.all(
|
|
@@ -80,41 +76,137 @@ async function getPageText(pdf, pageNumber) {
|
|
|
80
76
|
return items.map(({ str }) => str).join("\n");
|
|
81
77
|
}
|
|
82
78
|
|
|
83
|
-
async function
|
|
79
|
+
async function extractImages$1(data, pageNumber) {
|
|
84
80
|
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
85
81
|
const page = await pdf.getPage(pageNumber);
|
|
82
|
+
if (pageNumber < 1 || pageNumber > pdf.numPages) {
|
|
83
|
+
throw new Error(
|
|
84
|
+
`Invalid page number. Must be between 1 and ${pdf.numPages}.`
|
|
85
|
+
);
|
|
86
|
+
}
|
|
86
87
|
const operatorList = await page.getOperatorList();
|
|
87
88
|
const { OPS } = await getResolvedPDFJS();
|
|
88
89
|
const images = [];
|
|
89
|
-
for (
|
|
90
|
+
for (let i = 0; i < operatorList.fnArray.length; i++) {
|
|
91
|
+
const op = operatorList.fnArray[i];
|
|
90
92
|
if (op !== OPS.paintImageXObject) {
|
|
91
93
|
continue;
|
|
92
94
|
}
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
95
|
+
const imageKey = operatorList.argsArray[i][0];
|
|
96
|
+
const image = await page.objs.get(imageKey);
|
|
97
|
+
images.push(image.data);
|
|
97
98
|
}
|
|
98
99
|
return images;
|
|
99
100
|
}
|
|
101
|
+
async function renderPageAsImage$1(data, pageNumber, options = {}) {
|
|
102
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
103
|
+
const page = await pdf.getPage(pageNumber);
|
|
104
|
+
if (pageNumber < 1 || pageNumber > pdf.numPages) {
|
|
105
|
+
throw new Error(
|
|
106
|
+
`Invalid page number. Must be between 1 and ${pdf.numPages}.`
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
let outputScale = options.scale || 1;
|
|
110
|
+
let viewport = page.getViewport({ scale: outputScale });
|
|
111
|
+
if (options.width) {
|
|
112
|
+
outputScale = options.width / viewport.width;
|
|
113
|
+
} else if (options.height) {
|
|
114
|
+
outputScale = options.height / viewport.height;
|
|
115
|
+
}
|
|
116
|
+
if (outputScale !== 1 && outputScale > 0) {
|
|
117
|
+
viewport = page.getViewport({ scale: outputScale });
|
|
118
|
+
}
|
|
119
|
+
const canvasFactory = await createIsomorphicCanvasFactory();
|
|
120
|
+
const ctx = canvasFactory.create(viewport.width, viewport.height);
|
|
121
|
+
await page.render({
|
|
122
|
+
canvasContext: ctx.context,
|
|
123
|
+
viewport
|
|
124
|
+
}).promise;
|
|
125
|
+
const dataUrl = isBrowser ? ctx.canvas.toDataURL() : ctx.canvas.toDataURL();
|
|
126
|
+
const response = await fetch(dataUrl);
|
|
127
|
+
return await response.arrayBuffer();
|
|
128
|
+
}
|
|
129
|
+
async function createIsomorphicCanvasFactory() {
|
|
130
|
+
let _canvas;
|
|
131
|
+
if (isNode) {
|
|
132
|
+
try {
|
|
133
|
+
_canvas = await import('canvas');
|
|
134
|
+
} catch {
|
|
135
|
+
throw new Error(
|
|
136
|
+
'The "canvas" package is required when running in Node.js.'
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
_createCanvas(width, height) {
|
|
142
|
+
if (isBrowser) {
|
|
143
|
+
const canvas = document.createElement("canvas");
|
|
144
|
+
canvas.width = width;
|
|
145
|
+
canvas.height = height;
|
|
146
|
+
return canvas;
|
|
147
|
+
} else if (isNode) {
|
|
148
|
+
return _canvas.createCanvas(width, height);
|
|
149
|
+
}
|
|
150
|
+
throw new Error("Unsupported environment for canvas creation.");
|
|
151
|
+
},
|
|
152
|
+
create(width, height) {
|
|
153
|
+
const _canvas2 = this._createCanvas(width, height);
|
|
154
|
+
const context = _canvas2.getContext(
|
|
155
|
+
"2d"
|
|
156
|
+
);
|
|
157
|
+
return {
|
|
158
|
+
canvas: _canvas2,
|
|
159
|
+
context
|
|
160
|
+
};
|
|
161
|
+
},
|
|
162
|
+
reset(ctx, width, height) {
|
|
163
|
+
if (ctx.canvas) {
|
|
164
|
+
ctx.canvas.width = width;
|
|
165
|
+
ctx.canvas.height = height;
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
destroy(ctx) {
|
|
169
|
+
if (ctx.canvas) {
|
|
170
|
+
ctx.canvas.width = 0;
|
|
171
|
+
ctx.canvas.height = 0;
|
|
172
|
+
}
|
|
173
|
+
ctx.canvas = void 0;
|
|
174
|
+
ctx.context = void 0;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
}
|
|
100
178
|
|
|
101
|
-
|
|
179
|
+
async function defineUnPDFConfig(options) {
|
|
180
|
+
const { pdfjs } = { ...options };
|
|
181
|
+
if (pdfjs) {
|
|
182
|
+
await resolvePDFJSImports(pdfjs, { force: true });
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const getMeta = async (...args) => {
|
|
187
|
+
await resolvePDFJSImports();
|
|
188
|
+
return await getMeta$1(...args);
|
|
189
|
+
};
|
|
190
|
+
const extractText = async (...args) => {
|
|
102
191
|
await resolvePDFJSImports();
|
|
103
|
-
return await
|
|
192
|
+
return await extractText$1(...args);
|
|
104
193
|
};
|
|
105
|
-
const extractPDFText =
|
|
194
|
+
const extractPDFText = extractText;
|
|
195
|
+
const extractImages = async (...args) => {
|
|
106
196
|
await resolvePDFJSImports();
|
|
107
|
-
return await
|
|
197
|
+
return await extractImages$1(...args);
|
|
108
198
|
};
|
|
109
|
-
const
|
|
199
|
+
const renderPageAsImage = async (...args) => {
|
|
110
200
|
await resolvePDFJSImports();
|
|
111
|
-
return await
|
|
201
|
+
return await renderPageAsImage$1(...args);
|
|
112
202
|
};
|
|
113
203
|
|
|
114
204
|
exports.defineUnPDFConfig = defineUnPDFConfig;
|
|
205
|
+
exports.extractImages = extractImages;
|
|
115
206
|
exports.extractPDFText = extractPDFText;
|
|
207
|
+
exports.extractText = extractText;
|
|
116
208
|
exports.getDocumentProxy = getDocumentProxy;
|
|
117
|
-
exports.
|
|
118
|
-
exports.getPDFMeta = getPDFMeta;
|
|
209
|
+
exports.getMeta = getMeta;
|
|
119
210
|
exports.getResolvedPDFJS = getResolvedPDFJS;
|
|
211
|
+
exports.renderPageAsImage = renderPageAsImage;
|
|
120
212
|
exports.resolvePDFJSImports = resolvePDFJSImports;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
import { BinaryData, PDFDocumentProxy, DocumentInitParameters } from './types/src/display/api';
|
|
2
2
|
import * as _PDFJS from './types/src/pdf';
|
|
3
3
|
|
|
4
|
-
declare function
|
|
4
|
+
declare function getMeta$1(data: BinaryData | PDFDocumentProxy): Promise<{
|
|
5
5
|
info: Record<string, any>;
|
|
6
6
|
metadata: Record<string, any>;
|
|
7
7
|
}>;
|
|
8
8
|
|
|
9
|
-
declare function
|
|
9
|
+
declare function extractText$1(data: BinaryData | PDFDocumentProxy, options?: {
|
|
10
10
|
mergePages?: boolean;
|
|
11
11
|
}): Promise<{
|
|
12
12
|
totalPages: number;
|
|
13
13
|
text: string | string[];
|
|
14
14
|
}>;
|
|
15
15
|
|
|
16
|
-
declare function
|
|
16
|
+
declare function extractImages$1(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
|
|
17
|
+
declare function renderPageAsImage$1(data: BinaryData | PDFDocumentProxy, pageNumber: number, options?: {
|
|
18
|
+
/** @default 1 */
|
|
19
|
+
scale?: number;
|
|
20
|
+
width?: number;
|
|
21
|
+
height?: number;
|
|
22
|
+
}): Promise<ArrayBuffer>;
|
|
17
23
|
|
|
18
24
|
type PDFJS = typeof _PDFJS;
|
|
19
25
|
interface UnPDFConfiguration {
|
|
@@ -26,9 +32,11 @@ interface UnPDFConfiguration {
|
|
|
26
32
|
* // Use the legacy build
|
|
27
33
|
* () => import('pdfjs-dist/legacy/build/pdf.js')
|
|
28
34
|
*/
|
|
29
|
-
pdfjs?: () => Promise<
|
|
35
|
+
pdfjs?: () => Promise<any>;
|
|
30
36
|
}
|
|
31
37
|
|
|
38
|
+
declare function defineUnPDFConfig(options: UnPDFConfiguration): Promise<void>;
|
|
39
|
+
|
|
32
40
|
/**
|
|
33
41
|
* Returns a PDFDocumentProxy instance from a given binary data.
|
|
34
42
|
*
|
|
@@ -36,14 +44,16 @@ interface UnPDFConfiguration {
|
|
|
36
44
|
* - `isEvalSupported: false`
|
|
37
45
|
*/
|
|
38
46
|
declare function getDocumentProxy(data: BinaryData, options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
|
|
39
|
-
declare function defineUnPDFConfig(options: UnPDFConfiguration): Promise<void>;
|
|
40
47
|
declare function getResolvedPDFJS(): Promise<PDFJS>;
|
|
41
|
-
declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<
|
|
48
|
+
declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<any>, { force }?: {
|
|
42
49
|
force?: boolean | undefined;
|
|
43
50
|
}): Promise<void>;
|
|
44
51
|
|
|
45
|
-
declare const
|
|
46
|
-
declare const
|
|
47
|
-
|
|
52
|
+
declare const getMeta: typeof getMeta$1;
|
|
53
|
+
declare const extractText: typeof extractText$1;
|
|
54
|
+
/** @deprecated Use `extractText` instead. */
|
|
55
|
+
declare const extractPDFText: typeof extractText$1;
|
|
56
|
+
declare const extractImages: typeof extractImages$1;
|
|
57
|
+
declare const renderPageAsImage: typeof renderPageAsImage$1;
|
|
48
58
|
|
|
49
|
-
export { defineUnPDFConfig, extractPDFText,
|
|
59
|
+
export { defineUnPDFConfig, extractImages, extractPDFText, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
import { BinaryData, PDFDocumentProxy, DocumentInitParameters } from './types/src/display/api';
|
|
2
2
|
import * as _PDFJS from './types/src/pdf';
|
|
3
3
|
|
|
4
|
-
declare function
|
|
4
|
+
declare function getMeta$1(data: BinaryData | PDFDocumentProxy): Promise<{
|
|
5
5
|
info: Record<string, any>;
|
|
6
6
|
metadata: Record<string, any>;
|
|
7
7
|
}>;
|
|
8
8
|
|
|
9
|
-
declare function
|
|
9
|
+
declare function extractText$1(data: BinaryData | PDFDocumentProxy, options?: {
|
|
10
10
|
mergePages?: boolean;
|
|
11
11
|
}): Promise<{
|
|
12
12
|
totalPages: number;
|
|
13
13
|
text: string | string[];
|
|
14
14
|
}>;
|
|
15
15
|
|
|
16
|
-
declare function
|
|
16
|
+
declare function extractImages$1(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
|
|
17
|
+
declare function renderPageAsImage$1(data: BinaryData | PDFDocumentProxy, pageNumber: number, options?: {
|
|
18
|
+
/** @default 1 */
|
|
19
|
+
scale?: number;
|
|
20
|
+
width?: number;
|
|
21
|
+
height?: number;
|
|
22
|
+
}): Promise<ArrayBuffer>;
|
|
17
23
|
|
|
18
24
|
type PDFJS = typeof _PDFJS;
|
|
19
25
|
interface UnPDFConfiguration {
|
|
@@ -26,9 +32,11 @@ interface UnPDFConfiguration {
|
|
|
26
32
|
* // Use the legacy build
|
|
27
33
|
* () => import('pdfjs-dist/legacy/build/pdf.js')
|
|
28
34
|
*/
|
|
29
|
-
pdfjs?: () => Promise<
|
|
35
|
+
pdfjs?: () => Promise<any>;
|
|
30
36
|
}
|
|
31
37
|
|
|
38
|
+
declare function defineUnPDFConfig(options: UnPDFConfiguration): Promise<void>;
|
|
39
|
+
|
|
32
40
|
/**
|
|
33
41
|
* Returns a PDFDocumentProxy instance from a given binary data.
|
|
34
42
|
*
|
|
@@ -36,14 +44,16 @@ interface UnPDFConfiguration {
|
|
|
36
44
|
* - `isEvalSupported: false`
|
|
37
45
|
*/
|
|
38
46
|
declare function getDocumentProxy(data: BinaryData, options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
|
|
39
|
-
declare function defineUnPDFConfig(options: UnPDFConfiguration): Promise<void>;
|
|
40
47
|
declare function getResolvedPDFJS(): Promise<PDFJS>;
|
|
41
|
-
declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<
|
|
48
|
+
declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<any>, { force }?: {
|
|
42
49
|
force?: boolean | undefined;
|
|
43
50
|
}): Promise<void>;
|
|
44
51
|
|
|
45
|
-
declare const
|
|
46
|
-
declare const
|
|
47
|
-
|
|
52
|
+
declare const getMeta: typeof getMeta$1;
|
|
53
|
+
declare const extractText: typeof extractText$1;
|
|
54
|
+
/** @deprecated Use `extractText` instead. */
|
|
55
|
+
declare const extractPDFText: typeof extractText$1;
|
|
56
|
+
declare const extractImages: typeof extractImages$1;
|
|
57
|
+
declare const renderPageAsImage: typeof renderPageAsImage$1;
|
|
48
58
|
|
|
49
|
-
export { defineUnPDFConfig, extractPDFText,
|
|
59
|
+
export { defineUnPDFConfig, extractImages, extractPDFText, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
import { BinaryData, PDFDocumentProxy, DocumentInitParameters } from './types/src/display/api';
|
|
2
2
|
import * as _PDFJS from './types/src/pdf';
|
|
3
3
|
|
|
4
|
-
declare function
|
|
4
|
+
declare function getMeta$1(data: BinaryData | PDFDocumentProxy): Promise<{
|
|
5
5
|
info: Record<string, any>;
|
|
6
6
|
metadata: Record<string, any>;
|
|
7
7
|
}>;
|
|
8
8
|
|
|
9
|
-
declare function
|
|
9
|
+
declare function extractText$1(data: BinaryData | PDFDocumentProxy, options?: {
|
|
10
10
|
mergePages?: boolean;
|
|
11
11
|
}): Promise<{
|
|
12
12
|
totalPages: number;
|
|
13
13
|
text: string | string[];
|
|
14
14
|
}>;
|
|
15
15
|
|
|
16
|
-
declare function
|
|
16
|
+
declare function extractImages$1(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
|
|
17
|
+
declare function renderPageAsImage$1(data: BinaryData | PDFDocumentProxy, pageNumber: number, options?: {
|
|
18
|
+
/** @default 1 */
|
|
19
|
+
scale?: number;
|
|
20
|
+
width?: number;
|
|
21
|
+
height?: number;
|
|
22
|
+
}): Promise<ArrayBuffer>;
|
|
17
23
|
|
|
18
24
|
type PDFJS = typeof _PDFJS;
|
|
19
25
|
interface UnPDFConfiguration {
|
|
@@ -26,9 +32,11 @@ interface UnPDFConfiguration {
|
|
|
26
32
|
* // Use the legacy build
|
|
27
33
|
* () => import('pdfjs-dist/legacy/build/pdf.js')
|
|
28
34
|
*/
|
|
29
|
-
pdfjs?: () => Promise<
|
|
35
|
+
pdfjs?: () => Promise<any>;
|
|
30
36
|
}
|
|
31
37
|
|
|
38
|
+
declare function defineUnPDFConfig(options: UnPDFConfiguration): Promise<void>;
|
|
39
|
+
|
|
32
40
|
/**
|
|
33
41
|
* Returns a PDFDocumentProxy instance from a given binary data.
|
|
34
42
|
*
|
|
@@ -36,14 +44,16 @@ interface UnPDFConfiguration {
|
|
|
36
44
|
* - `isEvalSupported: false`
|
|
37
45
|
*/
|
|
38
46
|
declare function getDocumentProxy(data: BinaryData, options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
|
|
39
|
-
declare function defineUnPDFConfig(options: UnPDFConfiguration): Promise<void>;
|
|
40
47
|
declare function getResolvedPDFJS(): Promise<PDFJS>;
|
|
41
|
-
declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<
|
|
48
|
+
declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<any>, { force }?: {
|
|
42
49
|
force?: boolean | undefined;
|
|
43
50
|
}): Promise<void>;
|
|
44
51
|
|
|
45
|
-
declare const
|
|
46
|
-
declare const
|
|
47
|
-
|
|
52
|
+
declare const getMeta: typeof getMeta$1;
|
|
53
|
+
declare const extractText: typeof extractText$1;
|
|
54
|
+
/** @deprecated Use `extractText` instead. */
|
|
55
|
+
declare const extractPDFText: typeof extractText$1;
|
|
56
|
+
declare const extractImages: typeof extractImages$1;
|
|
57
|
+
declare const renderPageAsImage: typeof renderPageAsImage$1;
|
|
48
58
|
|
|
49
|
-
export { defineUnPDFConfig, extractPDFText,
|
|
59
|
+
export { defineUnPDFConfig, extractImages, extractPDFText, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
let resolvedModule;
|
|
2
|
+
const isNode = globalThis.process?.release?.name === "node";
|
|
3
|
+
const isBrowser = typeof window !== "undefined";
|
|
2
4
|
async function getDocumentProxy(data, options = {}) {
|
|
3
5
|
const { getDocument } = await getResolvedPDFJS();
|
|
4
6
|
const pdf = await getDocument({
|
|
@@ -8,12 +10,6 @@ async function getDocumentProxy(data, options = {}) {
|
|
|
8
10
|
}).promise;
|
|
9
11
|
return pdf;
|
|
10
12
|
}
|
|
11
|
-
async function defineUnPDFConfig(options) {
|
|
12
|
-
const { pdfjs } = { ...options };
|
|
13
|
-
if (pdfjs) {
|
|
14
|
-
await resolvePDFJSImports(pdfjs, { force: true });
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
13
|
async function getResolvedPDFJS() {
|
|
18
14
|
if (!resolvedModule) {
|
|
19
15
|
await resolvePDFJSImports();
|
|
@@ -47,7 +43,7 @@ function isPDFDocumentProxy(data) {
|
|
|
47
43
|
return typeof data === "object" && data !== null && "_pdfInfo" in data;
|
|
48
44
|
}
|
|
49
45
|
|
|
50
|
-
async function
|
|
46
|
+
async function getMeta$1(data) {
|
|
51
47
|
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
52
48
|
const meta = await pdf.getMetadata();
|
|
53
49
|
return {
|
|
@@ -56,7 +52,7 @@ async function getPDFMeta$1(data) {
|
|
|
56
52
|
};
|
|
57
53
|
}
|
|
58
54
|
|
|
59
|
-
async function
|
|
55
|
+
async function extractText$1(data, options = {}) {
|
|
60
56
|
const { mergePages = false } = { ...options };
|
|
61
57
|
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
62
58
|
const texts = await Promise.all(
|
|
@@ -78,35 +74,129 @@ async function getPageText(pdf, pageNumber) {
|
|
|
78
74
|
return items.map(({ str }) => str).join("\n");
|
|
79
75
|
}
|
|
80
76
|
|
|
81
|
-
async function
|
|
77
|
+
async function extractImages$1(data, pageNumber) {
|
|
82
78
|
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
83
79
|
const page = await pdf.getPage(pageNumber);
|
|
80
|
+
if (pageNumber < 1 || pageNumber > pdf.numPages) {
|
|
81
|
+
throw new Error(
|
|
82
|
+
`Invalid page number. Must be between 1 and ${pdf.numPages}.`
|
|
83
|
+
);
|
|
84
|
+
}
|
|
84
85
|
const operatorList = await page.getOperatorList();
|
|
85
86
|
const { OPS } = await getResolvedPDFJS();
|
|
86
87
|
const images = [];
|
|
87
|
-
for (
|
|
88
|
+
for (let i = 0; i < operatorList.fnArray.length; i++) {
|
|
89
|
+
const op = operatorList.fnArray[i];
|
|
88
90
|
if (op !== OPS.paintImageXObject) {
|
|
89
91
|
continue;
|
|
90
92
|
}
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
93
|
+
const imageKey = operatorList.argsArray[i][0];
|
|
94
|
+
const image = await page.objs.get(imageKey);
|
|
95
|
+
images.push(image.data);
|
|
95
96
|
}
|
|
96
97
|
return images;
|
|
97
98
|
}
|
|
99
|
+
async function renderPageAsImage$1(data, pageNumber, options = {}) {
|
|
100
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
101
|
+
const page = await pdf.getPage(pageNumber);
|
|
102
|
+
if (pageNumber < 1 || pageNumber > pdf.numPages) {
|
|
103
|
+
throw new Error(
|
|
104
|
+
`Invalid page number. Must be between 1 and ${pdf.numPages}.`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
let outputScale = options.scale || 1;
|
|
108
|
+
let viewport = page.getViewport({ scale: outputScale });
|
|
109
|
+
if (options.width) {
|
|
110
|
+
outputScale = options.width / viewport.width;
|
|
111
|
+
} else if (options.height) {
|
|
112
|
+
outputScale = options.height / viewport.height;
|
|
113
|
+
}
|
|
114
|
+
if (outputScale !== 1 && outputScale > 0) {
|
|
115
|
+
viewport = page.getViewport({ scale: outputScale });
|
|
116
|
+
}
|
|
117
|
+
const canvasFactory = await createIsomorphicCanvasFactory();
|
|
118
|
+
const ctx = canvasFactory.create(viewport.width, viewport.height);
|
|
119
|
+
await page.render({
|
|
120
|
+
canvasContext: ctx.context,
|
|
121
|
+
viewport
|
|
122
|
+
}).promise;
|
|
123
|
+
const dataUrl = isBrowser ? ctx.canvas.toDataURL() : ctx.canvas.toDataURL();
|
|
124
|
+
const response = await fetch(dataUrl);
|
|
125
|
+
return await response.arrayBuffer();
|
|
126
|
+
}
|
|
127
|
+
async function createIsomorphicCanvasFactory() {
|
|
128
|
+
let _canvas;
|
|
129
|
+
if (isNode) {
|
|
130
|
+
try {
|
|
131
|
+
_canvas = await import('canvas');
|
|
132
|
+
} catch {
|
|
133
|
+
throw new Error(
|
|
134
|
+
'The "canvas" package is required when running in Node.js.'
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
_createCanvas(width, height) {
|
|
140
|
+
if (isBrowser) {
|
|
141
|
+
const canvas = document.createElement("canvas");
|
|
142
|
+
canvas.width = width;
|
|
143
|
+
canvas.height = height;
|
|
144
|
+
return canvas;
|
|
145
|
+
} else if (isNode) {
|
|
146
|
+
return _canvas.createCanvas(width, height);
|
|
147
|
+
}
|
|
148
|
+
throw new Error("Unsupported environment for canvas creation.");
|
|
149
|
+
},
|
|
150
|
+
create(width, height) {
|
|
151
|
+
const _canvas2 = this._createCanvas(width, height);
|
|
152
|
+
const context = _canvas2.getContext(
|
|
153
|
+
"2d"
|
|
154
|
+
);
|
|
155
|
+
return {
|
|
156
|
+
canvas: _canvas2,
|
|
157
|
+
context
|
|
158
|
+
};
|
|
159
|
+
},
|
|
160
|
+
reset(ctx, width, height) {
|
|
161
|
+
if (ctx.canvas) {
|
|
162
|
+
ctx.canvas.width = width;
|
|
163
|
+
ctx.canvas.height = height;
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
destroy(ctx) {
|
|
167
|
+
if (ctx.canvas) {
|
|
168
|
+
ctx.canvas.width = 0;
|
|
169
|
+
ctx.canvas.height = 0;
|
|
170
|
+
}
|
|
171
|
+
ctx.canvas = void 0;
|
|
172
|
+
ctx.context = void 0;
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
}
|
|
98
176
|
|
|
99
|
-
|
|
177
|
+
async function defineUnPDFConfig(options) {
|
|
178
|
+
const { pdfjs } = { ...options };
|
|
179
|
+
if (pdfjs) {
|
|
180
|
+
await resolvePDFJSImports(pdfjs, { force: true });
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const getMeta = async (...args) => {
|
|
185
|
+
await resolvePDFJSImports();
|
|
186
|
+
return await getMeta$1(...args);
|
|
187
|
+
};
|
|
188
|
+
const extractText = async (...args) => {
|
|
100
189
|
await resolvePDFJSImports();
|
|
101
|
-
return await
|
|
190
|
+
return await extractText$1(...args);
|
|
102
191
|
};
|
|
103
|
-
const extractPDFText =
|
|
192
|
+
const extractPDFText = extractText;
|
|
193
|
+
const extractImages = async (...args) => {
|
|
104
194
|
await resolvePDFJSImports();
|
|
105
|
-
return await
|
|
195
|
+
return await extractImages$1(...args);
|
|
106
196
|
};
|
|
107
|
-
const
|
|
197
|
+
const renderPageAsImage = async (...args) => {
|
|
108
198
|
await resolvePDFJSImports();
|
|
109
|
-
return await
|
|
199
|
+
return await renderPageAsImage$1(...args);
|
|
110
200
|
};
|
|
111
201
|
|
|
112
|
-
export { defineUnPDFConfig, extractPDFText,
|
|
202
|
+
export { defineUnPDFConfig, extractImages, extractPDFText, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
|