unpdf 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-PRESENT Johann Schopplich
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # unpdf
2
+
3
+ A collection of utilities to work with PDFs.
4
+
5
+ `unpdf` takes advantage of [export conditions](https://nodejs.org/api/packages.html#packages_conditional_exports) to provide a minimal bundle size for the browser. As of now, the available methods are only supported in Node contexts.
6
+
7
+ **Why this package then?**
8
+
9
+ - To circumvent build issues in serverless environments, where the `canvas` package is used by `PDF.js` not supported.
10
+ - WIP and more to come.
11
+
12
+ ## Installation
13
+
14
+ Run the following command to add `unpdf` to your project.
15
+
16
+ ```bash
17
+ pnpm install unpdf # or npm or yarn
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ```ts
23
+ import { decodePDFText } from 'unpdf'
24
+
25
+ const pdfBuffer = await fetch('https://example.com/file.pdf').then(res => res.arrayBuffer())
26
+
27
+ const { totalPages, info, metadata, text } = await decodePDFText(
28
+ new Uint8Array(pdfBuffer), { mergePages: true }
29
+ )
30
+ ```
31
+
32
+ ## License
33
+
34
+ [MIT](./LICENSE) License © 2023-PRESENT [Johann Schopplich](https://github.com/johannschopplich)
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ async function decodePDFText(data, { mergePages = false } = {}) {
4
+ const { getDocument } = await getPDFJSImports();
5
+ const pdf = await getDocument({
6
+ data,
7
+ useWorkerFetch: false,
8
+ useSystemFonts: true,
9
+ isEvalSupported: false
10
+ }).promise;
11
+ const meta = await pdf.getMetadata().catch(() => null);
12
+ const texts = await Promise.all(
13
+ Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
14
+ );
15
+ const filteredTexts = texts.filter(Boolean);
16
+ return {
17
+ totalPages: pdf.numPages,
18
+ info: meta?.info,
19
+ metadata: meta?.metadata?.getAll(),
20
+ text: mergePages ? filteredTexts.join("\n\n") : filteredTexts
21
+ };
22
+ }
23
+ async function getPageText(pdf, pageNumber) {
24
+ const page = await pdf.getPage(pageNumber);
25
+ const content = await page.getTextContent();
26
+ const items = content.items;
27
+ if (items.length === 0)
28
+ return;
29
+ return items.map(({ str }) => str).join("\n");
30
+ }
31
+ async function getPDFJSImports() {
32
+ try {
33
+ const { default: mod } = await import('pdfjs-dist/legacy/build/pdf');
34
+ const { getDocument, version } = mod;
35
+ return { getDocument, version };
36
+ } catch (e) {
37
+ console.error(e);
38
+ throw new Error(
39
+ "PDF.js is not available. Please run `pnpm add -D pdfjs-dist` and try again."
40
+ );
41
+ }
42
+ }
43
+
44
+ exports.decodePDFText = decodePDFText;
@@ -0,0 +1 @@
1
+ export { d as decodePDFText } from './shared/unpdf.98b795c6.cjs';
@@ -0,0 +1 @@
1
+ export { d as decodePDFText } from './shared/unpdf.98b795c6.mjs';
@@ -0,0 +1 @@
1
+ export { d as decodePDFText } from './shared/unpdf.98b795c6.js';
@@ -0,0 +1,42 @@
1
+ async function decodePDFText(data, { mergePages = false } = {}) {
2
+ const { getDocument } = await getPDFJSImports();
3
+ const pdf = await getDocument({
4
+ data,
5
+ useWorkerFetch: false,
6
+ useSystemFonts: true,
7
+ isEvalSupported: false
8
+ }).promise;
9
+ const meta = await pdf.getMetadata().catch(() => null);
10
+ const texts = await Promise.all(
11
+ Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
12
+ );
13
+ const filteredTexts = texts.filter(Boolean);
14
+ return {
15
+ totalPages: pdf.numPages,
16
+ info: meta?.info,
17
+ metadata: meta?.metadata?.getAll(),
18
+ text: mergePages ? filteredTexts.join("\n\n") : filteredTexts
19
+ };
20
+ }
21
+ async function getPageText(pdf, pageNumber) {
22
+ const page = await pdf.getPage(pageNumber);
23
+ const content = await page.getTextContent();
24
+ const items = content.items;
25
+ if (items.length === 0)
26
+ return;
27
+ return items.map(({ str }) => str).join("\n");
28
+ }
29
+ async function getPDFJSImports() {
30
+ try {
31
+ const { default: mod } = await import('pdfjs-dist/legacy/build/pdf');
32
+ const { getDocument, version } = mod;
33
+ return { getDocument, version };
34
+ } catch (e) {
35
+ console.error(e);
36
+ throw new Error(
37
+ "PDF.js is not available. Please run `pnpm add -D pdfjs-dist` and try again."
38
+ );
39
+ }
40
+ }
41
+
42
+ export { decodePDFText };
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ async function decodePDFText(..._args) {
4
+ throw new Error("Not supported in browser context yet");
5
+ }
6
+
7
+ exports.decodePDFText = decodePDFText;
@@ -0,0 +1,5 @@
1
+ import { d as decodePDFText$1, P as PDFContent } from './shared/unpdf.98b795c6.cjs';
2
+
3
+ declare function decodePDFText(..._args: Parameters<typeof decodePDFText$1>): Promise<PDFContent>;
4
+
5
+ export { decodePDFText };
@@ -0,0 +1,5 @@
1
+ import { d as decodePDFText$1, P as PDFContent } from './shared/unpdf.98b795c6.mjs';
2
+
3
+ declare function decodePDFText(..._args: Parameters<typeof decodePDFText$1>): Promise<PDFContent>;
4
+
5
+ export { decodePDFText };
@@ -0,0 +1,5 @@
1
+ import { d as decodePDFText$1, P as PDFContent } from './shared/unpdf.98b795c6.js';
2
+
3
+ declare function decodePDFText(..._args: Parameters<typeof decodePDFText$1>): Promise<PDFContent>;
4
+
5
+ export { decodePDFText };
@@ -0,0 +1,5 @@
1
+ async function decodePDFText(..._args) {
2
+ throw new Error("Not supported in browser context yet");
3
+ }
4
+
5
+ export { decodePDFText };
@@ -0,0 +1,12 @@
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
+ export { PDFContent as P, decodePDFText as d };
@@ -0,0 +1,12 @@
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
+ export { PDFContent as P, decodePDFText as d };
@@ -0,0 +1,12 @@
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
+ export { PDFContent as P, decodePDFText as d };
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "unpdf",
3
+ "type": "module",
4
+ "version": "0.1.0",
5
+ "packageManager": "pnpm@8.6.12",
6
+ "description": "Utilities to work with PDFs",
7
+ "author": "Johann Schopplich <pkg@johannschopplich.com>",
8
+ "license": "MIT",
9
+ "homepage": "https://github.com/johannschopplich/unpdf#readme",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/johannschopplich/unpdf.git"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/johannschopplich/unpdf/issues"
16
+ },
17
+ "keywords": [
18
+ "pdf",
19
+ "parse"
20
+ ],
21
+ "sideEffects": false,
22
+ "exports": {
23
+ "browser": "./dist/index.web.mjs",
24
+ "bun": "./dist/index.web.mjs",
25
+ "deno": "./dist/index.web.mjs",
26
+ "edge-light": "./dist/index.web.mjs",
27
+ "edge-routine": "./dist/index.web.mjs",
28
+ "lagon": "./dist/index.web.mjs",
29
+ "netlify": "./dist/index.web.mjs",
30
+ "react-native": "./dist/index.web.mjs",
31
+ "wintercg": "./dist/index.web.mjs",
32
+ "worker": "./dist/index.web.mjs",
33
+ "workerd": "./dist/index.web.mjs",
34
+ "node": {
35
+ "types": "./dist/index.node.d.ts",
36
+ "import": "./dist/index.node.mjs",
37
+ "require": "./dist/index.node.cjs"
38
+ },
39
+ "types": "./dist/index.web.d.ts",
40
+ "import": "./dist/index.web.mjs",
41
+ "require": "./dist/index.web.cjs"
42
+ },
43
+ "main": "./dist/index.node.cjs",
44
+ "module": "./dist/index.web.mjs",
45
+ "browser": "./dist/index.web.mjs",
46
+ "types": "./dist/index.web.d.ts",
47
+ "files": [
48
+ "dist"
49
+ ],
50
+ "scripts": {
51
+ "build": "unbuild",
52
+ "dev": "unbuild --stub",
53
+ "lint": "eslint .",
54
+ "lint:fix": "eslint . --fix",
55
+ "release": "bumpp --commit --push --tag",
56
+ "test:types": "tsc --noEmit"
57
+ },
58
+ "peerDependencies": {
59
+ "pdfjs-dist": "^3.9.179"
60
+ },
61
+ "devDependencies": {
62
+ "@antfu/eslint-config": "^0.40.2",
63
+ "bumpp": "^9.1.1",
64
+ "eslint": "^8.47.0",
65
+ "pdfjs-dist": "^3.9.179",
66
+ "typescript": "^5.1.6",
67
+ "unbuild": "^2.0.0-rc.0"
68
+ }
69
+ }