unpdf 0.5.4 → 0.6.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 +65 -31
- package/dist/index.cjs +120 -0
- package/dist/index.d.cts +49 -0
- package/dist/index.d.mts +49 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.mjs +112 -0
- package/dist/pdfjs.d.mts +1 -0
- package/dist/pdfjs.d.ts +1 -0
- package/dist/pdfjs.mjs +1 -0
- package/dist/types/external/webL10n/l10n.d.ts +0 -0
- package/dist/types/src/display/annotation_layer.d.ts +203 -0
- package/dist/types/src/display/annotation_storage.d.ts +89 -0
- package/dist/types/src/display/api.d.ts +1466 -0
- package/dist/types/src/display/base_factory.d.ts +54 -0
- package/dist/types/src/display/canvas.d.ts +208 -0
- package/dist/types/src/display/display_utils.d.ts +277 -0
- package/dist/types/src/display/editor/annotation_editor_layer.d.ts +196 -0
- package/dist/types/src/display/editor/editor.d.ts +325 -0
- package/dist/types/src/display/editor/freetext.d.ts +72 -0
- package/dist/types/src/display/editor/ink.d.ts +95 -0
- package/dist/types/src/display/editor/stamp.d.ts +28 -0
- package/dist/types/src/display/editor/tools.d.ts +349 -0
- package/dist/types/src/display/font_loader.d.ts +43 -0
- package/dist/types/src/display/metadata.d.ts +11 -0
- package/dist/types/src/display/optional_content_config.d.ts +13 -0
- package/dist/types/src/display/pattern_helper.d.ts +73 -0
- package/dist/types/src/display/text_layer.d.ts +151 -0
- package/dist/types/src/display/transport_stream.d.ts +68 -0
- package/dist/types/src/display/worker_options.d.ts +27 -0
- package/dist/types/src/display/xfa_layer.d.ts +46 -0
- package/dist/types/src/display/xfa_text.d.ts +19 -0
- package/dist/types/src/pdf.d.ts +50 -0
- package/dist/types/src/shared/image_utils.d.ts +17 -0
- package/dist/types/src/shared/message_handler.d.ts +43 -0
- package/dist/types/src/shared/murmurhash3.d.ts +7 -0
- package/dist/types/src/shared/scripting_utils.d.ts +16 -0
- package/dist/types/src/shared/util.d.ts +398 -0
- package/dist/types/web/annotation_editor_layer_builder.d.ts +50 -0
- package/dist/types/web/annotation_layer_builder.d.ts +83 -0
- package/dist/types/web/app_options.d.ts +14 -0
- package/dist/types/web/download_manager.d.ts +14 -0
- package/dist/types/web/event_utils.d.ts +75 -0
- package/dist/types/web/generic_scripting.d.ts +8 -0
- package/dist/types/web/genericl10n.d.ts +13 -0
- package/dist/types/web/interfaces.d.ts +163 -0
- package/dist/types/web/l10n_utils.d.ts +7 -0
- package/dist/types/web/pdf_find_controller.d.ts +108 -0
- package/dist/types/web/pdf_find_utils.d.ts +16 -0
- package/dist/types/web/pdf_history.d.ts +168 -0
- package/dist/types/web/pdf_link_service.d.ts +243 -0
- package/dist/types/web/pdf_page_view.d.ts +209 -0
- package/dist/types/web/pdf_rendering_queue.d.ts +53 -0
- package/dist/types/web/pdf_scripting_manager.component.d.ts +5 -0
- package/dist/types/web/pdf_scripting_manager.d.ts +61 -0
- package/dist/types/web/pdf_single_page_viewer.d.ts +7 -0
- package/dist/types/web/pdf_thumbnail_view.d.ts +131 -0
- package/dist/types/web/pdf_thumbnail_viewer.d.ts +104 -0
- package/dist/types/web/pdf_viewer.component.d.ts +24 -0
- package/dist/types/web/pdf_viewer.d.ts +455 -0
- package/dist/types/web/struct_tree_layer_builder.d.ts +7 -0
- package/dist/types/web/text_accessibility.d.ts +42 -0
- package/dist/types/web/text_highlighter.d.ts +63 -0
- package/dist/types/web/text_layer_builder.d.ts +64 -0
- package/dist/types/web/ui_utils.d.ts +288 -0
- package/dist/types/web/xfa_layer_builder.d.ts +41 -0
- package/package.json +54 -44
- package/dist/index.node.cjs +0 -25
- package/dist/index.node.d.cts +0 -10
- package/dist/index.node.d.mts +0 -10
- package/dist/index.node.d.ts +0 -10
- package/dist/index.node.mjs +0 -18
- package/dist/index.web.cjs +0 -25
- package/dist/index.web.d.cts +0 -10
- package/dist/index.web.d.mts +0 -10
- package/dist/index.web.d.ts +0 -10
- package/dist/index.web.mjs +0 -18
- package/dist/index.worker.cjs +0 -21
- package/dist/index.worker.d.cts +0 -10
- package/dist/index.worker.d.mts +0 -10
- package/dist/index.worker.d.ts +0 -10
- package/dist/index.worker.mjs +0 -13
- package/dist/shared/unpdf.3dcacb97.mjs +0 -49
- package/dist/shared/unpdf.42987ff1.cjs +0 -52
- package/dist/shared/unpdf.6363738c.d.cts +0 -43
- package/dist/shared/unpdf.6363738c.d.mts +0 -43
- package/dist/shared/unpdf.6363738c.d.ts +0 -43
- package/dist/shared/unpdf.ab6a2535.mjs +0 -46
- package/dist/shared/unpdf.eba9a3dc.cjs +0 -53
package/README.md
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
# unpdf
|
|
2
2
|
|
|
3
|
-
A collection of utilities to work with PDFs.
|
|
3
|
+
A collection of utilities to work with PDFs. Designed specifically for Deno, workers and other nodeless environments.
|
|
4
4
|
|
|
5
|
-
`unpdf`
|
|
5
|
+
`unpdf` ships with a serverless build/redistribution of Mozilla's [PDF.js](https://github.com/mozilla/pdf.js) for serverless environments. Apart from some string replacements and mocks, [`unenv`](https://github.com/unjs/unenv) does the heavy lifting by converting Node.js specific code to be platform-agnostic. See [`pdfjs.rollup.config.ts`](./pdfjs.rollup.config.ts) for all the details.
|
|
6
6
|
|
|
7
7
|
This library is also intended as a modern alternative to the unmaintained but still popular [`pdf-parse`](https://www.npmjs.com/package/pdf-parse).
|
|
8
8
|
|
|
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
13
|
- 💬 Extract text and images from PDFs
|
|
13
14
|
- 🧱 Opt-in to legacy PDF.js build
|
|
15
|
+
- 💨 Zero dependencies
|
|
14
16
|
|
|
15
17
|
## Installation
|
|
16
18
|
|
|
@@ -29,32 +31,37 @@ yarn add unpdf
|
|
|
29
31
|
|
|
30
32
|
## Usage
|
|
31
33
|
|
|
34
|
+
### Extract Text From PDF
|
|
35
|
+
|
|
32
36
|
```ts
|
|
33
|
-
import { extractPDFText } from
|
|
37
|
+
import { extractPDFText } from "unpdf";
|
|
34
38
|
|
|
35
39
|
// Fetch a PDF file from the web
|
|
36
|
-
const pdf = await fetch(
|
|
37
|
-
.
|
|
40
|
+
const pdf = await fetch(
|
|
41
|
+
"https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
|
|
42
|
+
).then((res) => res.arrayBuffer());
|
|
38
43
|
|
|
39
44
|
// Or load it from the filesystem
|
|
40
|
-
const pdf = await readFile(
|
|
45
|
+
const pdf = await readFile("./dummy.pdf");
|
|
41
46
|
|
|
42
47
|
// Pass the PDF buffer to the relevant method
|
|
43
|
-
const { totalPages, text } = await extractPDFText(
|
|
44
|
-
|
|
45
|
-
)
|
|
48
|
+
const { totalPages, text } = await extractPDFText(new Uint8Array(pdf), {
|
|
49
|
+
mergePages: true,
|
|
50
|
+
});
|
|
46
51
|
```
|
|
47
52
|
|
|
48
53
|
### Use Legacy Or Custom PDF.js Build
|
|
49
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 an older version or the legacy build, you can define a custom PDF.js module.
|
|
56
|
+
|
|
50
57
|
```ts
|
|
51
58
|
// Before using any other methods, define the PDF.js module
|
|
52
|
-
import { defineUnPDFConfig } from
|
|
59
|
+
import { defineUnPDFConfig } from "unpdf";
|
|
53
60
|
|
|
54
|
-
// Use the legacy build
|
|
55
61
|
defineUnPDFConfig({
|
|
56
|
-
|
|
57
|
-
|
|
62
|
+
// Use the legacy build
|
|
63
|
+
pdfjs: () => import("pdfjs-dist/legacy/build/pdf.js"),
|
|
64
|
+
});
|
|
58
65
|
|
|
59
66
|
// Now, you can use the other methods
|
|
60
67
|
// …
|
|
@@ -62,10 +69,32 @@ defineUnPDFConfig({
|
|
|
62
69
|
|
|
63
70
|
### Access the PDF.js Module
|
|
64
71
|
|
|
72
|
+
This will return the resolved PDF.js module. If no build is defined, the serverless build bundled with `unpdf` will be initialized.
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { getResolvedPDFJS } from "unpdf";
|
|
76
|
+
|
|
77
|
+
const { version } = await getResolvedPDFJS();
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Use Serverless PDF.js Build In 🦕 Deno
|
|
81
|
+
|
|
82
|
+
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.
|
|
83
|
+
|
|
65
84
|
```ts
|
|
66
|
-
import {
|
|
85
|
+
import { getDocument } from "https://esm.sh/unpdf/pdfjs";
|
|
86
|
+
|
|
87
|
+
const data = Deno.readFileSync("dummy.pdf");
|
|
88
|
+
const doc = await getDocument(data).promise;
|
|
67
89
|
|
|
68
|
-
|
|
90
|
+
console.log(await doc.getMetadata());
|
|
91
|
+
|
|
92
|
+
for (let i = 1; i <= doc.numPages; i++) {
|
|
93
|
+
const page = await doc.getPage(i);
|
|
94
|
+
const textContent = await page.getTextContent();
|
|
95
|
+
const contents = textContent.items.map((item) => item.str).join(" ");
|
|
96
|
+
console.log(contents);
|
|
97
|
+
}
|
|
69
98
|
```
|
|
70
99
|
|
|
71
100
|
## Config
|
|
@@ -78,9 +107,10 @@ interface UnPDFConfiguration {
|
|
|
78
107
|
* the PDF.js module.
|
|
79
108
|
*
|
|
80
109
|
* @example
|
|
110
|
+
* // Use the legacy build
|
|
81
111
|
* () => import('pdfjs-dist/legacy/build/pdf.js')
|
|
82
112
|
*/
|
|
83
|
-
pdfjs?: () => Promise<
|
|
113
|
+
pdfjs?: () => Promise<PDFJS>;
|
|
84
114
|
}
|
|
85
115
|
```
|
|
86
116
|
|
|
@@ -91,7 +121,7 @@ interface UnPDFConfiguration {
|
|
|
91
121
|
Define a custom PDF.js module, like the legacy build. Make sure to call this method before using any other methods.
|
|
92
122
|
|
|
93
123
|
```ts
|
|
94
|
-
function defineUnPDFConfig(config: UnPDFConfiguration): Promise<void
|
|
124
|
+
function defineUnPDFConfig(config: UnPDFConfiguration): Promise<void>;
|
|
95
125
|
```
|
|
96
126
|
|
|
97
127
|
### `getResolvedPDFJS`
|
|
@@ -99,18 +129,16 @@ function defineUnPDFConfig(config: UnPDFConfiguration): Promise<void>
|
|
|
99
129
|
Returns the resolved PDF.js module. If no build is defined, the latest version will be initialized.
|
|
100
130
|
|
|
101
131
|
```ts
|
|
102
|
-
function getResolvedPDFJS(): Promise<
|
|
132
|
+
function getResolvedPDFJS(): Promise<PDFJS>;
|
|
103
133
|
```
|
|
104
134
|
|
|
105
135
|
### `getPDFMeta`
|
|
106
136
|
|
|
107
137
|
```ts
|
|
108
|
-
function getPDFMeta(
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
metadata: Record<string, any>
|
|
113
|
-
}>
|
|
138
|
+
function getPDFMeta(data: BinaryData | PDFDocumentProxy): Promise<{
|
|
139
|
+
info: Record<string, any>;
|
|
140
|
+
metadata: Record<string, any>;
|
|
141
|
+
}>;
|
|
114
142
|
```
|
|
115
143
|
|
|
116
144
|
### `extractPDFText`
|
|
@@ -118,11 +146,11 @@ function getPDFMeta(
|
|
|
118
146
|
```ts
|
|
119
147
|
function extractPDFText(
|
|
120
148
|
data: BinaryData | PDFDocumentProxy,
|
|
121
|
-
{ mergePages }?: { mergePages?: boolean }
|
|
149
|
+
{ mergePages }?: { mergePages?: boolean },
|
|
122
150
|
): Promise<{
|
|
123
|
-
totalPages: number
|
|
124
|
-
text: string | string[]
|
|
125
|
-
}
|
|
151
|
+
totalPages: number;
|
|
152
|
+
text: string | string[];
|
|
153
|
+
}>;
|
|
126
154
|
```
|
|
127
155
|
|
|
128
156
|
### `getImagesFromPage`
|
|
@@ -130,10 +158,16 @@ function extractPDFText(
|
|
|
130
158
|
```ts
|
|
131
159
|
function getImagesFromPage(
|
|
132
160
|
data: BinaryData | PDFDocumentProxy,
|
|
133
|
-
pageNumber: number
|
|
134
|
-
): Promise<ArrayBuffer[]
|
|
161
|
+
pageNumber: number,
|
|
162
|
+
): Promise<ArrayBuffer[]>;
|
|
135
163
|
```
|
|
136
164
|
|
|
165
|
+
## FAQ
|
|
166
|
+
|
|
167
|
+
### What About Canvas?
|
|
168
|
+
|
|
169
|
+
The official PDF.js library depends on the optional `canvas` module, which [doesn't work inside worker threads](https://github.com/Automattic/node-canvas/issues/1394). That's why `unpdf` ships with a serverless build of PDF.js that mocks the `canvas` module.
|
|
170
|
+
|
|
137
171
|
## License
|
|
138
172
|
|
|
139
173
|
[MIT](./LICENSE) License © 2023-PRESENT [Johann Schopplich](https://github.com/johannschopplich)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
let resolvedModule;
|
|
4
|
+
async function getDocumentProxy(data, options = {}) {
|
|
5
|
+
const { getDocument } = await getResolvedPDFJS();
|
|
6
|
+
const pdf = await getDocument({
|
|
7
|
+
data,
|
|
8
|
+
isEvalSupported: false,
|
|
9
|
+
...options
|
|
10
|
+
}).promise;
|
|
11
|
+
return pdf;
|
|
12
|
+
}
|
|
13
|
+
async function defineUnPDFConfig(options) {
|
|
14
|
+
const { pdfjs } = { ...options };
|
|
15
|
+
if (pdfjs) {
|
|
16
|
+
await resolvePDFJSImports(pdfjs, { force: true });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
async function getResolvedPDFJS() {
|
|
20
|
+
if (!resolvedModule) {
|
|
21
|
+
await resolvePDFJSImports();
|
|
22
|
+
}
|
|
23
|
+
return resolvedModule;
|
|
24
|
+
}
|
|
25
|
+
async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
26
|
+
if (resolvedModule && !force) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (pdfjsResolver) {
|
|
30
|
+
try {
|
|
31
|
+
const _import = await pdfjsResolver();
|
|
32
|
+
resolvedModule = _import.default || _import;
|
|
33
|
+
return;
|
|
34
|
+
} catch {
|
|
35
|
+
throw new Error(
|
|
36
|
+
"Resolving failed. Please check the provided configuration."
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
resolvedModule = await import('unpdf/pdfjs');
|
|
42
|
+
} catch {
|
|
43
|
+
throw new Error(
|
|
44
|
+
"PDF.js is not available. Please add the package as a dependency."
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function isPDFDocumentProxy(data) {
|
|
49
|
+
return typeof data === "object" && data !== null && "_pdfInfo" in data;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function getPDFMeta$1(data) {
|
|
53
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
54
|
+
const meta = await pdf.getMetadata();
|
|
55
|
+
return {
|
|
56
|
+
info: meta?.info ?? {},
|
|
57
|
+
metadata: meta?.metadata?.getAll() ?? {}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function extractPDFText$1(data, options = {}) {
|
|
62
|
+
const { mergePages = false } = { ...options };
|
|
63
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
64
|
+
const texts = await Promise.all(
|
|
65
|
+
Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
|
|
66
|
+
);
|
|
67
|
+
const filteredTexts = texts.filter(Boolean);
|
|
68
|
+
return {
|
|
69
|
+
totalPages: pdf.numPages,
|
|
70
|
+
text: mergePages ? filteredTexts.join("\n\n") : filteredTexts
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
async function getPageText(pdf, pageNumber) {
|
|
74
|
+
const page = await pdf.getPage(pageNumber);
|
|
75
|
+
const content = await page.getTextContent();
|
|
76
|
+
const items = content.items;
|
|
77
|
+
if (items.length === 0) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
return items.map(({ str }) => str).join("\n");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function getImagesFromPage$1(data, pageNumber) {
|
|
84
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
85
|
+
const page = await pdf.getPage(pageNumber);
|
|
86
|
+
const operatorList = await page.getOperatorList();
|
|
87
|
+
const { OPS } = await getResolvedPDFJS();
|
|
88
|
+
const images = [];
|
|
89
|
+
for (const op of operatorList.fnArray) {
|
|
90
|
+
if (op !== OPS.paintImageXObject) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const image = await page.objs.get(operatorList.argsArray[op][0]);
|
|
94
|
+
if (image.data) {
|
|
95
|
+
images.push(image.data.buffer);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return images;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const getPDFMeta = async (...args) => {
|
|
102
|
+
await resolvePDFJSImports();
|
|
103
|
+
return await getPDFMeta$1(...args);
|
|
104
|
+
};
|
|
105
|
+
const extractPDFText = async (...args) => {
|
|
106
|
+
await resolvePDFJSImports();
|
|
107
|
+
return await extractPDFText$1(...args);
|
|
108
|
+
};
|
|
109
|
+
const getImagesFromPage = async (...args) => {
|
|
110
|
+
await resolvePDFJSImports();
|
|
111
|
+
return await getImagesFromPage$1(...args);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
exports.defineUnPDFConfig = defineUnPDFConfig;
|
|
115
|
+
exports.extractPDFText = extractPDFText;
|
|
116
|
+
exports.getDocumentProxy = getDocumentProxy;
|
|
117
|
+
exports.getImagesFromPage = getImagesFromPage;
|
|
118
|
+
exports.getPDFMeta = getPDFMeta;
|
|
119
|
+
exports.getResolvedPDFJS = getResolvedPDFJS;
|
|
120
|
+
exports.resolvePDFJSImports = resolvePDFJSImports;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { BinaryData, PDFDocumentProxy, DocumentInitParameters } from './types/src/display/api';
|
|
2
|
+
import * as _PDFJS from './types/src/pdf';
|
|
3
|
+
|
|
4
|
+
declare function getPDFMeta$1(data: BinaryData | PDFDocumentProxy): Promise<{
|
|
5
|
+
info: Record<string, any>;
|
|
6
|
+
metadata: Record<string, any>;
|
|
7
|
+
}>;
|
|
8
|
+
|
|
9
|
+
declare function extractPDFText$1(data: BinaryData | PDFDocumentProxy, options?: {
|
|
10
|
+
mergePages?: boolean;
|
|
11
|
+
}): Promise<{
|
|
12
|
+
totalPages: number;
|
|
13
|
+
text: string | string[];
|
|
14
|
+
}>;
|
|
15
|
+
|
|
16
|
+
declare function getImagesFromPage$1(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<ArrayBuffer[]>;
|
|
17
|
+
|
|
18
|
+
type PDFJS = typeof _PDFJS;
|
|
19
|
+
interface UnPDFConfiguration {
|
|
20
|
+
/**
|
|
21
|
+
* By default, UnPDF will use the latest version of PDF.js. If you want to
|
|
22
|
+
* use an older version or the legacy build, set a promise that resolves to
|
|
23
|
+
* the PDF.js module.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* // Use the legacy build
|
|
27
|
+
* () => import('pdfjs-dist/legacy/build/pdf.js')
|
|
28
|
+
*/
|
|
29
|
+
pdfjs?: () => Promise<PDFJS>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Returns a PDFDocumentProxy instance from a given binary data.
|
|
34
|
+
*
|
|
35
|
+
* Applies the following defaults:
|
|
36
|
+
* - `isEvalSupported: false`
|
|
37
|
+
*/
|
|
38
|
+
declare function getDocumentProxy(data: BinaryData, options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
|
|
39
|
+
declare function defineUnPDFConfig(options: UnPDFConfiguration): Promise<void>;
|
|
40
|
+
declare function getResolvedPDFJS(): Promise<PDFJS>;
|
|
41
|
+
declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<PDFJS>, { force }?: {
|
|
42
|
+
force?: boolean | undefined;
|
|
43
|
+
}): Promise<void>;
|
|
44
|
+
|
|
45
|
+
declare const getPDFMeta: typeof getPDFMeta$1;
|
|
46
|
+
declare const extractPDFText: typeof extractPDFText$1;
|
|
47
|
+
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
|
48
|
+
|
|
49
|
+
export { defineUnPDFConfig, extractPDFText, getDocumentProxy, getImagesFromPage, getPDFMeta, getResolvedPDFJS, resolvePDFJSImports };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { BinaryData, PDFDocumentProxy, DocumentInitParameters } from './types/src/display/api';
|
|
2
|
+
import * as _PDFJS from './types/src/pdf';
|
|
3
|
+
|
|
4
|
+
declare function getPDFMeta$1(data: BinaryData | PDFDocumentProxy): Promise<{
|
|
5
|
+
info: Record<string, any>;
|
|
6
|
+
metadata: Record<string, any>;
|
|
7
|
+
}>;
|
|
8
|
+
|
|
9
|
+
declare function extractPDFText$1(data: BinaryData | PDFDocumentProxy, options?: {
|
|
10
|
+
mergePages?: boolean;
|
|
11
|
+
}): Promise<{
|
|
12
|
+
totalPages: number;
|
|
13
|
+
text: string | string[];
|
|
14
|
+
}>;
|
|
15
|
+
|
|
16
|
+
declare function getImagesFromPage$1(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<ArrayBuffer[]>;
|
|
17
|
+
|
|
18
|
+
type PDFJS = typeof _PDFJS;
|
|
19
|
+
interface UnPDFConfiguration {
|
|
20
|
+
/**
|
|
21
|
+
* By default, UnPDF will use the latest version of PDF.js. If you want to
|
|
22
|
+
* use an older version or the legacy build, set a promise that resolves to
|
|
23
|
+
* the PDF.js module.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* // Use the legacy build
|
|
27
|
+
* () => import('pdfjs-dist/legacy/build/pdf.js')
|
|
28
|
+
*/
|
|
29
|
+
pdfjs?: () => Promise<PDFJS>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Returns a PDFDocumentProxy instance from a given binary data.
|
|
34
|
+
*
|
|
35
|
+
* Applies the following defaults:
|
|
36
|
+
* - `isEvalSupported: false`
|
|
37
|
+
*/
|
|
38
|
+
declare function getDocumentProxy(data: BinaryData, options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
|
|
39
|
+
declare function defineUnPDFConfig(options: UnPDFConfiguration): Promise<void>;
|
|
40
|
+
declare function getResolvedPDFJS(): Promise<PDFJS>;
|
|
41
|
+
declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<PDFJS>, { force }?: {
|
|
42
|
+
force?: boolean | undefined;
|
|
43
|
+
}): Promise<void>;
|
|
44
|
+
|
|
45
|
+
declare const getPDFMeta: typeof getPDFMeta$1;
|
|
46
|
+
declare const extractPDFText: typeof extractPDFText$1;
|
|
47
|
+
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
|
48
|
+
|
|
49
|
+
export { defineUnPDFConfig, extractPDFText, getDocumentProxy, getImagesFromPage, getPDFMeta, getResolvedPDFJS, resolvePDFJSImports };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { BinaryData, PDFDocumentProxy, DocumentInitParameters } from './types/src/display/api';
|
|
2
|
+
import * as _PDFJS from './types/src/pdf';
|
|
3
|
+
|
|
4
|
+
declare function getPDFMeta$1(data: BinaryData | PDFDocumentProxy): Promise<{
|
|
5
|
+
info: Record<string, any>;
|
|
6
|
+
metadata: Record<string, any>;
|
|
7
|
+
}>;
|
|
8
|
+
|
|
9
|
+
declare function extractPDFText$1(data: BinaryData | PDFDocumentProxy, options?: {
|
|
10
|
+
mergePages?: boolean;
|
|
11
|
+
}): Promise<{
|
|
12
|
+
totalPages: number;
|
|
13
|
+
text: string | string[];
|
|
14
|
+
}>;
|
|
15
|
+
|
|
16
|
+
declare function getImagesFromPage$1(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<ArrayBuffer[]>;
|
|
17
|
+
|
|
18
|
+
type PDFJS = typeof _PDFJS;
|
|
19
|
+
interface UnPDFConfiguration {
|
|
20
|
+
/**
|
|
21
|
+
* By default, UnPDF will use the latest version of PDF.js. If you want to
|
|
22
|
+
* use an older version or the legacy build, set a promise that resolves to
|
|
23
|
+
* the PDF.js module.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* // Use the legacy build
|
|
27
|
+
* () => import('pdfjs-dist/legacy/build/pdf.js')
|
|
28
|
+
*/
|
|
29
|
+
pdfjs?: () => Promise<PDFJS>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Returns a PDFDocumentProxy instance from a given binary data.
|
|
34
|
+
*
|
|
35
|
+
* Applies the following defaults:
|
|
36
|
+
* - `isEvalSupported: false`
|
|
37
|
+
*/
|
|
38
|
+
declare function getDocumentProxy(data: BinaryData, options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
|
|
39
|
+
declare function defineUnPDFConfig(options: UnPDFConfiguration): Promise<void>;
|
|
40
|
+
declare function getResolvedPDFJS(): Promise<PDFJS>;
|
|
41
|
+
declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<PDFJS>, { force }?: {
|
|
42
|
+
force?: boolean | undefined;
|
|
43
|
+
}): Promise<void>;
|
|
44
|
+
|
|
45
|
+
declare const getPDFMeta: typeof getPDFMeta$1;
|
|
46
|
+
declare const extractPDFText: typeof extractPDFText$1;
|
|
47
|
+
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
|
48
|
+
|
|
49
|
+
export { defineUnPDFConfig, extractPDFText, getDocumentProxy, getImagesFromPage, getPDFMeta, getResolvedPDFJS, resolvePDFJSImports };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
let resolvedModule;
|
|
2
|
+
async function getDocumentProxy(data, options = {}) {
|
|
3
|
+
const { getDocument } = await getResolvedPDFJS();
|
|
4
|
+
const pdf = await getDocument({
|
|
5
|
+
data,
|
|
6
|
+
isEvalSupported: false,
|
|
7
|
+
...options
|
|
8
|
+
}).promise;
|
|
9
|
+
return pdf;
|
|
10
|
+
}
|
|
11
|
+
async function defineUnPDFConfig(options) {
|
|
12
|
+
const { pdfjs } = { ...options };
|
|
13
|
+
if (pdfjs) {
|
|
14
|
+
await resolvePDFJSImports(pdfjs, { force: true });
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
async function getResolvedPDFJS() {
|
|
18
|
+
if (!resolvedModule) {
|
|
19
|
+
await resolvePDFJSImports();
|
|
20
|
+
}
|
|
21
|
+
return resolvedModule;
|
|
22
|
+
}
|
|
23
|
+
async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
24
|
+
if (resolvedModule && !force) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (pdfjsResolver) {
|
|
28
|
+
try {
|
|
29
|
+
const _import = await pdfjsResolver();
|
|
30
|
+
resolvedModule = _import.default || _import;
|
|
31
|
+
return;
|
|
32
|
+
} catch {
|
|
33
|
+
throw new Error(
|
|
34
|
+
"Resolving failed. Please check the provided configuration."
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
resolvedModule = await import('unpdf/pdfjs');
|
|
40
|
+
} catch {
|
|
41
|
+
throw new Error(
|
|
42
|
+
"PDF.js is not available. Please add the package as a dependency."
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function isPDFDocumentProxy(data) {
|
|
47
|
+
return typeof data === "object" && data !== null && "_pdfInfo" in data;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function getPDFMeta$1(data) {
|
|
51
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
52
|
+
const meta = await pdf.getMetadata();
|
|
53
|
+
return {
|
|
54
|
+
info: meta?.info ?? {},
|
|
55
|
+
metadata: meta?.metadata?.getAll() ?? {}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function extractPDFText$1(data, options = {}) {
|
|
60
|
+
const { mergePages = false } = { ...options };
|
|
61
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
62
|
+
const texts = await Promise.all(
|
|
63
|
+
Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
|
|
64
|
+
);
|
|
65
|
+
const filteredTexts = texts.filter(Boolean);
|
|
66
|
+
return {
|
|
67
|
+
totalPages: pdf.numPages,
|
|
68
|
+
text: mergePages ? filteredTexts.join("\n\n") : filteredTexts
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
async function getPageText(pdf, pageNumber) {
|
|
72
|
+
const page = await pdf.getPage(pageNumber);
|
|
73
|
+
const content = await page.getTextContent();
|
|
74
|
+
const items = content.items;
|
|
75
|
+
if (items.length === 0) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
return items.map(({ str }) => str).join("\n");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function getImagesFromPage$1(data, pageNumber) {
|
|
82
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
83
|
+
const page = await pdf.getPage(pageNumber);
|
|
84
|
+
const operatorList = await page.getOperatorList();
|
|
85
|
+
const { OPS } = await getResolvedPDFJS();
|
|
86
|
+
const images = [];
|
|
87
|
+
for (const op of operatorList.fnArray) {
|
|
88
|
+
if (op !== OPS.paintImageXObject) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const image = await page.objs.get(operatorList.argsArray[op][0]);
|
|
92
|
+
if (image.data) {
|
|
93
|
+
images.push(image.data.buffer);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return images;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const getPDFMeta = async (...args) => {
|
|
100
|
+
await resolvePDFJSImports();
|
|
101
|
+
return await getPDFMeta$1(...args);
|
|
102
|
+
};
|
|
103
|
+
const extractPDFText = async (...args) => {
|
|
104
|
+
await resolvePDFJSImports();
|
|
105
|
+
return await extractPDFText$1(...args);
|
|
106
|
+
};
|
|
107
|
+
const getImagesFromPage = async (...args) => {
|
|
108
|
+
await resolvePDFJSImports();
|
|
109
|
+
return await getImagesFromPage$1(...args);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export { defineUnPDFConfig, extractPDFText, getDocumentProxy, getImagesFromPage, getPDFMeta, getResolvedPDFJS, resolvePDFJSImports };
|
package/dist/pdfjs.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './types/src/pdf.d.ts'
|
package/dist/pdfjs.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './types/src/pdf.d.ts'
|