unpdf 0.3.0 → 0.4.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 +23 -0
- package/dist/index.node.cjs +5 -3
- package/dist/index.node.d.cts +3 -1
- package/dist/index.node.d.mts +3 -1
- package/dist/index.node.d.ts +3 -1
- package/dist/index.node.mjs +5 -3
- package/dist/index.web.cjs +5 -3
- package/dist/index.web.d.cts +3 -1
- package/dist/index.web.d.mts +3 -1
- package/dist/index.web.d.ts +3 -1
- package/dist/index.web.mjs +5 -3
- package/dist/index.worker.cjs +3 -0
- package/dist/index.worker.d.cts +3 -1
- package/dist/index.worker.d.mts +3 -1
- package/dist/index.worker.d.ts +3 -1
- package/dist/index.worker.mjs +2 -0
- package/dist/shared/{unpdf.72a42ec0.mjs → unpdf.0f3aa1eb.mjs} +3 -40
- package/dist/shared/unpdf.54007d6f.cjs +41 -0
- package/dist/shared/unpdf.5b4a24ed.mjs +36 -0
- package/dist/shared/unpdf.72be7352.d.cts +29 -0
- package/dist/shared/unpdf.72be7352.d.mts +29 -0
- package/dist/shared/unpdf.72be7352.d.ts +29 -0
- package/dist/shared/{unpdf.970262b7.cjs → unpdf.ff73f2a9.cjs} +4 -43
- package/package.json +4 -1
- package/dist/shared/unpdf.5c39ffe2.d.cts +0 -14
- package/dist/shared/unpdf.5c39ffe2.d.mts +0 -14
- package/dist/shared/unpdf.5c39ffe2.d.ts +0 -14
package/README.md
CHANGED
|
@@ -10,6 +10,7 @@ This library is also intended as a modern alternative to the unmaintained [`pdf-
|
|
|
10
10
|
|
|
11
11
|
- 🏗️ Conditional exports for Browser, Node and worker environments
|
|
12
12
|
- 💬 Extract text from PDFs
|
|
13
|
+
- 🧱 Use custom PDF.js build
|
|
13
14
|
|
|
14
15
|
## Installation
|
|
15
16
|
|
|
@@ -38,8 +39,30 @@ const { totalPages, info, metadata, text } = await decodePDFText(
|
|
|
38
39
|
)
|
|
39
40
|
```
|
|
40
41
|
|
|
42
|
+
## Config
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
interface UnPDFConfiguration {
|
|
46
|
+
/**
|
|
47
|
+
* By default, UnPDF will use the latest version of PDF.js. If you want to
|
|
48
|
+
* use an older version or the legacy build, set a promise that resolves to
|
|
49
|
+
* the PDF.js module.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* () => import('pdfjs-dist/legacy/build/pdf.js')
|
|
53
|
+
*/
|
|
54
|
+
pdfjs?: () => typeof PDFJS
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
41
58
|
## Methods
|
|
42
59
|
|
|
60
|
+
### `defineUnPDFConfig`
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>
|
|
64
|
+
```
|
|
65
|
+
|
|
43
66
|
### `decodePDFText`
|
|
44
67
|
|
|
45
68
|
```ts
|
package/dist/index.node.cjs
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const image = require('./shared/unpdf.
|
|
3
|
+
const image = require('./shared/unpdf.ff73f2a9.cjs');
|
|
4
|
+
const utils = require('./shared/unpdf.54007d6f.cjs');
|
|
4
5
|
|
|
5
6
|
const decodePDFText = async (...args) => {
|
|
6
|
-
await
|
|
7
|
+
await utils.resolvePDFJSImports();
|
|
7
8
|
return await image.decodePDFText(...args);
|
|
8
9
|
};
|
|
9
10
|
const getImagesFromPage = async (...args) => {
|
|
10
|
-
await
|
|
11
|
+
await utils.resolvePDFJSImports();
|
|
11
12
|
return await image.getImagesFromPage(...args);
|
|
12
13
|
};
|
|
13
14
|
|
|
15
|
+
exports.defineUnPDFConfig = utils.defineUnPDFConfig;
|
|
14
16
|
exports.decodePDFText = decodePDFText;
|
|
15
17
|
exports.getImagesFromPage = getImagesFromPage;
|
package/dist/index.node.d.cts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.72be7352.cjs';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.72be7352.cjs';
|
|
3
|
+
import 'pdfjs-dist';
|
|
2
4
|
|
|
3
5
|
declare const decodePDFText: typeof decodePDFText$1;
|
|
4
6
|
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
package/dist/index.node.d.mts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.72be7352.mjs';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.72be7352.mjs';
|
|
3
|
+
import 'pdfjs-dist';
|
|
2
4
|
|
|
3
5
|
declare const decodePDFText: typeof decodePDFText$1;
|
|
4
6
|
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
package/dist/index.node.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.72be7352.js';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.72be7352.js';
|
|
3
|
+
import 'pdfjs-dist';
|
|
2
4
|
|
|
3
5
|
declare const decodePDFText: typeof decodePDFText$1;
|
|
4
6
|
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
package/dist/index.node.mjs
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.0f3aa1eb.mjs';
|
|
2
|
+
import { r as resolvePDFJSImports } from './shared/unpdf.5b4a24ed.mjs';
|
|
3
|
+
export { d as defineUnPDFConfig } from './shared/unpdf.5b4a24ed.mjs';
|
|
2
4
|
|
|
3
5
|
const decodePDFText = async (...args) => {
|
|
4
|
-
await
|
|
6
|
+
await resolvePDFJSImports();
|
|
5
7
|
return await decodePDFText$1(...args);
|
|
6
8
|
};
|
|
7
9
|
const getImagesFromPage = async (...args) => {
|
|
8
|
-
await
|
|
10
|
+
await resolvePDFJSImports();
|
|
9
11
|
return await getImagesFromPage$1(...args);
|
|
10
12
|
};
|
|
11
13
|
|
package/dist/index.web.cjs
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const image = require('./shared/unpdf.
|
|
3
|
+
const image = require('./shared/unpdf.ff73f2a9.cjs');
|
|
4
|
+
const utils = require('./shared/unpdf.54007d6f.cjs');
|
|
4
5
|
|
|
5
6
|
const decodePDFText = async (...args) => {
|
|
6
|
-
await
|
|
7
|
+
await utils.resolvePDFJSImports();
|
|
7
8
|
return await image.decodePDFText(...args);
|
|
8
9
|
};
|
|
9
10
|
const getImagesFromPage = async (...args) => {
|
|
10
|
-
await
|
|
11
|
+
await utils.resolvePDFJSImports();
|
|
11
12
|
return await image.getImagesFromPage(...args);
|
|
12
13
|
};
|
|
13
14
|
|
|
15
|
+
exports.defineUnPDFConfig = utils.defineUnPDFConfig;
|
|
14
16
|
exports.decodePDFText = decodePDFText;
|
|
15
17
|
exports.getImagesFromPage = getImagesFromPage;
|
package/dist/index.web.d.cts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.72be7352.cjs';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.72be7352.cjs';
|
|
3
|
+
import 'pdfjs-dist';
|
|
2
4
|
|
|
3
5
|
declare const decodePDFText: typeof decodePDFText$1;
|
|
4
6
|
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
package/dist/index.web.d.mts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.72be7352.mjs';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.72be7352.mjs';
|
|
3
|
+
import 'pdfjs-dist';
|
|
2
4
|
|
|
3
5
|
declare const decodePDFText: typeof decodePDFText$1;
|
|
4
6
|
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
package/dist/index.web.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.72be7352.js';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.72be7352.js';
|
|
3
|
+
import 'pdfjs-dist';
|
|
2
4
|
|
|
3
5
|
declare const decodePDFText: typeof decodePDFText$1;
|
|
4
6
|
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
package/dist/index.web.mjs
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.0f3aa1eb.mjs';
|
|
2
|
+
import { r as resolvePDFJSImports } from './shared/unpdf.5b4a24ed.mjs';
|
|
3
|
+
export { d as defineUnPDFConfig } from './shared/unpdf.5b4a24ed.mjs';
|
|
2
4
|
|
|
3
5
|
const decodePDFText = async (...args) => {
|
|
4
|
-
await
|
|
6
|
+
await resolvePDFJSImports();
|
|
5
7
|
return await decodePDFText$1(...args);
|
|
6
8
|
};
|
|
7
9
|
const getImagesFromPage = async (...args) => {
|
|
8
|
-
await
|
|
10
|
+
await resolvePDFJSImports();
|
|
9
11
|
return await getImagesFromPage$1(...args);
|
|
10
12
|
};
|
|
11
13
|
|
package/dist/index.worker.cjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const utils = require('./shared/unpdf.54007d6f.cjs');
|
|
4
|
+
|
|
3
5
|
const decodePDFText = async () => {
|
|
4
6
|
throw new Error("Not implemented in worker context yet");
|
|
5
7
|
};
|
|
@@ -7,5 +9,6 @@ const getImagesFromPage = async () => {
|
|
|
7
9
|
throw new Error("Not implemented in worker context yet");
|
|
8
10
|
};
|
|
9
11
|
|
|
12
|
+
exports.defineUnPDFConfig = utils.defineUnPDFConfig;
|
|
10
13
|
exports.decodePDFText = decodePDFText;
|
|
11
14
|
exports.getImagesFromPage = getImagesFromPage;
|
package/dist/index.worker.d.cts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.72be7352.cjs';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.72be7352.cjs';
|
|
3
|
+
import 'pdfjs-dist';
|
|
2
4
|
|
|
3
5
|
declare const decodePDFText: typeof decodePDFText$1;
|
|
4
6
|
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
package/dist/index.worker.d.mts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.72be7352.mjs';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.72be7352.mjs';
|
|
3
|
+
import 'pdfjs-dist';
|
|
2
4
|
|
|
3
5
|
declare const decodePDFText: typeof decodePDFText$1;
|
|
4
6
|
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
package/dist/index.worker.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.72be7352.js';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.72be7352.js';
|
|
3
|
+
import 'pdfjs-dist';
|
|
2
4
|
|
|
3
5
|
declare const decodePDFText: typeof decodePDFText$1;
|
|
4
6
|
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
package/dist/index.worker.mjs
CHANGED
|
@@ -1,41 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
async function getDocumentProxy(data) {
|
|
3
|
-
const { getDocument } = instance;
|
|
4
|
-
const pdf = await getDocument({
|
|
5
|
-
data,
|
|
6
|
-
useWorkerFetch: false,
|
|
7
|
-
useSystemFonts: true,
|
|
8
|
-
isEvalSupported: false
|
|
9
|
-
}).promise;
|
|
10
|
-
return pdf;
|
|
11
|
-
}
|
|
12
|
-
function getResolvedPDFJSInstance() {
|
|
13
|
-
return instance;
|
|
14
|
-
}
|
|
15
|
-
async function resolvePDFJSWebImports() {
|
|
16
|
-
if (instance)
|
|
17
|
-
return;
|
|
18
|
-
try {
|
|
19
|
-
const { default: mod } = await import('pdfjs-dist');
|
|
20
|
-
instance = mod;
|
|
21
|
-
} catch (error) {
|
|
22
|
-
throw new Error(
|
|
23
|
-
"PDF.js is not available. Please add the package as a dependency."
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
async function resolvePDFJSNodeImports() {
|
|
28
|
-
if (instance)
|
|
29
|
-
return;
|
|
30
|
-
try {
|
|
31
|
-
const { default: mod } = await import('pdfjs-dist/legacy/build/pdf');
|
|
32
|
-
instance = mod;
|
|
33
|
-
} catch (error) {
|
|
34
|
-
throw new Error(
|
|
35
|
-
"PDF.js is not available. Please add the package as a dependency."
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
1
|
+
import { g as getDocumentProxy, a as getResolvedPDFJS } from './unpdf.5b4a24ed.mjs';
|
|
39
2
|
|
|
40
3
|
async function decodePDFText(data, { mergePages = false } = {}) {
|
|
41
4
|
const pdf = await getDocumentProxy(data);
|
|
@@ -64,7 +27,7 @@ async function getImagesFromPage(data, pageNumber) {
|
|
|
64
27
|
const pdf = await getDocumentProxy(data);
|
|
65
28
|
const page = await pdf.getPage(pageNumber);
|
|
66
29
|
const operatorList = await page.getOperatorList();
|
|
67
|
-
const { OPS } =
|
|
30
|
+
const { OPS } = getResolvedPDFJS();
|
|
68
31
|
const images = [];
|
|
69
32
|
for (const op of operatorList.fnArray) {
|
|
70
33
|
if (op !== OPS.paintImageXObject)
|
|
@@ -76,4 +39,4 @@ async function getImagesFromPage(data, pageNumber) {
|
|
|
76
39
|
return images;
|
|
77
40
|
}
|
|
78
41
|
|
|
79
|
-
export {
|
|
42
|
+
export { decodePDFText as d, getImagesFromPage as g };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
let resolvedModule;
|
|
4
|
+
async function getDocumentProxy(data) {
|
|
5
|
+
const { getDocument } = getResolvedPDFJS();
|
|
6
|
+
const pdf = await getDocument({
|
|
7
|
+
data,
|
|
8
|
+
useWorkerFetch: false,
|
|
9
|
+
useSystemFonts: true,
|
|
10
|
+
isEvalSupported: false
|
|
11
|
+
}).promise;
|
|
12
|
+
return pdf;
|
|
13
|
+
}
|
|
14
|
+
async function defineUnPDFConfig({ pdfjs }) {
|
|
15
|
+
if (pdfjs) {
|
|
16
|
+
try {
|
|
17
|
+
const { default: mod } = await pdfjs();
|
|
18
|
+
resolvedModule = mod;
|
|
19
|
+
} catch (error) {
|
|
20
|
+
throw new Error("Resolving the PDF.js module failed. Please check your configuration.");
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function getResolvedPDFJS() {
|
|
25
|
+
return resolvedModule;
|
|
26
|
+
}
|
|
27
|
+
async function resolvePDFJSImports() {
|
|
28
|
+
if (resolvedModule)
|
|
29
|
+
return;
|
|
30
|
+
try {
|
|
31
|
+
const { default: mod } = await import('pdfjs-dist');
|
|
32
|
+
resolvedModule = mod;
|
|
33
|
+
} catch (error) {
|
|
34
|
+
throw new Error("PDF.js is not available. Please add the package as a dependency.");
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
exports.defineUnPDFConfig = defineUnPDFConfig;
|
|
39
|
+
exports.getDocumentProxy = getDocumentProxy;
|
|
40
|
+
exports.getResolvedPDFJS = getResolvedPDFJS;
|
|
41
|
+
exports.resolvePDFJSImports = resolvePDFJSImports;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
let resolvedModule;
|
|
2
|
+
async function getDocumentProxy(data) {
|
|
3
|
+
const { getDocument } = getResolvedPDFJS();
|
|
4
|
+
const pdf = await getDocument({
|
|
5
|
+
data,
|
|
6
|
+
useWorkerFetch: false,
|
|
7
|
+
useSystemFonts: true,
|
|
8
|
+
isEvalSupported: false
|
|
9
|
+
}).promise;
|
|
10
|
+
return pdf;
|
|
11
|
+
}
|
|
12
|
+
async function defineUnPDFConfig({ pdfjs }) {
|
|
13
|
+
if (pdfjs) {
|
|
14
|
+
try {
|
|
15
|
+
const { default: mod } = await pdfjs();
|
|
16
|
+
resolvedModule = mod;
|
|
17
|
+
} catch (error) {
|
|
18
|
+
throw new Error("Resolving the PDF.js module failed. Please check your configuration.");
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function getResolvedPDFJS() {
|
|
23
|
+
return resolvedModule;
|
|
24
|
+
}
|
|
25
|
+
async function resolvePDFJSImports() {
|
|
26
|
+
if (resolvedModule)
|
|
27
|
+
return;
|
|
28
|
+
try {
|
|
29
|
+
const { default: mod } = await import('pdfjs-dist');
|
|
30
|
+
resolvedModule = mod;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
throw new Error("PDF.js is not available. Please add the package as a dependency.");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { getResolvedPDFJS as a, defineUnPDFConfig as d, getDocumentProxy as g, resolvePDFJSImports as r };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import PDFJS from 'pdfjs-dist';
|
|
2
|
+
|
|
3
|
+
interface PDFContent {
|
|
4
|
+
totalPages: number;
|
|
5
|
+
info?: Record<string, any>;
|
|
6
|
+
metadata?: any;
|
|
7
|
+
text: string | string[];
|
|
8
|
+
}
|
|
9
|
+
interface UnPDFConfiguration {
|
|
10
|
+
/**
|
|
11
|
+
* By default, UnPDF will use the latest version of PDF.js. If you want to
|
|
12
|
+
* use an older version or the legacy build, set a promise that resolves to
|
|
13
|
+
* the PDF.js module.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* () => import('pdfjs-dist/legacy/build/pdf.js')
|
|
17
|
+
*/
|
|
18
|
+
pdfjs?: () => Promise<typeof PDFJS>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare function decodePDFText(data: ArrayBuffer, { mergePages }?: {
|
|
22
|
+
mergePages?: boolean;
|
|
23
|
+
}): Promise<PDFContent>;
|
|
24
|
+
|
|
25
|
+
declare function getImagesFromPage(data: ArrayBuffer, pageNumber: number): Promise<ArrayBuffer[]>;
|
|
26
|
+
|
|
27
|
+
declare function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>;
|
|
28
|
+
|
|
29
|
+
export { defineUnPDFConfig as a, decodePDFText as d, getImagesFromPage as g };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import PDFJS from 'pdfjs-dist';
|
|
2
|
+
|
|
3
|
+
interface PDFContent {
|
|
4
|
+
totalPages: number;
|
|
5
|
+
info?: Record<string, any>;
|
|
6
|
+
metadata?: any;
|
|
7
|
+
text: string | string[];
|
|
8
|
+
}
|
|
9
|
+
interface UnPDFConfiguration {
|
|
10
|
+
/**
|
|
11
|
+
* By default, UnPDF will use the latest version of PDF.js. If you want to
|
|
12
|
+
* use an older version or the legacy build, set a promise that resolves to
|
|
13
|
+
* the PDF.js module.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* () => import('pdfjs-dist/legacy/build/pdf.js')
|
|
17
|
+
*/
|
|
18
|
+
pdfjs?: () => Promise<typeof PDFJS>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare function decodePDFText(data: ArrayBuffer, { mergePages }?: {
|
|
22
|
+
mergePages?: boolean;
|
|
23
|
+
}): Promise<PDFContent>;
|
|
24
|
+
|
|
25
|
+
declare function getImagesFromPage(data: ArrayBuffer, pageNumber: number): Promise<ArrayBuffer[]>;
|
|
26
|
+
|
|
27
|
+
declare function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>;
|
|
28
|
+
|
|
29
|
+
export { defineUnPDFConfig as a, decodePDFText as d, getImagesFromPage as g };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import PDFJS from 'pdfjs-dist';
|
|
2
|
+
|
|
3
|
+
interface PDFContent {
|
|
4
|
+
totalPages: number;
|
|
5
|
+
info?: Record<string, any>;
|
|
6
|
+
metadata?: any;
|
|
7
|
+
text: string | string[];
|
|
8
|
+
}
|
|
9
|
+
interface UnPDFConfiguration {
|
|
10
|
+
/**
|
|
11
|
+
* By default, UnPDF will use the latest version of PDF.js. If you want to
|
|
12
|
+
* use an older version or the legacy build, set a promise that resolves to
|
|
13
|
+
* the PDF.js module.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* () => import('pdfjs-dist/legacy/build/pdf.js')
|
|
17
|
+
*/
|
|
18
|
+
pdfjs?: () => Promise<typeof PDFJS>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare function decodePDFText(data: ArrayBuffer, { mergePages }?: {
|
|
22
|
+
mergePages?: boolean;
|
|
23
|
+
}): Promise<PDFContent>;
|
|
24
|
+
|
|
25
|
+
declare function getImagesFromPage(data: ArrayBuffer, pageNumber: number): Promise<ArrayBuffer[]>;
|
|
26
|
+
|
|
27
|
+
declare function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>;
|
|
28
|
+
|
|
29
|
+
export { defineUnPDFConfig as a, decodePDFText as d, getImagesFromPage as g };
|
|
@@ -1,46 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
async function getDocumentProxy(data) {
|
|
5
|
-
const { getDocument } = instance;
|
|
6
|
-
const pdf = await getDocument({
|
|
7
|
-
data,
|
|
8
|
-
useWorkerFetch: false,
|
|
9
|
-
useSystemFonts: true,
|
|
10
|
-
isEvalSupported: false
|
|
11
|
-
}).promise;
|
|
12
|
-
return pdf;
|
|
13
|
-
}
|
|
14
|
-
function getResolvedPDFJSInstance() {
|
|
15
|
-
return instance;
|
|
16
|
-
}
|
|
17
|
-
async function resolvePDFJSWebImports() {
|
|
18
|
-
if (instance)
|
|
19
|
-
return;
|
|
20
|
-
try {
|
|
21
|
-
const { default: mod } = await import('pdfjs-dist');
|
|
22
|
-
instance = mod;
|
|
23
|
-
} catch (error) {
|
|
24
|
-
throw new Error(
|
|
25
|
-
"PDF.js is not available. Please add the package as a dependency."
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
async function resolvePDFJSNodeImports() {
|
|
30
|
-
if (instance)
|
|
31
|
-
return;
|
|
32
|
-
try {
|
|
33
|
-
const { default: mod } = await import('pdfjs-dist/legacy/build/pdf');
|
|
34
|
-
instance = mod;
|
|
35
|
-
} catch (error) {
|
|
36
|
-
throw new Error(
|
|
37
|
-
"PDF.js is not available. Please add the package as a dependency."
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
3
|
+
const utils = require('./unpdf.54007d6f.cjs');
|
|
41
4
|
|
|
42
5
|
async function decodePDFText(data, { mergePages = false } = {}) {
|
|
43
|
-
const pdf = await getDocumentProxy(data);
|
|
6
|
+
const pdf = await utils.getDocumentProxy(data);
|
|
44
7
|
const meta = await pdf.getMetadata().catch(() => null);
|
|
45
8
|
const texts = await Promise.all(
|
|
46
9
|
Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
|
|
@@ -63,10 +26,10 @@ async function getPageText(pdf, pageNumber) {
|
|
|
63
26
|
}
|
|
64
27
|
|
|
65
28
|
async function getImagesFromPage(data, pageNumber) {
|
|
66
|
-
const pdf = await getDocumentProxy(data);
|
|
29
|
+
const pdf = await utils.getDocumentProxy(data);
|
|
67
30
|
const page = await pdf.getPage(pageNumber);
|
|
68
31
|
const operatorList = await page.getOperatorList();
|
|
69
|
-
const { OPS } =
|
|
32
|
+
const { OPS } = utils.getResolvedPDFJS();
|
|
70
33
|
const images = [];
|
|
71
34
|
for (const op of operatorList.fnArray) {
|
|
72
35
|
if (op !== OPS.paintImageXObject)
|
|
@@ -80,5 +43,3 @@ async function getImagesFromPage(data, pageNumber) {
|
|
|
80
43
|
|
|
81
44
|
exports.decodePDFText = decodePDFText;
|
|
82
45
|
exports.getImagesFromPage = getImagesFromPage;
|
|
83
|
-
exports.resolvePDFJSNodeImports = resolvePDFJSNodeImports;
|
|
84
|
-
exports.resolvePDFJSWebImports = resolvePDFJSWebImports;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unpdf",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.1",
|
|
5
5
|
"packageManager": "pnpm@8.6.12",
|
|
6
6
|
"description": "Utilities to work with PDFs, like extracting text",
|
|
7
7
|
"author": "Johann Schopplich <pkg@johannschopplich.com>",
|
|
@@ -53,6 +53,7 @@
|
|
|
53
53
|
"lint": "eslint .",
|
|
54
54
|
"lint:fix": "eslint . --fix",
|
|
55
55
|
"release": "bumpp --commit --push --tag",
|
|
56
|
+
"test": "tsx test/index.ts",
|
|
56
57
|
"test:types": "tsc --noEmit"
|
|
57
58
|
},
|
|
58
59
|
"peerDependencies": {
|
|
@@ -60,9 +61,11 @@
|
|
|
60
61
|
},
|
|
61
62
|
"devDependencies": {
|
|
62
63
|
"@antfu/eslint-config": "^0.40.2",
|
|
64
|
+
"@types/node": "^20.4.10",
|
|
63
65
|
"bumpp": "^9.1.1",
|
|
64
66
|
"eslint": "^8.47.0",
|
|
65
67
|
"pdfjs-dist": "^3.9.179",
|
|
68
|
+
"tsx": "^3.12.7",
|
|
66
69
|
"typescript": "^5.1.6",
|
|
67
70
|
"unbuild": "^2.0.0-rc.0"
|
|
68
71
|
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
interface PDFContent {
|
|
2
|
-
totalPages: number;
|
|
3
|
-
info?: Record<string, any>;
|
|
4
|
-
metadata?: any;
|
|
5
|
-
text: string | string[];
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
declare function decodePDFText(data: ArrayBuffer, { mergePages }?: {
|
|
9
|
-
mergePages?: boolean;
|
|
10
|
-
}): Promise<PDFContent>;
|
|
11
|
-
|
|
12
|
-
declare function getImagesFromPage(data: ArrayBuffer, pageNumber: number): Promise<ArrayBuffer[]>;
|
|
13
|
-
|
|
14
|
-
export { decodePDFText as d, getImagesFromPage as g };
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
interface PDFContent {
|
|
2
|
-
totalPages: number;
|
|
3
|
-
info?: Record<string, any>;
|
|
4
|
-
metadata?: any;
|
|
5
|
-
text: string | string[];
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
declare function decodePDFText(data: ArrayBuffer, { mergePages }?: {
|
|
9
|
-
mergePages?: boolean;
|
|
10
|
-
}): Promise<PDFContent>;
|
|
11
|
-
|
|
12
|
-
declare function getImagesFromPage(data: ArrayBuffer, pageNumber: number): Promise<ArrayBuffer[]>;
|
|
13
|
-
|
|
14
|
-
export { decodePDFText as d, getImagesFromPage as g };
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
interface PDFContent {
|
|
2
|
-
totalPages: number;
|
|
3
|
-
info?: Record<string, any>;
|
|
4
|
-
metadata?: any;
|
|
5
|
-
text: string | string[];
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
declare function decodePDFText(data: ArrayBuffer, { mergePages }?: {
|
|
9
|
-
mergePages?: boolean;
|
|
10
|
-
}): Promise<PDFContent>;
|
|
11
|
-
|
|
12
|
-
declare function getImagesFromPage(data: ArrayBuffer, pageNumber: number): Promise<ArrayBuffer[]>;
|
|
13
|
-
|
|
14
|
-
export { decodePDFText as d, getImagesFromPage as g };
|