unpdf 0.2.2 → 0.4.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 +30 -5
- package/dist/index.node.cjs +11 -59
- package/dist/index.node.d.cts +5 -11
- package/dist/index.node.d.mts +5 -11
- package/dist/index.node.d.ts +5 -11
- package/dist/index.node.mjs +11 -59
- package/dist/index.web.cjs +10 -4
- 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 +10 -4
- package/dist/index.worker.cjs +14 -0
- package/dist/index.worker.d.cts +8 -0
- package/dist/index.worker.d.mts +8 -0
- package/dist/index.worker.d.ts +8 -0
- package/dist/index.worker.mjs +10 -0
- package/dist/shared/unpdf.6c71f5a9.mjs +35 -0
- package/dist/shared/unpdf.85e02bd7.cjs +40 -0
- package/dist/shared/unpdf.bfd8aae1.d.cts +29 -0
- package/dist/shared/unpdf.bfd8aae1.d.mts +29 -0
- package/dist/shared/unpdf.bfd8aae1.d.ts +29 -0
- package/dist/shared/unpdf.d39bdc45.cjs +45 -0
- package/dist/shared/unpdf.e26c195e.mjs +42 -0
- package/package.json +12 -12
package/README.md
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
# unpdf
|
|
2
2
|
|
|
3
|
-
A collection of utilities to work with PDFs.
|
|
3
|
+
A collection of utilities to work with PDFs. Uses Mozilla's [PDF.js](https://github.com/mozilla/pdf.js) under the hood.
|
|
4
4
|
|
|
5
|
-
`unpdf` takes advantage of [export conditions](https://nodejs.org/api/packages.html#packages_conditional_exports) to
|
|
5
|
+
`unpdf` takes advantage of [export conditions](https://nodejs.org/api/packages.html#packages_conditional_exports) to circumvent build issues in serverless environments. PDF.js depends on the optional `canvas` module, which [doesn't work inside worker threads](https://github.com/Automattic/node-canvas/issues/1394).
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
This library is also intended as a modern alternative to the unmaintained [`pdf-parse`](https://www.npmjs.com/package/pdf-parse).
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- 🏗️ Conditional exports for Browser, Node and worker environments
|
|
12
|
+
- 💬 Extract text from PDFs
|
|
13
|
+
- 🧱 Use custom PDF.js build
|
|
11
14
|
|
|
12
15
|
## Installation
|
|
13
16
|
|
|
@@ -36,8 +39,30 @@ const { totalPages, info, metadata, text } = await decodePDFText(
|
|
|
36
39
|
)
|
|
37
40
|
```
|
|
38
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
|
+
|
|
39
58
|
## Methods
|
|
40
59
|
|
|
60
|
+
### `defineUnPDFConfig`
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>
|
|
64
|
+
```
|
|
65
|
+
|
|
41
66
|
### `decodePDFText`
|
|
42
67
|
|
|
43
68
|
```ts
|
package/dist/index.node.cjs
CHANGED
|
@@ -1,65 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const pdf = await getDocument({
|
|
6
|
-
data,
|
|
7
|
-
useWorkerFetch: false,
|
|
8
|
-
useSystemFonts: true,
|
|
9
|
-
isEvalSupported: false
|
|
10
|
-
}).promise;
|
|
11
|
-
return pdf;
|
|
12
|
-
}
|
|
13
|
-
async function getPDFJSImports() {
|
|
14
|
-
try {
|
|
15
|
-
const { default: mod } = await import('pdfjs-dist/legacy/build/pdf');
|
|
16
|
-
return mod;
|
|
17
|
-
} catch (e) {
|
|
18
|
-
console.error(e);
|
|
19
|
-
throw new Error(
|
|
20
|
-
"PDF.js is not available. Please run `pnpm add -D pdfjs-dist` and try again."
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
3
|
+
const image = require('./shared/unpdf.d39bdc45.cjs');
|
|
4
|
+
const utils = require('./shared/unpdf.85e02bd7.cjs');
|
|
24
5
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
totalPages: pdf.numPages,
|
|
34
|
-
info: meta?.info,
|
|
35
|
-
metadata: meta?.metadata?.getAll(),
|
|
36
|
-
text: mergePages ? filteredTexts.join("\n\n") : filteredTexts
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
async function getPageText(pdf, pageNumber) {
|
|
40
|
-
const page = await pdf.getPage(pageNumber);
|
|
41
|
-
const content = await page.getTextContent();
|
|
42
|
-
const items = content.items;
|
|
43
|
-
if (items.length === 0)
|
|
44
|
-
return;
|
|
45
|
-
return items.map(({ str }) => str).join("\n");
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async function getImagesFromPage(data, pageNumber) {
|
|
49
|
-
const pdf = await getDocumentProxy(data);
|
|
50
|
-
const page = await pdf.getPage(pageNumber);
|
|
51
|
-
const operatorList = await page.getOperatorList();
|
|
52
|
-
const { OPS } = await getPDFJSImports();
|
|
53
|
-
const images = [];
|
|
54
|
-
for (const op of operatorList.fnArray) {
|
|
55
|
-
if (op !== OPS.paintImageXObject)
|
|
56
|
-
continue;
|
|
57
|
-
const image = await page.objs.get(operatorList.argsArray[op][0]);
|
|
58
|
-
if (image.data)
|
|
59
|
-
images.push(image.data.buffer);
|
|
60
|
-
}
|
|
61
|
-
return images;
|
|
62
|
-
}
|
|
6
|
+
const decodePDFText = async (...args) => {
|
|
7
|
+
await utils.resolvePDFJSImports();
|
|
8
|
+
return await image.decodePDFText(...args);
|
|
9
|
+
};
|
|
10
|
+
const getImagesFromPage = async (...args) => {
|
|
11
|
+
await utils.resolvePDFJSImports();
|
|
12
|
+
return await image.getImagesFromPage(...args);
|
|
13
|
+
};
|
|
63
14
|
|
|
15
|
+
exports.defineUnPDFConfig = utils.defineUnPDFConfig;
|
|
64
16
|
exports.decodePDFText = decodePDFText;
|
|
65
17
|
exports.getImagesFromPage = getImagesFromPage;
|
package/dist/index.node.d.cts
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
metadata?: any;
|
|
5
|
-
text: string | string[];
|
|
6
|
-
}
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.bfd8aae1.cjs';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.bfd8aae1.cjs';
|
|
3
|
+
import 'pdfjs-dist';
|
|
7
4
|
|
|
8
|
-
declare
|
|
9
|
-
|
|
10
|
-
}): Promise<PDFContent>;
|
|
11
|
-
|
|
12
|
-
declare function getImagesFromPage(data: ArrayBuffer, pageNumber: number): Promise<ArrayBuffer[]>;
|
|
5
|
+
declare const decodePDFText: typeof decodePDFText$1;
|
|
6
|
+
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
|
13
7
|
|
|
14
8
|
export { decodePDFText, getImagesFromPage };
|
package/dist/index.node.d.mts
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
metadata?: any;
|
|
5
|
-
text: string | string[];
|
|
6
|
-
}
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.bfd8aae1.mjs';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.bfd8aae1.mjs';
|
|
3
|
+
import 'pdfjs-dist';
|
|
7
4
|
|
|
8
|
-
declare
|
|
9
|
-
|
|
10
|
-
}): Promise<PDFContent>;
|
|
11
|
-
|
|
12
|
-
declare function getImagesFromPage(data: ArrayBuffer, pageNumber: number): Promise<ArrayBuffer[]>;
|
|
5
|
+
declare const decodePDFText: typeof decodePDFText$1;
|
|
6
|
+
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
|
13
7
|
|
|
14
8
|
export { decodePDFText, getImagesFromPage };
|
package/dist/index.node.d.ts
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
metadata?: any;
|
|
5
|
-
text: string | string[];
|
|
6
|
-
}
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.bfd8aae1.js';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.bfd8aae1.js';
|
|
3
|
+
import 'pdfjs-dist';
|
|
7
4
|
|
|
8
|
-
declare
|
|
9
|
-
|
|
10
|
-
}): Promise<PDFContent>;
|
|
11
|
-
|
|
12
|
-
declare function getImagesFromPage(data: ArrayBuffer, pageNumber: number): Promise<ArrayBuffer[]>;
|
|
5
|
+
declare const decodePDFText: typeof decodePDFText$1;
|
|
6
|
+
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
|
13
7
|
|
|
14
8
|
export { decodePDFText, getImagesFromPage };
|
package/dist/index.node.mjs
CHANGED
|
@@ -1,62 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
data,
|
|
5
|
-
useWorkerFetch: false,
|
|
6
|
-
useSystemFonts: true,
|
|
7
|
-
isEvalSupported: false
|
|
8
|
-
}).promise;
|
|
9
|
-
return pdf;
|
|
10
|
-
}
|
|
11
|
-
async function getPDFJSImports() {
|
|
12
|
-
try {
|
|
13
|
-
const { default: mod } = await import('pdfjs-dist/legacy/build/pdf');
|
|
14
|
-
return mod;
|
|
15
|
-
} catch (e) {
|
|
16
|
-
console.error(e);
|
|
17
|
-
throw new Error(
|
|
18
|
-
"PDF.js is not available. Please run `pnpm add -D pdfjs-dist` and try again."
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.e26c195e.mjs';
|
|
2
|
+
import { r as resolvePDFJSImports } from './shared/unpdf.6c71f5a9.mjs';
|
|
3
|
+
export { d as defineUnPDFConfig } from './shared/unpdf.6c71f5a9.mjs';
|
|
22
4
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
totalPages: pdf.numPages,
|
|
32
|
-
info: meta?.info,
|
|
33
|
-
metadata: meta?.metadata?.getAll(),
|
|
34
|
-
text: mergePages ? filteredTexts.join("\n\n") : filteredTexts
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
async function getPageText(pdf, pageNumber) {
|
|
38
|
-
const page = await pdf.getPage(pageNumber);
|
|
39
|
-
const content = await page.getTextContent();
|
|
40
|
-
const items = content.items;
|
|
41
|
-
if (items.length === 0)
|
|
42
|
-
return;
|
|
43
|
-
return items.map(({ str }) => str).join("\n");
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async function getImagesFromPage(data, pageNumber) {
|
|
47
|
-
const pdf = await getDocumentProxy(data);
|
|
48
|
-
const page = await pdf.getPage(pageNumber);
|
|
49
|
-
const operatorList = await page.getOperatorList();
|
|
50
|
-
const { OPS } = await getPDFJSImports();
|
|
51
|
-
const images = [];
|
|
52
|
-
for (const op of operatorList.fnArray) {
|
|
53
|
-
if (op !== OPS.paintImageXObject)
|
|
54
|
-
continue;
|
|
55
|
-
const image = await page.objs.get(operatorList.argsArray[op][0]);
|
|
56
|
-
if (image.data)
|
|
57
|
-
images.push(image.data.buffer);
|
|
58
|
-
}
|
|
59
|
-
return images;
|
|
60
|
-
}
|
|
5
|
+
const decodePDFText = async (...args) => {
|
|
6
|
+
await resolvePDFJSImports();
|
|
7
|
+
return await decodePDFText$1(...args);
|
|
8
|
+
};
|
|
9
|
+
const getImagesFromPage = async (...args) => {
|
|
10
|
+
await resolvePDFJSImports();
|
|
11
|
+
return await getImagesFromPage$1(...args);
|
|
12
|
+
};
|
|
61
13
|
|
|
62
14
|
export { decodePDFText, getImagesFromPage };
|
package/dist/index.web.cjs
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
3
|
+
const image = require('./shared/unpdf.d39bdc45.cjs');
|
|
4
|
+
const utils = require('./shared/unpdf.85e02bd7.cjs');
|
|
5
|
+
|
|
6
|
+
const decodePDFText = async (...args) => {
|
|
7
|
+
await utils.resolvePDFJSImports();
|
|
8
|
+
return await image.decodePDFText(...args);
|
|
5
9
|
};
|
|
6
|
-
const getImagesFromPage = async () => {
|
|
7
|
-
|
|
10
|
+
const getImagesFromPage = async (...args) => {
|
|
11
|
+
await utils.resolvePDFJSImports();
|
|
12
|
+
return await image.getImagesFromPage(...args);
|
|
8
13
|
};
|
|
9
14
|
|
|
15
|
+
exports.defineUnPDFConfig = utils.defineUnPDFConfig;
|
|
10
16
|
exports.decodePDFText = decodePDFText;
|
|
11
17
|
exports.getImagesFromPage = getImagesFromPage;
|
package/dist/index.web.d.cts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.bfd8aae1.cjs';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.bfd8aae1.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 {
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.bfd8aae1.mjs';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.bfd8aae1.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 {
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.bfd8aae1.js';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.bfd8aae1.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,8 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.e26c195e.mjs';
|
|
2
|
+
import { r as resolvePDFJSImports } from './shared/unpdf.6c71f5a9.mjs';
|
|
3
|
+
export { d as defineUnPDFConfig } from './shared/unpdf.6c71f5a9.mjs';
|
|
4
|
+
|
|
5
|
+
const decodePDFText = async (...args) => {
|
|
6
|
+
await resolvePDFJSImports();
|
|
7
|
+
return await decodePDFText$1(...args);
|
|
3
8
|
};
|
|
4
|
-
const getImagesFromPage = async () => {
|
|
5
|
-
|
|
9
|
+
const getImagesFromPage = async (...args) => {
|
|
10
|
+
await resolvePDFJSImports();
|
|
11
|
+
return await getImagesFromPage$1(...args);
|
|
6
12
|
};
|
|
7
13
|
|
|
8
14
|
export { decodePDFText, getImagesFromPage };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const utils = require('./shared/unpdf.85e02bd7.cjs');
|
|
4
|
+
|
|
5
|
+
const decodePDFText = async () => {
|
|
6
|
+
throw new Error("Not implemented in worker context yet");
|
|
7
|
+
};
|
|
8
|
+
const getImagesFromPage = async () => {
|
|
9
|
+
throw new Error("Not implemented in worker context yet");
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
exports.defineUnPDFConfig = utils.defineUnPDFConfig;
|
|
13
|
+
exports.decodePDFText = decodePDFText;
|
|
14
|
+
exports.getImagesFromPage = getImagesFromPage;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.bfd8aae1.cjs';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.bfd8aae1.cjs';
|
|
3
|
+
import 'pdfjs-dist';
|
|
4
|
+
|
|
5
|
+
declare const decodePDFText: typeof decodePDFText$1;
|
|
6
|
+
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
|
7
|
+
|
|
8
|
+
export { decodePDFText, getImagesFromPage };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.bfd8aae1.mjs';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.bfd8aae1.mjs';
|
|
3
|
+
import 'pdfjs-dist';
|
|
4
|
+
|
|
5
|
+
declare const decodePDFText: typeof decodePDFText$1;
|
|
6
|
+
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
|
7
|
+
|
|
8
|
+
export { decodePDFText, getImagesFromPage };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { d as decodePDFText$1, g as getImagesFromPage$1 } from './shared/unpdf.bfd8aae1.js';
|
|
2
|
+
export { a as defineUnPDFConfig } from './shared/unpdf.bfd8aae1.js';
|
|
3
|
+
import 'pdfjs-dist';
|
|
4
|
+
|
|
5
|
+
declare const decodePDFText: typeof decodePDFText$1;
|
|
6
|
+
declare const getImagesFromPage: typeof getImagesFromPage$1;
|
|
7
|
+
|
|
8
|
+
export { decodePDFText, getImagesFromPage };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { d as defineUnPDFConfig } from './shared/unpdf.6c71f5a9.mjs';
|
|
2
|
+
|
|
3
|
+
const decodePDFText = async () => {
|
|
4
|
+
throw new Error("Not implemented in worker context yet");
|
|
5
|
+
};
|
|
6
|
+
const getImagesFromPage = async () => {
|
|
7
|
+
throw new Error("Not implemented in worker context yet");
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export { decodePDFText, getImagesFromPage };
|
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
resolvedModule = await pdfjs();
|
|
16
|
+
} catch (error) {
|
|
17
|
+
throw new Error("Resolving the PDF.js module failed. Please check your configuration.");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function getResolvedPDFJS() {
|
|
22
|
+
return resolvedModule;
|
|
23
|
+
}
|
|
24
|
+
async function resolvePDFJSImports() {
|
|
25
|
+
if (resolvedModule)
|
|
26
|
+
return;
|
|
27
|
+
try {
|
|
28
|
+
const { default: mod } = await import('pdfjs-dist');
|
|
29
|
+
resolvedModule = mod;
|
|
30
|
+
} catch (error) {
|
|
31
|
+
throw new Error("PDF.js is not available. Please add the package as a dependency.");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { getResolvedPDFJS as a, defineUnPDFConfig as d, getDocumentProxy as g, resolvePDFJSImports as r };
|
|
@@ -0,0 +1,40 @@
|
|
|
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
|
+
resolvedModule = await pdfjs();
|
|
18
|
+
} catch (error) {
|
|
19
|
+
throw new Error("Resolving the PDF.js module failed. Please check your configuration.");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function getResolvedPDFJS() {
|
|
24
|
+
return resolvedModule;
|
|
25
|
+
}
|
|
26
|
+
async function resolvePDFJSImports() {
|
|
27
|
+
if (resolvedModule)
|
|
28
|
+
return;
|
|
29
|
+
try {
|
|
30
|
+
const { default: mod } = await import('pdfjs-dist');
|
|
31
|
+
resolvedModule = mod;
|
|
32
|
+
} catch (error) {
|
|
33
|
+
throw new Error("PDF.js is not available. Please add the package as a dependency.");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
exports.defineUnPDFConfig = defineUnPDFConfig;
|
|
38
|
+
exports.getDocumentProxy = getDocumentProxy;
|
|
39
|
+
exports.getResolvedPDFJS = getResolvedPDFJS;
|
|
40
|
+
exports.resolvePDFJSImports = resolvePDFJSImports;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as 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 * as 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 * as 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,45 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const utils = require('./unpdf.85e02bd7.cjs');
|
|
4
|
+
|
|
5
|
+
async function decodePDFText(data, { mergePages = false } = {}) {
|
|
6
|
+
const pdf = await utils.getDocumentProxy(data);
|
|
7
|
+
const meta = await pdf.getMetadata().catch(() => null);
|
|
8
|
+
const texts = await Promise.all(
|
|
9
|
+
Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
|
|
10
|
+
);
|
|
11
|
+
const filteredTexts = texts.filter(Boolean);
|
|
12
|
+
return {
|
|
13
|
+
totalPages: pdf.numPages,
|
|
14
|
+
info: meta?.info,
|
|
15
|
+
metadata: meta?.metadata?.getAll(),
|
|
16
|
+
text: mergePages ? filteredTexts.join("\n\n") : filteredTexts
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
async function getPageText(pdf, pageNumber) {
|
|
20
|
+
const page = await pdf.getPage(pageNumber);
|
|
21
|
+
const content = await page.getTextContent();
|
|
22
|
+
const items = content.items;
|
|
23
|
+
if (items.length === 0)
|
|
24
|
+
return;
|
|
25
|
+
return items.map(({ str }) => str).join("\n");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function getImagesFromPage(data, pageNumber) {
|
|
29
|
+
const pdf = await utils.getDocumentProxy(data);
|
|
30
|
+
const page = await pdf.getPage(pageNumber);
|
|
31
|
+
const operatorList = await page.getOperatorList();
|
|
32
|
+
const { OPS } = utils.getResolvedPDFJS();
|
|
33
|
+
const images = [];
|
|
34
|
+
for (const op of operatorList.fnArray) {
|
|
35
|
+
if (op !== OPS.paintImageXObject)
|
|
36
|
+
continue;
|
|
37
|
+
const image = await page.objs.get(operatorList.argsArray[op][0]);
|
|
38
|
+
if (image.data)
|
|
39
|
+
images.push(image.data.buffer);
|
|
40
|
+
}
|
|
41
|
+
return images;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
exports.decodePDFText = decodePDFText;
|
|
45
|
+
exports.getImagesFromPage = getImagesFromPage;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { g as getDocumentProxy, a as getResolvedPDFJS } from './unpdf.6c71f5a9.mjs';
|
|
2
|
+
|
|
3
|
+
async function decodePDFText(data, { mergePages = false } = {}) {
|
|
4
|
+
const pdf = await getDocumentProxy(data);
|
|
5
|
+
const meta = await pdf.getMetadata().catch(() => null);
|
|
6
|
+
const texts = await Promise.all(
|
|
7
|
+
Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
|
|
8
|
+
);
|
|
9
|
+
const filteredTexts = texts.filter(Boolean);
|
|
10
|
+
return {
|
|
11
|
+
totalPages: pdf.numPages,
|
|
12
|
+
info: meta?.info,
|
|
13
|
+
metadata: meta?.metadata?.getAll(),
|
|
14
|
+
text: mergePages ? filteredTexts.join("\n\n") : filteredTexts
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
async function getPageText(pdf, pageNumber) {
|
|
18
|
+
const page = await pdf.getPage(pageNumber);
|
|
19
|
+
const content = await page.getTextContent();
|
|
20
|
+
const items = content.items;
|
|
21
|
+
if (items.length === 0)
|
|
22
|
+
return;
|
|
23
|
+
return items.map(({ str }) => str).join("\n");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function getImagesFromPage(data, pageNumber) {
|
|
27
|
+
const pdf = await getDocumentProxy(data);
|
|
28
|
+
const page = await pdf.getPage(pageNumber);
|
|
29
|
+
const operatorList = await page.getOperatorList();
|
|
30
|
+
const { OPS } = getResolvedPDFJS();
|
|
31
|
+
const images = [];
|
|
32
|
+
for (const op of operatorList.fnArray) {
|
|
33
|
+
if (op !== OPS.paintImageXObject)
|
|
34
|
+
continue;
|
|
35
|
+
const image = await page.objs.get(operatorList.argsArray[op][0]);
|
|
36
|
+
if (image.data)
|
|
37
|
+
images.push(image.data.buffer);
|
|
38
|
+
}
|
|
39
|
+
return images;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { decodePDFText as d, getImagesFromPage as g };
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unpdf",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"packageManager": "pnpm@8.6.12",
|
|
6
|
-
"description": "Utilities to work with PDFs",
|
|
6
|
+
"description": "Utilities to work with PDFs, like extracting text",
|
|
7
7
|
"author": "Johann Schopplich <pkg@johannschopplich.com>",
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"homepage": "https://github.com/johannschopplich/unpdf#readme",
|
|
@@ -21,16 +21,16 @@
|
|
|
21
21
|
"sideEffects": false,
|
|
22
22
|
"exports": {
|
|
23
23
|
"browser": "./dist/index.web.mjs",
|
|
24
|
-
"bun": "./dist/index.
|
|
25
|
-
"deno": "./dist/index.
|
|
26
|
-
"edge-light": "./dist/index.
|
|
27
|
-
"edge-routine": "./dist/index.
|
|
28
|
-
"lagon": "./dist/index.
|
|
29
|
-
"netlify": "./dist/index.
|
|
30
|
-
"react-native": "./dist/index.
|
|
31
|
-
"wintercg": "./dist/index.
|
|
32
|
-
"worker": "./dist/index.
|
|
33
|
-
"workerd": "./dist/index.
|
|
24
|
+
"bun": "./dist/index.worker.mjs",
|
|
25
|
+
"deno": "./dist/index.worker.mjs",
|
|
26
|
+
"edge-light": "./dist/index.worker.mjs",
|
|
27
|
+
"edge-routine": "./dist/index.worker.mjs",
|
|
28
|
+
"lagon": "./dist/index.worker.mjs",
|
|
29
|
+
"netlify": "./dist/index.worker.mjs",
|
|
30
|
+
"react-native": "./dist/index.worker.mjs",
|
|
31
|
+
"wintercg": "./dist/index.worker.mjs",
|
|
32
|
+
"worker": "./dist/index.worker.mjs",
|
|
33
|
+
"workerd": "./dist/index.worker.mjs",
|
|
34
34
|
"node": {
|
|
35
35
|
"types": "./dist/index.node.d.ts",
|
|
36
36
|
"import": "./dist/index.node.mjs",
|