unpdf 0.10.1 → 0.12.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 +12 -8
- package/dist/index.cjs +15 -15
- package/dist/index.d.cts +32 -13
- package/dist/index.d.mts +32 -13
- package/dist/index.d.ts +32 -13
- package/dist/index.mjs +15 -15
- package/dist/pdfjs.d.mts +1 -0
- package/dist/pdfjs.d.ts +1 -0
- package/dist/pdfjs.mjs +1 -1
- package/dist/types/src/display/annotation_layer.d.ts +12 -5
- package/dist/types/src/display/annotation_storage.d.ts +10 -0
- package/dist/types/src/display/api.d.ts +95 -45
- package/dist/types/src/display/base_factory.d.ts +7 -1
- package/dist/types/src/display/canvas.d.ts +4 -0
- package/dist/types/src/display/display_utils.d.ts +6 -4
- package/dist/types/src/display/draw_layer.d.ts +12 -14
- package/dist/types/src/display/editor/alt_text.d.ts +33 -0
- package/dist/types/src/display/editor/annotation_editor_layer.d.ts +26 -2
- package/dist/types/src/display/editor/color_picker.d.ts +19 -0
- package/dist/types/src/display/editor/editor.d.ts +85 -16
- package/dist/types/src/display/editor/freetext.d.ts +6 -1
- package/dist/types/src/display/editor/highlight.d.ts +88 -0
- package/dist/types/src/display/editor/ink.d.ts +5 -4
- package/dist/types/src/display/editor/outliner.d.ts +53 -9
- package/dist/types/src/display/editor/stamp.d.ts +22 -1
- package/dist/types/src/display/editor/toolbar.d.ts +10 -1
- package/dist/types/src/display/editor/tools.d.ts +56 -6
- package/dist/types/src/display/fetch_stream.d.ts +3 -4
- package/dist/types/src/display/font_loader.d.ts +1 -5
- package/dist/types/src/display/network.d.ts +2 -3
- package/dist/types/src/display/node_stream.d.ts +5 -6
- package/dist/types/src/display/node_utils.d.ts +6 -5
- package/dist/types/src/display/optional_content_config.d.ts +6 -1
- package/dist/types/src/display/text_layer.d.ts +37 -106
- package/dist/types/src/display/transport_stream.d.ts +3 -7
- package/dist/types/src/display/worker_options.d.ts +20 -22
- package/dist/types/src/pdf.d.ts +3 -6
- package/dist/types/src/shared/util.d.ts +24 -22
- package/dist/types/web/annotation_editor_layer_builder.d.ts +10 -13
- package/dist/types/web/annotation_layer_builder.d.ts +8 -4
- package/dist/types/web/app_options.d.ts +5 -4
- package/dist/types/web/download_manager.d.ts +1 -2
- package/dist/types/web/draw_layer_builder.d.ts +23 -0
- package/dist/types/web/event_utils.d.ts +8 -6
- package/dist/types/web/genericl10n.d.ts +5 -3
- package/dist/types/web/interfaces.d.ts +10 -14
- package/dist/types/web/l10n.d.ts +7 -2
- package/dist/types/web/pdf_find_controller.d.ts +25 -2
- package/dist/types/web/pdf_history.d.ts +1 -53
- package/dist/types/web/pdf_link_service.d.ts +4 -107
- package/dist/types/web/pdf_page_view.d.ts +16 -21
- package/dist/types/web/pdf_scripting_manager.d.ts +6 -10
- package/dist/types/web/pdf_thumbnail_view.d.ts +11 -15
- package/dist/types/web/pdf_thumbnail_viewer.d.ts +16 -17
- package/dist/types/web/pdf_viewer.component.d.ts +1 -2
- package/dist/types/web/pdf_viewer.d.ts +67 -35
- package/dist/types/web/text_accessibility.d.ts +1 -1
- package/dist/types/web/text_highlighter.d.ts +1 -1
- package/dist/types/web/text_layer_builder.d.ts +17 -29
- package/dist/types/web/ui_utils.d.ts +7 -2
- package/package.json +24 -23
- package/dist/types/web/l10n_utils.d.ts +0 -9
package/README.md
CHANGED
|
@@ -16,8 +16,7 @@ This library is also intended as a modern alternative to the unmaintained but st
|
|
|
16
16
|
|
|
17
17
|
## PDF.js Compatibility
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
> This package is currently using PDF.js v4.0.189.
|
|
19
|
+
The serverless build of PDF.js provided by `unpdf` is based on PDF.js v4.6.82. If you need a different version, you can [use another PDF.js build](#use-official-or-legacy-pdfjs-build).
|
|
21
20
|
|
|
22
21
|
## Installation
|
|
23
22
|
|
|
@@ -50,7 +49,7 @@ const buffer = await fetch(
|
|
|
50
49
|
const buffer = await readFile("./dummy.pdf");
|
|
51
50
|
|
|
52
51
|
// Load PDF from buffer
|
|
53
|
-
const pdf = await getDocumentProxy(new Uint8Array(
|
|
52
|
+
const pdf = await getDocumentProxy(new Uint8Array(buffer));
|
|
54
53
|
// Extract text from PDF
|
|
55
54
|
const { totalPages, text } = await extractText(pdf, { mergePages: true });
|
|
56
55
|
```
|
|
@@ -86,12 +85,15 @@ for (let i = 1; i <= doc.numPages; i++) {
|
|
|
86
85
|
|
|
87
86
|
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
87
|
|
|
88
|
+
> [!WARNING]
|
|
89
|
+
> The latest PDF.js v4.6.82 uses `Promise.withResolvers`, which may not be supported in all environments, such as Node < 22. Consider to use the bundled serverless build, which includes a polyfill, or use an older version of PDF.js.
|
|
90
|
+
|
|
89
91
|
```ts
|
|
90
92
|
// Before using any other method, define the PDF.js module
|
|
91
93
|
// if you need another PDF.js build
|
|
92
94
|
import { configureUnPDF } from "unpdf";
|
|
93
95
|
|
|
94
|
-
configureUnPDF({
|
|
96
|
+
await configureUnPDF({
|
|
95
97
|
// Use the official PDF.js build (make sure to install it first)
|
|
96
98
|
pdfjs: () => import("pdfjs-dist"),
|
|
97
99
|
});
|
|
@@ -138,7 +140,9 @@ function getResolvedPDFJS(): Promise<PDFJS>;
|
|
|
138
140
|
### `getMeta`
|
|
139
141
|
|
|
140
142
|
```ts
|
|
141
|
-
function getMeta(
|
|
143
|
+
function getMeta(
|
|
144
|
+
data: DocumentInitParameters["data"] | PDFDocumentProxy,
|
|
145
|
+
): Promise<{
|
|
142
146
|
info: Record<string, any>;
|
|
143
147
|
metadata: Record<string, any>;
|
|
144
148
|
}>;
|
|
@@ -150,7 +154,7 @@ Extracts all text from a PDF. If `mergePages` is set to `true`, the text of all
|
|
|
150
154
|
|
|
151
155
|
```ts
|
|
152
156
|
function extractText(
|
|
153
|
-
data:
|
|
157
|
+
data: DocumentInitParameters["data"] | PDFDocumentProxy,
|
|
154
158
|
{ mergePages }?: { mergePages?: boolean },
|
|
155
159
|
): Promise<{
|
|
156
160
|
totalPages: number;
|
|
@@ -175,7 +179,7 @@ In order to use this method, you have to meet the following requirements:
|
|
|
175
179
|
```ts
|
|
176
180
|
import { configureUnPDF, renderPageAsImage } from "unpdf";
|
|
177
181
|
|
|
178
|
-
configureUnPDF({
|
|
182
|
+
await configureUnPDF({
|
|
179
183
|
// Use the official PDF.js build
|
|
180
184
|
pdfjs: () => import("pdfjs-dist"),
|
|
181
185
|
});
|
|
@@ -194,7 +198,7 @@ await writeFile("dummy-page-1.png", Buffer.from(result));
|
|
|
194
198
|
|
|
195
199
|
```ts
|
|
196
200
|
declare function renderPageAsImage(
|
|
197
|
-
data:
|
|
201
|
+
data: DocumentInitParameters["data"],
|
|
198
202
|
pageNumber: number,
|
|
199
203
|
options?: {
|
|
200
204
|
canvas?: () => Promise<typeof import("canvas")>;
|
package/dist/index.cjs
CHANGED
|
@@ -31,16 +31,18 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
|
31
31
|
resolvedModule = await resolvedModule.resolvePDFJS();
|
|
32
32
|
}
|
|
33
33
|
return;
|
|
34
|
-
} catch {
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error(error);
|
|
35
36
|
throw new Error(
|
|
36
37
|
"Resolving failed. Please check the provided configuration."
|
|
37
38
|
);
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
41
|
try {
|
|
41
|
-
const { resolvePDFJS } = await
|
|
42
|
+
const { resolvePDFJS } = await Promise.resolve().then(function () { return pdfjs; });
|
|
42
43
|
resolvedModule = await resolvePDFJS();
|
|
43
|
-
} catch {
|
|
44
|
+
} catch (error) {
|
|
45
|
+
console.error(error);
|
|
44
46
|
throw new Error(
|
|
45
47
|
"PDF.js is not available. Please add the package as a dependency."
|
|
46
48
|
);
|
|
@@ -69,20 +71,15 @@ async function extractText$1(data, options = {}) {
|
|
|
69
71
|
const texts = await Promise.all(
|
|
70
72
|
Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
|
|
71
73
|
);
|
|
72
|
-
const filteredTexts = texts.filter(Boolean);
|
|
73
74
|
return {
|
|
74
75
|
totalPages: pdf.numPages,
|
|
75
|
-
text: mergePages ?
|
|
76
|
+
text: mergePages ? texts.join("\n").replace(/\s+/g, " ") : texts
|
|
76
77
|
};
|
|
77
78
|
}
|
|
78
|
-
async function getPageText(
|
|
79
|
-
const page = await
|
|
79
|
+
async function getPageText(document, pageNumber) {
|
|
80
|
+
const page = await document.getPage(pageNumber);
|
|
80
81
|
const content = await page.getTextContent();
|
|
81
|
-
|
|
82
|
-
if (items.length === 0) {
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
return items.map(({ str }) => str).join("\n");
|
|
82
|
+
return content.items.filter((item) => item.str != null).map((item) => item.str + (item.hasEOL ? "\n" : "")).join("");
|
|
86
83
|
}
|
|
87
84
|
|
|
88
85
|
async function extractImages$1(data, pageNumber) {
|
|
@@ -109,7 +106,7 @@ async function extractImages$1(data, pageNumber) {
|
|
|
109
106
|
}
|
|
110
107
|
async function renderPageAsImage$1(data, pageNumber, options = {}) {
|
|
111
108
|
const canvasFactory = await createIsomorphicCanvasFactory(options.canvas);
|
|
112
|
-
const pdf = await getDocumentProxy(data, { canvasFactory });
|
|
109
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data, { canvasFactory });
|
|
113
110
|
const page = await pdf.getPage(pageNumber);
|
|
114
111
|
if (pageNumber < 1 || pageNumber > pdf.numPages) {
|
|
115
112
|
throw new Error(
|
|
@@ -187,7 +184,6 @@ async function configureUnPDF(options) {
|
|
|
187
184
|
}
|
|
188
185
|
}
|
|
189
186
|
|
|
190
|
-
const defineUnPDFConfig = configureUnPDF;
|
|
191
187
|
const getMeta = async (...args) => {
|
|
192
188
|
await resolvePDFJSImports();
|
|
193
189
|
return await getMeta$1(...args);
|
|
@@ -205,8 +201,12 @@ const renderPageAsImage = async (...args) => {
|
|
|
205
201
|
return await renderPageAsImage$1(...args);
|
|
206
202
|
};
|
|
207
203
|
|
|
204
|
+
const pdfjs = {
|
|
205
|
+
__proto__: null
|
|
206
|
+
};
|
|
207
|
+
|
|
208
208
|
exports.configureUnPDF = configureUnPDF;
|
|
209
|
-
exports.
|
|
209
|
+
exports.createIsomorphicCanvasFactory = createIsomorphicCanvasFactory;
|
|
210
210
|
exports.extractImages = extractImages;
|
|
211
211
|
exports.extractText = extractText;
|
|
212
212
|
exports.getDocumentProxy = getDocumentProxy;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,27 +1,49 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DocumentInitParameters, PDFDocumentProxy } from './types/src/display/api';
|
|
2
2
|
import * as canvas from 'canvas';
|
|
3
|
+
import { Canvas } from 'canvas';
|
|
3
4
|
import * as _PDFJS from './types/src/pdf';
|
|
4
5
|
|
|
5
|
-
declare function getMeta$1(data:
|
|
6
|
+
declare function getMeta$1(data: DocumentInitParameters["data"] | PDFDocumentProxy): Promise<{
|
|
6
7
|
info: Record<string, any>;
|
|
7
8
|
metadata: Record<string, any>;
|
|
8
9
|
}>;
|
|
9
10
|
|
|
10
|
-
declare function extractText$1(data:
|
|
11
|
-
mergePages?:
|
|
11
|
+
declare function extractText$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, options?: {
|
|
12
|
+
mergePages?: false;
|
|
12
13
|
}): Promise<{
|
|
13
14
|
totalPages: number;
|
|
14
|
-
text: string
|
|
15
|
+
text: string[];
|
|
16
|
+
}>;
|
|
17
|
+
declare function extractText$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, options: {
|
|
18
|
+
mergePages: true;
|
|
19
|
+
}): Promise<{
|
|
20
|
+
totalPages: number;
|
|
21
|
+
text: string;
|
|
15
22
|
}>;
|
|
16
23
|
|
|
17
|
-
declare function extractImages$1(data:
|
|
18
|
-
declare function renderPageAsImage$1(data:
|
|
24
|
+
declare function extractImages$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
|
|
25
|
+
declare function renderPageAsImage$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number, options?: {
|
|
19
26
|
canvas?: () => Promise<typeof canvas>;
|
|
20
27
|
/** @default 1 */
|
|
21
28
|
scale?: number;
|
|
22
29
|
width?: number;
|
|
23
30
|
height?: number;
|
|
24
31
|
}): Promise<ArrayBuffer>;
|
|
32
|
+
declare function createIsomorphicCanvasFactory(canvas?: () => Promise<typeof canvas>): Promise<{
|
|
33
|
+
_createCanvas(width: number, height: number): Canvas | HTMLCanvasElement;
|
|
34
|
+
create(width: number, height: number): {
|
|
35
|
+
canvas: Canvas | HTMLCanvasElement;
|
|
36
|
+
context: CanvasRenderingContext2D;
|
|
37
|
+
};
|
|
38
|
+
reset(ctx: {
|
|
39
|
+
canvas?: Canvas;
|
|
40
|
+
context?: CanvasRenderingContext2D;
|
|
41
|
+
}, width: number, height: number): void;
|
|
42
|
+
destroy(ctx: {
|
|
43
|
+
canvas?: Canvas;
|
|
44
|
+
context?: CanvasRenderingContext2D;
|
|
45
|
+
}): void;
|
|
46
|
+
}>;
|
|
25
47
|
|
|
26
48
|
type PDFJS = typeof _PDFJS;
|
|
27
49
|
interface UnPDFConfiguration {
|
|
@@ -37,8 +59,6 @@ interface UnPDFConfiguration {
|
|
|
37
59
|
pdfjs?: () => Promise<any>;
|
|
38
60
|
}
|
|
39
61
|
|
|
40
|
-
declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
|
|
41
|
-
|
|
42
62
|
/**
|
|
43
63
|
* Returns a PDFDocumentProxy instance from a given binary data.
|
|
44
64
|
*
|
|
@@ -46,18 +66,17 @@ declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
|
|
|
46
66
|
* - `isEvalSupported: false`
|
|
47
67
|
* - `useSystemFonts: true`
|
|
48
68
|
*/
|
|
49
|
-
declare function getDocumentProxy(data:
|
|
69
|
+
declare function getDocumentProxy(data: DocumentInitParameters["data"], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
|
|
50
70
|
declare function getResolvedPDFJS(): Promise<PDFJS>;
|
|
51
71
|
declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<any>, { force }?: {
|
|
52
72
|
force?: boolean | undefined;
|
|
53
73
|
}): Promise<void>;
|
|
54
74
|
|
|
55
|
-
|
|
56
|
-
declare const defineUnPDFConfig: typeof configureUnPDF;
|
|
75
|
+
declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
|
|
57
76
|
|
|
58
77
|
declare const getMeta: typeof getMeta$1;
|
|
59
78
|
declare const extractText: typeof extractText$1;
|
|
60
79
|
declare const extractImages: typeof extractImages$1;
|
|
61
80
|
declare const renderPageAsImage: typeof renderPageAsImage$1;
|
|
62
81
|
|
|
63
|
-
export { configureUnPDF,
|
|
82
|
+
export { configureUnPDF, createIsomorphicCanvasFactory, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,27 +1,49 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DocumentInitParameters, PDFDocumentProxy } from './types/src/display/api';
|
|
2
2
|
import * as canvas from 'canvas';
|
|
3
|
+
import { Canvas } from 'canvas';
|
|
3
4
|
import * as _PDFJS from './types/src/pdf';
|
|
4
5
|
|
|
5
|
-
declare function getMeta$1(data:
|
|
6
|
+
declare function getMeta$1(data: DocumentInitParameters["data"] | PDFDocumentProxy): Promise<{
|
|
6
7
|
info: Record<string, any>;
|
|
7
8
|
metadata: Record<string, any>;
|
|
8
9
|
}>;
|
|
9
10
|
|
|
10
|
-
declare function extractText$1(data:
|
|
11
|
-
mergePages?:
|
|
11
|
+
declare function extractText$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, options?: {
|
|
12
|
+
mergePages?: false;
|
|
12
13
|
}): Promise<{
|
|
13
14
|
totalPages: number;
|
|
14
|
-
text: string
|
|
15
|
+
text: string[];
|
|
16
|
+
}>;
|
|
17
|
+
declare function extractText$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, options: {
|
|
18
|
+
mergePages: true;
|
|
19
|
+
}): Promise<{
|
|
20
|
+
totalPages: number;
|
|
21
|
+
text: string;
|
|
15
22
|
}>;
|
|
16
23
|
|
|
17
|
-
declare function extractImages$1(data:
|
|
18
|
-
declare function renderPageAsImage$1(data:
|
|
24
|
+
declare function extractImages$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
|
|
25
|
+
declare function renderPageAsImage$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number, options?: {
|
|
19
26
|
canvas?: () => Promise<typeof canvas>;
|
|
20
27
|
/** @default 1 */
|
|
21
28
|
scale?: number;
|
|
22
29
|
width?: number;
|
|
23
30
|
height?: number;
|
|
24
31
|
}): Promise<ArrayBuffer>;
|
|
32
|
+
declare function createIsomorphicCanvasFactory(canvas?: () => Promise<typeof canvas>): Promise<{
|
|
33
|
+
_createCanvas(width: number, height: number): Canvas | HTMLCanvasElement;
|
|
34
|
+
create(width: number, height: number): {
|
|
35
|
+
canvas: Canvas | HTMLCanvasElement;
|
|
36
|
+
context: CanvasRenderingContext2D;
|
|
37
|
+
};
|
|
38
|
+
reset(ctx: {
|
|
39
|
+
canvas?: Canvas;
|
|
40
|
+
context?: CanvasRenderingContext2D;
|
|
41
|
+
}, width: number, height: number): void;
|
|
42
|
+
destroy(ctx: {
|
|
43
|
+
canvas?: Canvas;
|
|
44
|
+
context?: CanvasRenderingContext2D;
|
|
45
|
+
}): void;
|
|
46
|
+
}>;
|
|
25
47
|
|
|
26
48
|
type PDFJS = typeof _PDFJS;
|
|
27
49
|
interface UnPDFConfiguration {
|
|
@@ -37,8 +59,6 @@ interface UnPDFConfiguration {
|
|
|
37
59
|
pdfjs?: () => Promise<any>;
|
|
38
60
|
}
|
|
39
61
|
|
|
40
|
-
declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
|
|
41
|
-
|
|
42
62
|
/**
|
|
43
63
|
* Returns a PDFDocumentProxy instance from a given binary data.
|
|
44
64
|
*
|
|
@@ -46,18 +66,17 @@ declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
|
|
|
46
66
|
* - `isEvalSupported: false`
|
|
47
67
|
* - `useSystemFonts: true`
|
|
48
68
|
*/
|
|
49
|
-
declare function getDocumentProxy(data:
|
|
69
|
+
declare function getDocumentProxy(data: DocumentInitParameters["data"], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
|
|
50
70
|
declare function getResolvedPDFJS(): Promise<PDFJS>;
|
|
51
71
|
declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<any>, { force }?: {
|
|
52
72
|
force?: boolean | undefined;
|
|
53
73
|
}): Promise<void>;
|
|
54
74
|
|
|
55
|
-
|
|
56
|
-
declare const defineUnPDFConfig: typeof configureUnPDF;
|
|
75
|
+
declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
|
|
57
76
|
|
|
58
77
|
declare const getMeta: typeof getMeta$1;
|
|
59
78
|
declare const extractText: typeof extractText$1;
|
|
60
79
|
declare const extractImages: typeof extractImages$1;
|
|
61
80
|
declare const renderPageAsImage: typeof renderPageAsImage$1;
|
|
62
81
|
|
|
63
|
-
export { configureUnPDF,
|
|
82
|
+
export { configureUnPDF, createIsomorphicCanvasFactory, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,27 +1,49 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DocumentInitParameters, PDFDocumentProxy } from './types/src/display/api';
|
|
2
2
|
import * as canvas from 'canvas';
|
|
3
|
+
import { Canvas } from 'canvas';
|
|
3
4
|
import * as _PDFJS from './types/src/pdf';
|
|
4
5
|
|
|
5
|
-
declare function getMeta$1(data:
|
|
6
|
+
declare function getMeta$1(data: DocumentInitParameters["data"] | PDFDocumentProxy): Promise<{
|
|
6
7
|
info: Record<string, any>;
|
|
7
8
|
metadata: Record<string, any>;
|
|
8
9
|
}>;
|
|
9
10
|
|
|
10
|
-
declare function extractText$1(data:
|
|
11
|
-
mergePages?:
|
|
11
|
+
declare function extractText$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, options?: {
|
|
12
|
+
mergePages?: false;
|
|
12
13
|
}): Promise<{
|
|
13
14
|
totalPages: number;
|
|
14
|
-
text: string
|
|
15
|
+
text: string[];
|
|
16
|
+
}>;
|
|
17
|
+
declare function extractText$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, options: {
|
|
18
|
+
mergePages: true;
|
|
19
|
+
}): Promise<{
|
|
20
|
+
totalPages: number;
|
|
21
|
+
text: string;
|
|
15
22
|
}>;
|
|
16
23
|
|
|
17
|
-
declare function extractImages$1(data:
|
|
18
|
-
declare function renderPageAsImage$1(data:
|
|
24
|
+
declare function extractImages$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
|
|
25
|
+
declare function renderPageAsImage$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number, options?: {
|
|
19
26
|
canvas?: () => Promise<typeof canvas>;
|
|
20
27
|
/** @default 1 */
|
|
21
28
|
scale?: number;
|
|
22
29
|
width?: number;
|
|
23
30
|
height?: number;
|
|
24
31
|
}): Promise<ArrayBuffer>;
|
|
32
|
+
declare function createIsomorphicCanvasFactory(canvas?: () => Promise<typeof canvas>): Promise<{
|
|
33
|
+
_createCanvas(width: number, height: number): Canvas | HTMLCanvasElement;
|
|
34
|
+
create(width: number, height: number): {
|
|
35
|
+
canvas: Canvas | HTMLCanvasElement;
|
|
36
|
+
context: CanvasRenderingContext2D;
|
|
37
|
+
};
|
|
38
|
+
reset(ctx: {
|
|
39
|
+
canvas?: Canvas;
|
|
40
|
+
context?: CanvasRenderingContext2D;
|
|
41
|
+
}, width: number, height: number): void;
|
|
42
|
+
destroy(ctx: {
|
|
43
|
+
canvas?: Canvas;
|
|
44
|
+
context?: CanvasRenderingContext2D;
|
|
45
|
+
}): void;
|
|
46
|
+
}>;
|
|
25
47
|
|
|
26
48
|
type PDFJS = typeof _PDFJS;
|
|
27
49
|
interface UnPDFConfiguration {
|
|
@@ -37,8 +59,6 @@ interface UnPDFConfiguration {
|
|
|
37
59
|
pdfjs?: () => Promise<any>;
|
|
38
60
|
}
|
|
39
61
|
|
|
40
|
-
declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
|
|
41
|
-
|
|
42
62
|
/**
|
|
43
63
|
* Returns a PDFDocumentProxy instance from a given binary data.
|
|
44
64
|
*
|
|
@@ -46,18 +66,17 @@ declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
|
|
|
46
66
|
* - `isEvalSupported: false`
|
|
47
67
|
* - `useSystemFonts: true`
|
|
48
68
|
*/
|
|
49
|
-
declare function getDocumentProxy(data:
|
|
69
|
+
declare function getDocumentProxy(data: DocumentInitParameters["data"], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
|
|
50
70
|
declare function getResolvedPDFJS(): Promise<PDFJS>;
|
|
51
71
|
declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<any>, { force }?: {
|
|
52
72
|
force?: boolean | undefined;
|
|
53
73
|
}): Promise<void>;
|
|
54
74
|
|
|
55
|
-
|
|
56
|
-
declare const defineUnPDFConfig: typeof configureUnPDF;
|
|
75
|
+
declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
|
|
57
76
|
|
|
58
77
|
declare const getMeta: typeof getMeta$1;
|
|
59
78
|
declare const extractText: typeof extractText$1;
|
|
60
79
|
declare const extractImages: typeof extractImages$1;
|
|
61
80
|
declare const renderPageAsImage: typeof renderPageAsImage$1;
|
|
62
81
|
|
|
63
|
-
export { configureUnPDF,
|
|
82
|
+
export { configureUnPDF, createIsomorphicCanvasFactory, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
|
package/dist/index.mjs
CHANGED
|
@@ -29,16 +29,18 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
|
29
29
|
resolvedModule = await resolvedModule.resolvePDFJS();
|
|
30
30
|
}
|
|
31
31
|
return;
|
|
32
|
-
} catch {
|
|
32
|
+
} catch (error) {
|
|
33
|
+
console.error(error);
|
|
33
34
|
throw new Error(
|
|
34
35
|
"Resolving failed. Please check the provided configuration."
|
|
35
36
|
);
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
39
|
try {
|
|
39
|
-
const { resolvePDFJS } = await
|
|
40
|
+
const { resolvePDFJS } = await Promise.resolve().then(function () { return pdfjs; });
|
|
40
41
|
resolvedModule = await resolvePDFJS();
|
|
41
|
-
} catch {
|
|
42
|
+
} catch (error) {
|
|
43
|
+
console.error(error);
|
|
42
44
|
throw new Error(
|
|
43
45
|
"PDF.js is not available. Please add the package as a dependency."
|
|
44
46
|
);
|
|
@@ -67,20 +69,15 @@ async function extractText$1(data, options = {}) {
|
|
|
67
69
|
const texts = await Promise.all(
|
|
68
70
|
Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
|
|
69
71
|
);
|
|
70
|
-
const filteredTexts = texts.filter(Boolean);
|
|
71
72
|
return {
|
|
72
73
|
totalPages: pdf.numPages,
|
|
73
|
-
text: mergePages ?
|
|
74
|
+
text: mergePages ? texts.join("\n").replace(/\s+/g, " ") : texts
|
|
74
75
|
};
|
|
75
76
|
}
|
|
76
|
-
async function getPageText(
|
|
77
|
-
const page = await
|
|
77
|
+
async function getPageText(document, pageNumber) {
|
|
78
|
+
const page = await document.getPage(pageNumber);
|
|
78
79
|
const content = await page.getTextContent();
|
|
79
|
-
|
|
80
|
-
if (items.length === 0) {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
return items.map(({ str }) => str).join("\n");
|
|
80
|
+
return content.items.filter((item) => item.str != null).map((item) => item.str + (item.hasEOL ? "\n" : "")).join("");
|
|
84
81
|
}
|
|
85
82
|
|
|
86
83
|
async function extractImages$1(data, pageNumber) {
|
|
@@ -107,7 +104,7 @@ async function extractImages$1(data, pageNumber) {
|
|
|
107
104
|
}
|
|
108
105
|
async function renderPageAsImage$1(data, pageNumber, options = {}) {
|
|
109
106
|
const canvasFactory = await createIsomorphicCanvasFactory(options.canvas);
|
|
110
|
-
const pdf = await getDocumentProxy(data, { canvasFactory });
|
|
107
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data, { canvasFactory });
|
|
111
108
|
const page = await pdf.getPage(pageNumber);
|
|
112
109
|
if (pageNumber < 1 || pageNumber > pdf.numPages) {
|
|
113
110
|
throw new Error(
|
|
@@ -185,7 +182,6 @@ async function configureUnPDF(options) {
|
|
|
185
182
|
}
|
|
186
183
|
}
|
|
187
184
|
|
|
188
|
-
const defineUnPDFConfig = configureUnPDF;
|
|
189
185
|
const getMeta = async (...args) => {
|
|
190
186
|
await resolvePDFJSImports();
|
|
191
187
|
return await getMeta$1(...args);
|
|
@@ -203,4 +199,8 @@ const renderPageAsImage = async (...args) => {
|
|
|
203
199
|
return await renderPageAsImage$1(...args);
|
|
204
200
|
};
|
|
205
201
|
|
|
206
|
-
|
|
202
|
+
const pdfjs = {
|
|
203
|
+
__proto__: null
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
export { configureUnPDF, createIsomorphicCanvasFactory, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
|
package/dist/pdfjs.d.mts
CHANGED
package/dist/pdfjs.d.ts
CHANGED