unpdf 0.5.1 → 0.5.3
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 +17 -10
- package/dist/index.node.cjs +2 -2
- package/dist/index.node.d.cts +2 -2
- package/dist/index.node.d.mts +2 -2
- package/dist/index.node.d.ts +2 -2
- package/dist/index.node.mjs +3 -3
- package/dist/index.web.cjs +2 -2
- package/dist/index.web.d.cts +2 -2
- package/dist/index.web.d.mts +2 -2
- package/dist/index.web.d.ts +2 -2
- package/dist/index.web.mjs +3 -3
- package/dist/index.worker.cjs +1 -1
- package/dist/index.worker.d.cts +2 -2
- package/dist/index.worker.d.mts +2 -2
- package/dist/index.worker.d.ts +2 -2
- package/dist/index.worker.mjs +1 -1
- package/dist/shared/{unpdf.15eec1ee.cjs → unpdf.61a2a4e1.cjs} +16 -3
- package/dist/shared/{unpdf.3a651ae7.cjs → unpdf.9511ac30.cjs} +4 -4
- package/dist/shared/{unpdf.9db6f9f8.mjs → unpdf.c7f36e56.mjs} +16 -4
- package/dist/shared/{unpdf.d5a4740d.mjs → unpdf.cd67d475.mjs} +4 -4
- package/dist/shared/{unpdf.3a0402b0.d.cts → unpdf.d3d8f5ac.d.cts} +14 -6
- package/dist/shared/{unpdf.3a0402b0.d.mts → unpdf.d3d8f5ac.d.mts} +14 -6
- package/dist/shared/{unpdf.3a0402b0.d.ts → unpdf.d3d8f5ac.d.ts} +14 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ This library is also intended as a modern alternative to the unmaintained but st
|
|
|
9
9
|
## Features
|
|
10
10
|
|
|
11
11
|
- 🏗️ Conditional exports for Browser, Node and worker environments
|
|
12
|
-
- 💬 Extract text from PDFs
|
|
12
|
+
- 💬 Extract text and images from PDFs
|
|
13
13
|
- 🧱 Opt-in to legacy PDF.js build
|
|
14
14
|
|
|
15
15
|
## Installation
|
|
@@ -32,11 +32,16 @@ yarn add unpdf
|
|
|
32
32
|
```ts
|
|
33
33
|
import { extractPDFText } from 'unpdf'
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
// Fetch a PDF file from the web
|
|
36
|
+
const pdf = await fetch('https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf')
|
|
36
37
|
.then(res => res.arrayBuffer())
|
|
37
38
|
|
|
39
|
+
// Or load it from the filesystem
|
|
40
|
+
const pdf = await readFile('./dummy.pdf')
|
|
41
|
+
|
|
42
|
+
// Pass the PDF buffer to the relevant method
|
|
38
43
|
const { totalPages, text } = await extractPDFText(
|
|
39
|
-
new Uint8Array(
|
|
44
|
+
new Uint8Array(pdf), { mergePages: true }
|
|
40
45
|
)
|
|
41
46
|
```
|
|
42
47
|
|
|
@@ -75,7 +80,7 @@ interface UnPDFConfiguration {
|
|
|
75
80
|
* @example
|
|
76
81
|
* () => import('pdfjs-dist/legacy/build/pdf.js')
|
|
77
82
|
*/
|
|
78
|
-
pdfjs?: () => typeof PDFJS
|
|
83
|
+
pdfjs?: () => Promise<typeof PDFJS>
|
|
79
84
|
}
|
|
80
85
|
```
|
|
81
86
|
|
|
@@ -83,15 +88,15 @@ interface UnPDFConfiguration {
|
|
|
83
88
|
|
|
84
89
|
### `defineUnPDFConfig`
|
|
85
90
|
|
|
86
|
-
Define a custom PDF.js module, like the legacy build.
|
|
91
|
+
Define a custom PDF.js module, like the legacy build. Make sure to call this method before using any other methods.
|
|
87
92
|
|
|
88
93
|
```ts
|
|
89
|
-
function defineUnPDFConfig(
|
|
94
|
+
function defineUnPDFConfig(config: UnPDFConfiguration): Promise<void>
|
|
90
95
|
```
|
|
91
96
|
|
|
92
97
|
### `getResolvedPDFJS`
|
|
93
98
|
|
|
94
|
-
Returns the resolved PDF.js module.
|
|
99
|
+
Returns the resolved PDF.js module. If no build is defined, the latest version will be initialized.
|
|
95
100
|
|
|
96
101
|
```ts
|
|
97
102
|
function getResolvedPDFJS(): Promise<typeof import('pdfjs-dist')>
|
|
@@ -100,7 +105,9 @@ function getResolvedPDFJS(): Promise<typeof import('pdfjs-dist')>
|
|
|
100
105
|
### `getPDFMeta`
|
|
101
106
|
|
|
102
107
|
```ts
|
|
103
|
-
function getPDFMeta(
|
|
108
|
+
function getPDFMeta(
|
|
109
|
+
data: BinaryData | PDFDocumentProxy
|
|
110
|
+
): Promise<{
|
|
104
111
|
info: Record<string, any>
|
|
105
112
|
metadata: Record<string, any>
|
|
106
113
|
}>
|
|
@@ -110,7 +117,7 @@ function getPDFMeta(data: ArrayBuffer): Promise<{
|
|
|
110
117
|
|
|
111
118
|
```ts
|
|
112
119
|
function extractPDFText(
|
|
113
|
-
data:
|
|
120
|
+
data: BinaryData | PDFDocumentProxy,
|
|
114
121
|
{ mergePages }?: { mergePages?: boolean }
|
|
115
122
|
): Promise<{
|
|
116
123
|
totalPages: number
|
|
@@ -122,7 +129,7 @@ function extractPDFText(
|
|
|
122
129
|
|
|
123
130
|
```ts
|
|
124
131
|
function getImagesFromPage(
|
|
125
|
-
data:
|
|
132
|
+
data: BinaryData | PDFDocumentProxy,
|
|
126
133
|
pageNumber: number
|
|
127
134
|
): Promise<ArrayBuffer[]>
|
|
128
135
|
```
|
package/dist/index.node.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const image = require('./shared/unpdf.
|
|
4
|
-
const utils = require('./shared/unpdf.
|
|
3
|
+
const image = require('./shared/unpdf.9511ac30.cjs');
|
|
4
|
+
const utils = require('./shared/unpdf.61a2a4e1.cjs');
|
|
5
5
|
|
|
6
6
|
const getPDFMeta = async (...args) => {
|
|
7
7
|
await utils.resolvePDFJSImports();
|
package/dist/index.node.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.
|
|
2
|
-
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.
|
|
1
|
+
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.d3d8f5ac.cjs';
|
|
2
|
+
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.d3d8f5ac.cjs';
|
|
3
3
|
import 'pdfjs-dist/types/src/display/api';
|
|
4
4
|
import 'pdfjs-dist';
|
|
5
5
|
|
package/dist/index.node.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.
|
|
2
|
-
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.
|
|
1
|
+
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.d3d8f5ac.mjs';
|
|
2
|
+
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.d3d8f5ac.mjs';
|
|
3
3
|
import 'pdfjs-dist/types/src/display/api';
|
|
4
4
|
import 'pdfjs-dist';
|
|
5
5
|
|
package/dist/index.node.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.
|
|
2
|
-
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.
|
|
1
|
+
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.d3d8f5ac.js';
|
|
2
|
+
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.d3d8f5ac.js';
|
|
3
3
|
import 'pdfjs-dist/types/src/display/api';
|
|
4
4
|
import 'pdfjs-dist';
|
|
5
5
|
|
package/dist/index.node.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.
|
|
2
|
-
import { r as resolvePDFJSImports } from './shared/unpdf.
|
|
3
|
-
export { d as defineUnPDFConfig, g as getDocumentProxy, a as getResolvedPDFJS } from './shared/unpdf.
|
|
1
|
+
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.cd67d475.mjs';
|
|
2
|
+
import { r as resolvePDFJSImports } from './shared/unpdf.c7f36e56.mjs';
|
|
3
|
+
export { d as defineUnPDFConfig, g as getDocumentProxy, a as getResolvedPDFJS } from './shared/unpdf.c7f36e56.mjs';
|
|
4
4
|
|
|
5
5
|
const getPDFMeta = async (...args) => {
|
|
6
6
|
await resolvePDFJSImports();
|
package/dist/index.web.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const image = require('./shared/unpdf.
|
|
4
|
-
const utils = require('./shared/unpdf.
|
|
3
|
+
const image = require('./shared/unpdf.9511ac30.cjs');
|
|
4
|
+
const utils = require('./shared/unpdf.61a2a4e1.cjs');
|
|
5
5
|
|
|
6
6
|
const getPDFMeta = async (...args) => {
|
|
7
7
|
await utils.resolvePDFJSImports();
|
package/dist/index.web.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.
|
|
2
|
-
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.
|
|
1
|
+
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.d3d8f5ac.cjs';
|
|
2
|
+
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.d3d8f5ac.cjs';
|
|
3
3
|
import 'pdfjs-dist/types/src/display/api';
|
|
4
4
|
import 'pdfjs-dist';
|
|
5
5
|
|
package/dist/index.web.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.
|
|
2
|
-
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.
|
|
1
|
+
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.d3d8f5ac.mjs';
|
|
2
|
+
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.d3d8f5ac.mjs';
|
|
3
3
|
import 'pdfjs-dist/types/src/display/api';
|
|
4
4
|
import 'pdfjs-dist';
|
|
5
5
|
|
package/dist/index.web.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.
|
|
2
|
-
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.
|
|
1
|
+
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.d3d8f5ac.js';
|
|
2
|
+
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.d3d8f5ac.js';
|
|
3
3
|
import 'pdfjs-dist/types/src/display/api';
|
|
4
4
|
import 'pdfjs-dist';
|
|
5
5
|
|
package/dist/index.web.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.
|
|
2
|
-
import { r as resolvePDFJSImports } from './shared/unpdf.
|
|
3
|
-
export { d as defineUnPDFConfig, g as getDocumentProxy, a as getResolvedPDFJS } from './shared/unpdf.
|
|
1
|
+
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.cd67d475.mjs';
|
|
2
|
+
import { r as resolvePDFJSImports } from './shared/unpdf.c7f36e56.mjs';
|
|
3
|
+
export { d as defineUnPDFConfig, g as getDocumentProxy, a as getResolvedPDFJS } from './shared/unpdf.c7f36e56.mjs';
|
|
4
4
|
|
|
5
5
|
const getPDFMeta = async (...args) => {
|
|
6
6
|
await resolvePDFJSImports();
|
package/dist/index.worker.cjs
CHANGED
package/dist/index.worker.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.
|
|
2
|
-
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.
|
|
1
|
+
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.d3d8f5ac.cjs';
|
|
2
|
+
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.d3d8f5ac.cjs';
|
|
3
3
|
import 'pdfjs-dist/types/src/display/api';
|
|
4
4
|
import 'pdfjs-dist';
|
|
5
5
|
|
package/dist/index.worker.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.
|
|
2
|
-
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.
|
|
1
|
+
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.d3d8f5ac.mjs';
|
|
2
|
+
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.d3d8f5ac.mjs';
|
|
3
3
|
import 'pdfjs-dist/types/src/display/api';
|
|
4
4
|
import 'pdfjs-dist';
|
|
5
5
|
|
package/dist/index.worker.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.
|
|
2
|
-
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.
|
|
1
|
+
import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.d3d8f5ac.js';
|
|
2
|
+
export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.d3d8f5ac.js';
|
|
3
3
|
import 'pdfjs-dist/types/src/display/api';
|
|
4
4
|
import 'pdfjs-dist';
|
|
5
5
|
|
package/dist/index.worker.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { d as defineUnPDFConfig, g as getDocumentProxy, a as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.
|
|
1
|
+
export { d as defineUnPDFConfig, g as getDocumentProxy, a as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.c7f36e56.mjs';
|
|
2
2
|
|
|
3
3
|
const getPDFMeta = async () => {
|
|
4
4
|
throw new Error("Not implemented in worker context yet");
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
let resolvedModule;
|
|
4
|
-
async function getDocumentProxy(data) {
|
|
4
|
+
async function getDocumentProxy(data, options = {}) {
|
|
5
5
|
const { getDocument } = await getResolvedPDFJS();
|
|
6
6
|
const pdf = await getDocument({
|
|
7
7
|
data,
|
|
8
8
|
useWorkerFetch: false,
|
|
9
|
+
isEvalSupported: false,
|
|
9
10
|
useSystemFonts: true,
|
|
10
|
-
|
|
11
|
+
...options
|
|
11
12
|
}).promise;
|
|
12
13
|
return pdf;
|
|
13
14
|
}
|
|
@@ -26,9 +27,17 @@ async function getResolvedPDFJS() {
|
|
|
26
27
|
await resolvePDFJSImports();
|
|
27
28
|
return resolvedModule;
|
|
28
29
|
}
|
|
29
|
-
async function resolvePDFJSImports() {
|
|
30
|
+
async function resolvePDFJSImports(pdfjs) {
|
|
30
31
|
if (resolvedModule)
|
|
31
32
|
return;
|
|
33
|
+
if (pdfjs) {
|
|
34
|
+
try {
|
|
35
|
+
const { default: mod } = await pdfjs();
|
|
36
|
+
resolvedModule = mod;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
throw new Error("Resolving the PDF.js module failed. Please check your configuration.");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
32
41
|
try {
|
|
33
42
|
const { default: mod } = await import('pdfjs-dist');
|
|
34
43
|
resolvedModule = mod;
|
|
@@ -36,8 +45,12 @@ async function resolvePDFJSImports() {
|
|
|
36
45
|
throw new Error("PDF.js is not available. Please add the package as a dependency.");
|
|
37
46
|
}
|
|
38
47
|
}
|
|
48
|
+
function isPDFDocumentProxy(data) {
|
|
49
|
+
return typeof data === "object" && data !== null && "_pdfInfo" in data;
|
|
50
|
+
}
|
|
39
51
|
|
|
40
52
|
exports.defineUnPDFConfig = defineUnPDFConfig;
|
|
41
53
|
exports.getDocumentProxy = getDocumentProxy;
|
|
42
54
|
exports.getResolvedPDFJS = getResolvedPDFJS;
|
|
55
|
+
exports.isPDFDocumentProxy = isPDFDocumentProxy;
|
|
43
56
|
exports.resolvePDFJSImports = resolvePDFJSImports;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const utils = require('./unpdf.
|
|
3
|
+
const utils = require('./unpdf.61a2a4e1.cjs');
|
|
4
4
|
|
|
5
5
|
async function getPDFMeta(data) {
|
|
6
|
-
const pdf = await utils.getDocumentProxy(data);
|
|
6
|
+
const pdf = utils.isPDFDocumentProxy(data) ? data : await utils.getDocumentProxy(data);
|
|
7
7
|
const meta = await pdf.getMetadata().catch(() => null);
|
|
8
8
|
return {
|
|
9
9
|
info: meta?.info ?? {},
|
|
@@ -12,7 +12,7 @@ async function getPDFMeta(data) {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
async function extractPDFText(data, { mergePages = false } = {}) {
|
|
15
|
-
const pdf = await utils.getDocumentProxy(data);
|
|
15
|
+
const pdf = utils.isPDFDocumentProxy(data) ? data : await utils.getDocumentProxy(data);
|
|
16
16
|
const texts = await Promise.all(
|
|
17
17
|
Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
|
|
18
18
|
);
|
|
@@ -32,7 +32,7 @@ async function getPageText(pdf, pageNumber) {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
async function getImagesFromPage(data, pageNumber) {
|
|
35
|
-
const pdf = await utils.getDocumentProxy(data);
|
|
35
|
+
const pdf = utils.isPDFDocumentProxy(data) ? data : await utils.getDocumentProxy(data);
|
|
36
36
|
const page = await pdf.getPage(pageNumber);
|
|
37
37
|
const operatorList = await page.getOperatorList();
|
|
38
38
|
const { OPS } = await utils.getResolvedPDFJS();
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
let resolvedModule;
|
|
2
|
-
async function getDocumentProxy(data) {
|
|
2
|
+
async function getDocumentProxy(data, options = {}) {
|
|
3
3
|
const { getDocument } = await getResolvedPDFJS();
|
|
4
4
|
const pdf = await getDocument({
|
|
5
5
|
data,
|
|
6
6
|
useWorkerFetch: false,
|
|
7
|
+
isEvalSupported: false,
|
|
7
8
|
useSystemFonts: true,
|
|
8
|
-
|
|
9
|
+
...options
|
|
9
10
|
}).promise;
|
|
10
11
|
return pdf;
|
|
11
12
|
}
|
|
@@ -24,9 +25,17 @@ async function getResolvedPDFJS() {
|
|
|
24
25
|
await resolvePDFJSImports();
|
|
25
26
|
return resolvedModule;
|
|
26
27
|
}
|
|
27
|
-
async function resolvePDFJSImports() {
|
|
28
|
+
async function resolvePDFJSImports(pdfjs) {
|
|
28
29
|
if (resolvedModule)
|
|
29
30
|
return;
|
|
31
|
+
if (pdfjs) {
|
|
32
|
+
try {
|
|
33
|
+
const { default: mod } = await pdfjs();
|
|
34
|
+
resolvedModule = mod;
|
|
35
|
+
} catch (error) {
|
|
36
|
+
throw new Error("Resolving the PDF.js module failed. Please check your configuration.");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
30
39
|
try {
|
|
31
40
|
const { default: mod } = await import('pdfjs-dist');
|
|
32
41
|
resolvedModule = mod;
|
|
@@ -34,5 +43,8 @@ async function resolvePDFJSImports() {
|
|
|
34
43
|
throw new Error("PDF.js is not available. Please add the package as a dependency.");
|
|
35
44
|
}
|
|
36
45
|
}
|
|
46
|
+
function isPDFDocumentProxy(data) {
|
|
47
|
+
return typeof data === "object" && data !== null && "_pdfInfo" in data;
|
|
48
|
+
}
|
|
37
49
|
|
|
38
|
-
export { getResolvedPDFJS as a, defineUnPDFConfig as d, getDocumentProxy as g, resolvePDFJSImports as r };
|
|
50
|
+
export { getResolvedPDFJS as a, defineUnPDFConfig as d, getDocumentProxy as g, isPDFDocumentProxy as i, resolvePDFJSImports as r };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { g as getDocumentProxy, a as getResolvedPDFJS } from './unpdf.
|
|
1
|
+
import { i as isPDFDocumentProxy, g as getDocumentProxy, a as getResolvedPDFJS } from './unpdf.c7f36e56.mjs';
|
|
2
2
|
|
|
3
3
|
async function getPDFMeta(data) {
|
|
4
|
-
const pdf = await getDocumentProxy(data);
|
|
4
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
5
5
|
const meta = await pdf.getMetadata().catch(() => null);
|
|
6
6
|
return {
|
|
7
7
|
info: meta?.info ?? {},
|
|
@@ -10,7 +10,7 @@ async function getPDFMeta(data) {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
async function extractPDFText(data, { mergePages = false } = {}) {
|
|
13
|
-
const pdf = await getDocumentProxy(data);
|
|
13
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
14
14
|
const texts = await Promise.all(
|
|
15
15
|
Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
|
|
16
16
|
);
|
|
@@ -30,7 +30,7 @@ async function getPageText(pdf, pageNumber) {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
async function getImagesFromPage(data, pageNumber) {
|
|
33
|
-
const pdf = await getDocumentProxy(data);
|
|
33
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
34
34
|
const page = await pdf.getPage(pageNumber);
|
|
35
35
|
const operatorList = await page.getOperatorList();
|
|
36
36
|
const { OPS } = await getResolvedPDFJS();
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { BinaryData, PDFDocumentProxy, DocumentInitParameters } from 'pdfjs-dist/types/src/display/api';
|
|
2
2
|
import PDFJS from 'pdfjs-dist';
|
|
3
3
|
|
|
4
|
-
declare function getPDFMeta(data:
|
|
4
|
+
declare function getPDFMeta(data: BinaryData | PDFDocumentProxy): Promise<{
|
|
5
5
|
info: Record<string, any>;
|
|
6
6
|
metadata: Record<string, any>;
|
|
7
7
|
}>;
|
|
8
8
|
|
|
9
|
-
declare function extractPDFText(data:
|
|
9
|
+
declare function extractPDFText(data: BinaryData | PDFDocumentProxy, { mergePages }?: {
|
|
10
10
|
mergePages?: boolean;
|
|
11
11
|
}): Promise<{
|
|
12
12
|
totalPages: number;
|
|
13
13
|
text: string | string[];
|
|
14
14
|
}>;
|
|
15
15
|
|
|
16
|
-
declare function getImagesFromPage(data:
|
|
16
|
+
declare function getImagesFromPage(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<ArrayBuffer[]>;
|
|
17
17
|
|
|
18
18
|
interface UnPDFConfiguration {
|
|
19
19
|
/**
|
|
@@ -27,9 +27,17 @@ interface UnPDFConfiguration {
|
|
|
27
27
|
pdfjs?: () => Promise<typeof PDFJS>;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Returns a PDFDocumentProxy instance from a given binary data.
|
|
32
|
+
*
|
|
33
|
+
* Applies the following defaults:
|
|
34
|
+
* - `useWorkerFetch: false`
|
|
35
|
+
* - `isEvalSupported: false`
|
|
36
|
+
* - `useSystemFonts: true`
|
|
37
|
+
*/
|
|
38
|
+
declare function getDocumentProxy(data: BinaryData, options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
|
|
31
39
|
declare function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>;
|
|
32
40
|
declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
|
|
33
|
-
declare function resolvePDFJSImports(): Promise<void>;
|
|
41
|
+
declare function resolvePDFJSImports(pdfjs?: () => Promise<typeof PDFJS>): Promise<void>;
|
|
34
42
|
|
|
35
43
|
export { getImagesFromPage as a, getDocumentProxy as b, getResolvedPDFJS as c, defineUnPDFConfig as d, extractPDFText as e, getPDFMeta as g, resolvePDFJSImports as r };
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { BinaryData, PDFDocumentProxy, DocumentInitParameters } from 'pdfjs-dist/types/src/display/api';
|
|
2
2
|
import PDFJS from 'pdfjs-dist';
|
|
3
3
|
|
|
4
|
-
declare function getPDFMeta(data:
|
|
4
|
+
declare function getPDFMeta(data: BinaryData | PDFDocumentProxy): Promise<{
|
|
5
5
|
info: Record<string, any>;
|
|
6
6
|
metadata: Record<string, any>;
|
|
7
7
|
}>;
|
|
8
8
|
|
|
9
|
-
declare function extractPDFText(data:
|
|
9
|
+
declare function extractPDFText(data: BinaryData | PDFDocumentProxy, { mergePages }?: {
|
|
10
10
|
mergePages?: boolean;
|
|
11
11
|
}): Promise<{
|
|
12
12
|
totalPages: number;
|
|
13
13
|
text: string | string[];
|
|
14
14
|
}>;
|
|
15
15
|
|
|
16
|
-
declare function getImagesFromPage(data:
|
|
16
|
+
declare function getImagesFromPage(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<ArrayBuffer[]>;
|
|
17
17
|
|
|
18
18
|
interface UnPDFConfiguration {
|
|
19
19
|
/**
|
|
@@ -27,9 +27,17 @@ interface UnPDFConfiguration {
|
|
|
27
27
|
pdfjs?: () => Promise<typeof PDFJS>;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Returns a PDFDocumentProxy instance from a given binary data.
|
|
32
|
+
*
|
|
33
|
+
* Applies the following defaults:
|
|
34
|
+
* - `useWorkerFetch: false`
|
|
35
|
+
* - `isEvalSupported: false`
|
|
36
|
+
* - `useSystemFonts: true`
|
|
37
|
+
*/
|
|
38
|
+
declare function getDocumentProxy(data: BinaryData, options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
|
|
31
39
|
declare function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>;
|
|
32
40
|
declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
|
|
33
|
-
declare function resolvePDFJSImports(): Promise<void>;
|
|
41
|
+
declare function resolvePDFJSImports(pdfjs?: () => Promise<typeof PDFJS>): Promise<void>;
|
|
34
42
|
|
|
35
43
|
export { getImagesFromPage as a, getDocumentProxy as b, getResolvedPDFJS as c, defineUnPDFConfig as d, extractPDFText as e, getPDFMeta as g, resolvePDFJSImports as r };
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { BinaryData, PDFDocumentProxy, DocumentInitParameters } from 'pdfjs-dist/types/src/display/api';
|
|
2
2
|
import PDFJS from 'pdfjs-dist';
|
|
3
3
|
|
|
4
|
-
declare function getPDFMeta(data:
|
|
4
|
+
declare function getPDFMeta(data: BinaryData | PDFDocumentProxy): Promise<{
|
|
5
5
|
info: Record<string, any>;
|
|
6
6
|
metadata: Record<string, any>;
|
|
7
7
|
}>;
|
|
8
8
|
|
|
9
|
-
declare function extractPDFText(data:
|
|
9
|
+
declare function extractPDFText(data: BinaryData | PDFDocumentProxy, { mergePages }?: {
|
|
10
10
|
mergePages?: boolean;
|
|
11
11
|
}): Promise<{
|
|
12
12
|
totalPages: number;
|
|
13
13
|
text: string | string[];
|
|
14
14
|
}>;
|
|
15
15
|
|
|
16
|
-
declare function getImagesFromPage(data:
|
|
16
|
+
declare function getImagesFromPage(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<ArrayBuffer[]>;
|
|
17
17
|
|
|
18
18
|
interface UnPDFConfiguration {
|
|
19
19
|
/**
|
|
@@ -27,9 +27,17 @@ interface UnPDFConfiguration {
|
|
|
27
27
|
pdfjs?: () => Promise<typeof PDFJS>;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Returns a PDFDocumentProxy instance from a given binary data.
|
|
32
|
+
*
|
|
33
|
+
* Applies the following defaults:
|
|
34
|
+
* - `useWorkerFetch: false`
|
|
35
|
+
* - `isEvalSupported: false`
|
|
36
|
+
* - `useSystemFonts: true`
|
|
37
|
+
*/
|
|
38
|
+
declare function getDocumentProxy(data: BinaryData, options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
|
|
31
39
|
declare function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>;
|
|
32
40
|
declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
|
|
33
|
-
declare function resolvePDFJSImports(): Promise<void>;
|
|
41
|
+
declare function resolvePDFJSImports(pdfjs?: () => Promise<typeof PDFJS>): Promise<void>;
|
|
34
42
|
|
|
35
43
|
export { getImagesFromPage as a, getDocumentProxy as b, getResolvedPDFJS as c, defineUnPDFConfig as d, extractPDFText as e, getPDFMeta as g, resolvePDFJSImports as r };
|
package/package.json
CHANGED