unpdf 0.10.0 → 0.11.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 +9 -7
- package/dist/index.cjs +15 -19
- 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 -19
- 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 +10 -3
- package/dist/types/src/display/annotation_storage.d.ts +1 -0
- package/dist/types/src/display/api.d.ts +54 -26
- package/dist/types/src/display/base_factory.d.ts +3 -1
- package/dist/types/src/display/canvas.d.ts +2 -0
- package/dist/types/src/display/display_utils.d.ts +7 -5
- package/dist/types/src/display/draw_layer.d.ts +38 -0
- package/dist/types/src/display/editor/alt_text.d.ts +22 -0
- package/dist/types/src/display/editor/annotation_editor_layer.d.ts +24 -2
- package/dist/types/src/display/editor/color_picker.d.ts +19 -0
- package/dist/types/src/display/editor/editor.d.ts +78 -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 +4 -4
- package/dist/types/src/display/editor/outliner.d.ts +70 -0
- package/dist/types/src/display/editor/stamp.d.ts +1 -1
- package/dist/types/src/display/editor/toolbar.d.ts +17 -0
- package/dist/types/src/display/editor/tools.d.ts +39 -4
- 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 +4 -5
- 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 +40 -108
- 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 +6 -3
- package/dist/types/src/shared/util.d.ts +24 -23
- package/dist/types/web/annotation_editor_layer_builder.d.ts +10 -13
- package/dist/types/web/annotation_layer_builder.d.ts +7 -4
- package/dist/types/web/app_options.d.ts +1 -2
- package/dist/types/web/draw_layer_builder.d.ts +23 -0
- package/dist/types/web/genericl10n.d.ts +5 -3
- package/dist/types/web/interfaces.d.ts +11 -8
- package/dist/types/web/l10n.d.ts +5 -2
- package/dist/types/web/pdf_find_controller.d.ts +1 -2
- package/dist/types/web/pdf_history.d.ts +1 -53
- package/dist/types/web/pdf_link_service.d.ts +6 -109
- package/dist/types/web/pdf_page_view.d.ts +5 -16
- package/dist/types/web/pdf_scripting_manager.d.ts +6 -10
- package/dist/types/web/pdf_single_page_viewer.d.ts +2 -2
- package/dist/types/web/pdf_thumbnail_view.d.ts +1 -13
- package/dist/types/web/pdf_thumbnail_viewer.d.ts +1 -17
- package/dist/types/web/pdf_viewer.component.d.ts +1 -2
- package/dist/types/web/pdf_viewer.d.ts +90 -42
- 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 +1 -1
- package/package.json +21 -30
- package/dist/types/web/l10n_utils.d.ts +0 -9
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@ This library is also intended as a modern alternative to the unmaintained but st
|
|
|
17
17
|
## PDF.js Compatibility
|
|
18
18
|
|
|
19
19
|
> [!NOTE]
|
|
20
|
-
> This package is currently using PDF.js v4.
|
|
20
|
+
> This package is currently using PDF.js v4.3.136.
|
|
21
21
|
|
|
22
22
|
## Installation
|
|
23
23
|
|
|
@@ -50,7 +50,7 @@ const buffer = await fetch(
|
|
|
50
50
|
const buffer = await readFile("./dummy.pdf");
|
|
51
51
|
|
|
52
52
|
// Load PDF from buffer
|
|
53
|
-
const pdf = await getDocumentProxy(new Uint8Array(
|
|
53
|
+
const pdf = await getDocumentProxy(new Uint8Array(buffer));
|
|
54
54
|
// Extract text from PDF
|
|
55
55
|
const { totalPages, text } = await extractText(pdf, { mergePages: true });
|
|
56
56
|
```
|
|
@@ -91,7 +91,7 @@ Generally speaking, you don't need to worry about the PDF.js build. `unpdf` ship
|
|
|
91
91
|
// if you need another PDF.js build
|
|
92
92
|
import { configureUnPDF } from "unpdf";
|
|
93
93
|
|
|
94
|
-
configureUnPDF({
|
|
94
|
+
await configureUnPDF({
|
|
95
95
|
// Use the official PDF.js build (make sure to install it first)
|
|
96
96
|
pdfjs: () => import("pdfjs-dist"),
|
|
97
97
|
});
|
|
@@ -138,7 +138,9 @@ function getResolvedPDFJS(): Promise<PDFJS>;
|
|
|
138
138
|
### `getMeta`
|
|
139
139
|
|
|
140
140
|
```ts
|
|
141
|
-
function getMeta(
|
|
141
|
+
function getMeta(
|
|
142
|
+
data: DocumentInitParameters["data"] | PDFDocumentProxy,
|
|
143
|
+
): Promise<{
|
|
142
144
|
info: Record<string, any>;
|
|
143
145
|
metadata: Record<string, any>;
|
|
144
146
|
}>;
|
|
@@ -150,7 +152,7 @@ Extracts all text from a PDF. If `mergePages` is set to `true`, the text of all
|
|
|
150
152
|
|
|
151
153
|
```ts
|
|
152
154
|
function extractText(
|
|
153
|
-
data:
|
|
155
|
+
data: DocumentInitParameters["data"] | PDFDocumentProxy,
|
|
154
156
|
{ mergePages }?: { mergePages?: boolean },
|
|
155
157
|
): Promise<{
|
|
156
158
|
totalPages: number;
|
|
@@ -175,7 +177,7 @@ In order to use this method, you have to meet the following requirements:
|
|
|
175
177
|
```ts
|
|
176
178
|
import { configureUnPDF, renderPageAsImage } from "unpdf";
|
|
177
179
|
|
|
178
|
-
configureUnPDF({
|
|
180
|
+
await configureUnPDF({
|
|
179
181
|
// Use the official PDF.js build
|
|
180
182
|
pdfjs: () => import("pdfjs-dist"),
|
|
181
183
|
});
|
|
@@ -194,7 +196,7 @@ await writeFile("dummy-page-1.png", Buffer.from(result));
|
|
|
194
196
|
|
|
195
197
|
```ts
|
|
196
198
|
declare function renderPageAsImage(
|
|
197
|
-
data:
|
|
199
|
+
data: DocumentInitParameters["data"],
|
|
198
200
|
pageNumber: number,
|
|
199
201
|
options?: {
|
|
200
202
|
canvas?: () => Promise<typeof import("canvas")>;
|
package/dist/index.cjs
CHANGED
|
@@ -26,12 +26,10 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
|
26
26
|
}
|
|
27
27
|
if (pdfjsResolver) {
|
|
28
28
|
try {
|
|
29
|
-
|
|
30
|
-
if ("resolvePDFJS" in
|
|
31
|
-
resolvedModule = await
|
|
32
|
-
return;
|
|
29
|
+
resolvedModule = await interopDefault(pdfjsResolver());
|
|
30
|
+
if (resolvedModule && "resolvePDFJS" in resolvedModule) {
|
|
31
|
+
resolvedModule = await resolvedModule.resolvePDFJS();
|
|
33
32
|
}
|
|
34
|
-
resolvedModule = _import.default || _import;
|
|
35
33
|
return;
|
|
36
34
|
} catch {
|
|
37
35
|
throw new Error(
|
|
@@ -51,6 +49,10 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
|
51
49
|
function isPDFDocumentProxy(data) {
|
|
52
50
|
return typeof data === "object" && data !== null && "_pdfInfo" in data;
|
|
53
51
|
}
|
|
52
|
+
async function interopDefault(m) {
|
|
53
|
+
const resolved = await m;
|
|
54
|
+
return resolved.default || resolved;
|
|
55
|
+
}
|
|
54
56
|
|
|
55
57
|
async function getMeta$1(data) {
|
|
56
58
|
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
@@ -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) {
|
|
@@ -106,7 +103,8 @@ async function extractImages$1(data, pageNumber) {
|
|
|
106
103
|
return images;
|
|
107
104
|
}
|
|
108
105
|
async function renderPageAsImage$1(data, pageNumber, options = {}) {
|
|
109
|
-
const
|
|
106
|
+
const canvasFactory = await createIsomorphicCanvasFactory(options.canvas);
|
|
107
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data, { canvasFactory });
|
|
110
108
|
const page = await pdf.getPage(pageNumber);
|
|
111
109
|
if (pageNumber < 1 || pageNumber > pdf.numPages) {
|
|
112
110
|
throw new Error(
|
|
@@ -123,7 +121,6 @@ async function renderPageAsImage$1(data, pageNumber, options = {}) {
|
|
|
123
121
|
if (outputScale !== 1 && outputScale > 0) {
|
|
124
122
|
viewport = page.getViewport({ scale: outputScale });
|
|
125
123
|
}
|
|
126
|
-
const canvasFactory = await createIsomorphicCanvasFactory(options.canvas);
|
|
127
124
|
const ctx = canvasFactory.create(viewport.width, viewport.height);
|
|
128
125
|
await page.render({
|
|
129
126
|
canvasContext: ctx.context,
|
|
@@ -134,7 +131,7 @@ async function renderPageAsImage$1(data, pageNumber, options = {}) {
|
|
|
134
131
|
return await response.arrayBuffer();
|
|
135
132
|
}
|
|
136
133
|
async function createIsomorphicCanvasFactory(canvas) {
|
|
137
|
-
const _canvas = await canvas
|
|
134
|
+
const _canvas = canvas ? await interopDefault(canvas()) : void 0;
|
|
138
135
|
return {
|
|
139
136
|
_createCanvas(width, height) {
|
|
140
137
|
if (isBrowser) {
|
|
@@ -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);
|
|
@@ -204,7 +200,7 @@ const renderPageAsImage = async (...args) => {
|
|
|
204
200
|
};
|
|
205
201
|
|
|
206
202
|
exports.configureUnPDF = configureUnPDF;
|
|
207
|
-
exports.
|
|
203
|
+
exports.createIsomorphicCanvasFactory = createIsomorphicCanvasFactory;
|
|
208
204
|
exports.extractImages = extractImages;
|
|
209
205
|
exports.extractText = extractText;
|
|
210
206
|
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
|
@@ -24,12 +24,10 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
|
24
24
|
}
|
|
25
25
|
if (pdfjsResolver) {
|
|
26
26
|
try {
|
|
27
|
-
|
|
28
|
-
if ("resolvePDFJS" in
|
|
29
|
-
resolvedModule = await
|
|
30
|
-
return;
|
|
27
|
+
resolvedModule = await interopDefault(pdfjsResolver());
|
|
28
|
+
if (resolvedModule && "resolvePDFJS" in resolvedModule) {
|
|
29
|
+
resolvedModule = await resolvedModule.resolvePDFJS();
|
|
31
30
|
}
|
|
32
|
-
resolvedModule = _import.default || _import;
|
|
33
31
|
return;
|
|
34
32
|
} catch {
|
|
35
33
|
throw new Error(
|
|
@@ -49,6 +47,10 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
|
49
47
|
function isPDFDocumentProxy(data) {
|
|
50
48
|
return typeof data === "object" && data !== null && "_pdfInfo" in data;
|
|
51
49
|
}
|
|
50
|
+
async function interopDefault(m) {
|
|
51
|
+
const resolved = await m;
|
|
52
|
+
return resolved.default || resolved;
|
|
53
|
+
}
|
|
52
54
|
|
|
53
55
|
async function getMeta$1(data) {
|
|
54
56
|
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
@@ -65,20 +67,15 @@ async function extractText$1(data, options = {}) {
|
|
|
65
67
|
const texts = await Promise.all(
|
|
66
68
|
Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
|
|
67
69
|
);
|
|
68
|
-
const filteredTexts = texts.filter(Boolean);
|
|
69
70
|
return {
|
|
70
71
|
totalPages: pdf.numPages,
|
|
71
|
-
text: mergePages ?
|
|
72
|
+
text: mergePages ? texts.join("\n").replace(/\s+/g, " ") : texts
|
|
72
73
|
};
|
|
73
74
|
}
|
|
74
|
-
async function getPageText(
|
|
75
|
-
const page = await
|
|
75
|
+
async function getPageText(document, pageNumber) {
|
|
76
|
+
const page = await document.getPage(pageNumber);
|
|
76
77
|
const content = await page.getTextContent();
|
|
77
|
-
|
|
78
|
-
if (items.length === 0) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
return items.map(({ str }) => str).join("\n");
|
|
78
|
+
return content.items.filter((item) => item.str != null).map((item) => item.str + (item.hasEOL ? "\n" : "")).join("");
|
|
82
79
|
}
|
|
83
80
|
|
|
84
81
|
async function extractImages$1(data, pageNumber) {
|
|
@@ -104,7 +101,8 @@ async function extractImages$1(data, pageNumber) {
|
|
|
104
101
|
return images;
|
|
105
102
|
}
|
|
106
103
|
async function renderPageAsImage$1(data, pageNumber, options = {}) {
|
|
107
|
-
const
|
|
104
|
+
const canvasFactory = await createIsomorphicCanvasFactory(options.canvas);
|
|
105
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data, { canvasFactory });
|
|
108
106
|
const page = await pdf.getPage(pageNumber);
|
|
109
107
|
if (pageNumber < 1 || pageNumber > pdf.numPages) {
|
|
110
108
|
throw new Error(
|
|
@@ -121,7 +119,6 @@ async function renderPageAsImage$1(data, pageNumber, options = {}) {
|
|
|
121
119
|
if (outputScale !== 1 && outputScale > 0) {
|
|
122
120
|
viewport = page.getViewport({ scale: outputScale });
|
|
123
121
|
}
|
|
124
|
-
const canvasFactory = await createIsomorphicCanvasFactory(options.canvas);
|
|
125
122
|
const ctx = canvasFactory.create(viewport.width, viewport.height);
|
|
126
123
|
await page.render({
|
|
127
124
|
canvasContext: ctx.context,
|
|
@@ -132,7 +129,7 @@ async function renderPageAsImage$1(data, pageNumber, options = {}) {
|
|
|
132
129
|
return await response.arrayBuffer();
|
|
133
130
|
}
|
|
134
131
|
async function createIsomorphicCanvasFactory(canvas) {
|
|
135
|
-
const _canvas = await canvas
|
|
132
|
+
const _canvas = canvas ? await interopDefault(canvas()) : void 0;
|
|
136
133
|
return {
|
|
137
134
|
_createCanvas(width, height) {
|
|
138
135
|
if (isBrowser) {
|
|
@@ -183,7 +180,6 @@ async function configureUnPDF(options) {
|
|
|
183
180
|
}
|
|
184
181
|
}
|
|
185
182
|
|
|
186
|
-
const defineUnPDFConfig = configureUnPDF;
|
|
187
183
|
const getMeta = async (...args) => {
|
|
188
184
|
await resolvePDFJSImports();
|
|
189
185
|
return await getMeta$1(...args);
|
|
@@ -201,4 +197,4 @@ const renderPageAsImage = async (...args) => {
|
|
|
201
197
|
return await renderPageAsImage$1(...args);
|
|
202
198
|
};
|
|
203
199
|
|
|
204
|
-
export { configureUnPDF,
|
|
200
|
+
export { configureUnPDF, createIsomorphicCanvasFactory, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
|
package/dist/pdfjs.d.mts
CHANGED
package/dist/pdfjs.d.ts
CHANGED