unpdf 0.5.0 → 0.5.2

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 CHANGED
@@ -1,15 +1,15 @@
1
1
  # unpdf
2
2
 
3
- A collection of utilities to work with PDFs. Uses Mozilla's [PDF.js](https://github.com/mozilla/pdf.js) under the hood.
3
+ A collection of utilities to work with PDFs. Uses Mozilla's [PDF.js](https://github.com/mozilla/pdf.js) under the hood and lazily initializes the library.
4
4
 
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).
5
+ `unpdf` takes advantage of [export conditions](https://nodejs.org/api/packages.html#packages_conditional_exports) to circumvent build issues in serverless environments. For example, 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
- This library is also intended as a modern alternative to the unmaintained [`pdf-parse`](https://www.npmjs.com/package/pdf-parse).
7
+ This library is also intended as a modern alternative to the unmaintained but still popular [`pdf-parse`](https://www.npmjs.com/package/pdf-parse).
8
8
 
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
@@ -18,13 +18,13 @@ Run the following command to add `unpdf` to your project.
18
18
 
19
19
  ```bash
20
20
  # pnpm
21
- pnpm add -D unpdf
21
+ pnpm add unpdf
22
22
 
23
23
  # npm
24
- npm install -D unpdf
24
+ npm install unpdf
25
25
 
26
26
  # yarn
27
- yarn add -D unpdf
27
+ yarn add unpdf
28
28
  ```
29
29
 
30
30
  ## Usage
@@ -55,6 +55,14 @@ defineUnPDFConfig({
55
55
  // …
56
56
  ```
57
57
 
58
+ ### Access the PDF.js Module
59
+
60
+ ```ts
61
+ import { getResolvedPDFJS } from 'unpdf'
62
+
63
+ const { version } = await getResolvedPDFJS()
64
+ ```
65
+
58
66
  ## Config
59
67
 
60
68
  ```ts
@@ -75,16 +83,28 @@ interface UnPDFConfiguration {
75
83
 
76
84
  ### `defineUnPDFConfig`
77
85
 
86
+ Define a custom PDF.js module, like the legacy build.
87
+
78
88
  ```ts
79
- function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>
89
+ function defineUnPDFConfig(config: UnPDFConfiguration): Promise<void>
90
+ ```
91
+
92
+ ### `getResolvedPDFJS`
93
+
94
+ Returns the resolved PDF.js module.
95
+
96
+ ```ts
97
+ function getResolvedPDFJS(): Promise<typeof import('pdfjs-dist')>
80
98
  ```
81
99
 
82
100
  ### `getPDFMeta`
83
101
 
84
102
  ```ts
85
- function getPDFMeta(data: ArrayBuffer): Promise<{
103
+ function getPDFMeta(
104
+ data: BinaryData | PDFDocumentProxy
105
+ ): Promise<{
86
106
  info: Record<string, any>
87
- metadata: any
107
+ metadata: Record<string, any>
88
108
  }>
89
109
  ```
90
110
 
@@ -92,7 +112,7 @@ function getPDFMeta(data: ArrayBuffer): Promise<{
92
112
 
93
113
  ```ts
94
114
  function extractPDFText(
95
- data: ArrayBuffer,
115
+ data: BinaryData | PDFDocumentProxy,
96
116
  { mergePages }?: { mergePages?: boolean }
97
117
  ): Promise<{
98
118
  totalPages: number
@@ -104,7 +124,7 @@ function extractPDFText(
104
124
 
105
125
  ```ts
106
126
  function getImagesFromPage(
107
- data: ArrayBuffer,
127
+ data: BinaryData | PDFDocumentProxy,
108
128
  pageNumber: number
109
129
  ): Promise<ArrayBuffer[]>
110
130
  ```
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const image = require('./shared/unpdf.e023f326.cjs');
4
- const utils = require('./shared/unpdf.54007d6f.cjs');
3
+ const image = require('./shared/unpdf.6502e2c8.cjs');
4
+ const utils = require('./shared/unpdf.9b5cc790.cjs');
5
5
 
6
6
  const getPDFMeta = async (...args) => {
7
7
  await utils.resolvePDFJSImports();
@@ -17,6 +17,9 @@ const getImagesFromPage = async (...args) => {
17
17
  };
18
18
 
19
19
  exports.defineUnPDFConfig = utils.defineUnPDFConfig;
20
+ exports.getDocumentProxy = utils.getDocumentProxy;
21
+ exports.getResolvedPDFJS = utils.getResolvedPDFJS;
22
+ exports.resolvePDFJSImports = utils.resolvePDFJSImports;
20
23
  exports.extractPDFText = extractPDFText;
21
24
  exports.getImagesFromPage = getImagesFromPage;
22
25
  exports.getPDFMeta = getPDFMeta;
@@ -1,5 +1,6 @@
1
- import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.842960e8.cjs';
2
- export { d as defineUnPDFConfig } from './shared/unpdf.842960e8.cjs';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.0c92a4fe.cjs';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.0c92a4fe.cjs';
3
+ import 'pdfjs-dist/types/src/display/api';
3
4
  import 'pdfjs-dist';
4
5
 
5
6
  declare const getPDFMeta: typeof getPDFMeta$1;
@@ -1,5 +1,6 @@
1
- import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.842960e8.mjs';
2
- export { d as defineUnPDFConfig } from './shared/unpdf.842960e8.mjs';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.0c92a4fe.mjs';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.0c92a4fe.mjs';
3
+ import 'pdfjs-dist/types/src/display/api';
3
4
  import 'pdfjs-dist';
4
5
 
5
6
  declare const getPDFMeta: typeof getPDFMeta$1;
@@ -1,5 +1,6 @@
1
- import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.842960e8.js';
2
- export { d as defineUnPDFConfig } from './shared/unpdf.842960e8.js';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.0c92a4fe.js';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.0c92a4fe.js';
3
+ import 'pdfjs-dist/types/src/display/api';
3
4
  import 'pdfjs-dist';
4
5
 
5
6
  declare const getPDFMeta: typeof getPDFMeta$1;
@@ -1,6 +1,6 @@
1
- import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.0db47641.mjs';
2
- import { r as resolvePDFJSImports } from './shared/unpdf.5b4a24ed.mjs';
3
- export { d as defineUnPDFConfig } from './shared/unpdf.5b4a24ed.mjs';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.e3e60ef4.mjs';
2
+ import { r as resolvePDFJSImports } from './shared/unpdf.f801f444.mjs';
3
+ export { d as defineUnPDFConfig, g as getDocumentProxy, a as getResolvedPDFJS } from './shared/unpdf.f801f444.mjs';
4
4
 
5
5
  const getPDFMeta = async (...args) => {
6
6
  await resolvePDFJSImports();
@@ -15,4 +15,4 @@ const getImagesFromPage = async (...args) => {
15
15
  return await getImagesFromPage$1(...args);
16
16
  };
17
17
 
18
- export { extractPDFText, getImagesFromPage, getPDFMeta };
18
+ export { extractPDFText, getImagesFromPage, getPDFMeta, resolvePDFJSImports };
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const image = require('./shared/unpdf.e023f326.cjs');
4
- const utils = require('./shared/unpdf.54007d6f.cjs');
3
+ const image = require('./shared/unpdf.6502e2c8.cjs');
4
+ const utils = require('./shared/unpdf.9b5cc790.cjs');
5
5
 
6
6
  const getPDFMeta = async (...args) => {
7
7
  await utils.resolvePDFJSImports();
@@ -17,6 +17,9 @@ const getImagesFromPage = async (...args) => {
17
17
  };
18
18
 
19
19
  exports.defineUnPDFConfig = utils.defineUnPDFConfig;
20
+ exports.getDocumentProxy = utils.getDocumentProxy;
21
+ exports.getResolvedPDFJS = utils.getResolvedPDFJS;
22
+ exports.resolvePDFJSImports = utils.resolvePDFJSImports;
20
23
  exports.extractPDFText = extractPDFText;
21
24
  exports.getImagesFromPage = getImagesFromPage;
22
25
  exports.getPDFMeta = getPDFMeta;
@@ -1,5 +1,6 @@
1
- import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.842960e8.cjs';
2
- export { d as defineUnPDFConfig } from './shared/unpdf.842960e8.cjs';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.0c92a4fe.cjs';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.0c92a4fe.cjs';
3
+ import 'pdfjs-dist/types/src/display/api';
3
4
  import 'pdfjs-dist';
4
5
 
5
6
  declare const getPDFMeta: typeof getPDFMeta$1;
@@ -1,5 +1,6 @@
1
- import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.842960e8.mjs';
2
- export { d as defineUnPDFConfig } from './shared/unpdf.842960e8.mjs';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.0c92a4fe.mjs';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.0c92a4fe.mjs';
3
+ import 'pdfjs-dist/types/src/display/api';
3
4
  import 'pdfjs-dist';
4
5
 
5
6
  declare const getPDFMeta: typeof getPDFMeta$1;
@@ -1,5 +1,6 @@
1
- import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.842960e8.js';
2
- export { d as defineUnPDFConfig } from './shared/unpdf.842960e8.js';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.0c92a4fe.js';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.0c92a4fe.js';
3
+ import 'pdfjs-dist/types/src/display/api';
3
4
  import 'pdfjs-dist';
4
5
 
5
6
  declare const getPDFMeta: typeof getPDFMeta$1;
@@ -1,6 +1,6 @@
1
- import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.0db47641.mjs';
2
- import { r as resolvePDFJSImports } from './shared/unpdf.5b4a24ed.mjs';
3
- export { d as defineUnPDFConfig } from './shared/unpdf.5b4a24ed.mjs';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.e3e60ef4.mjs';
2
+ import { r as resolvePDFJSImports } from './shared/unpdf.f801f444.mjs';
3
+ export { d as defineUnPDFConfig, g as getDocumentProxy, a as getResolvedPDFJS } from './shared/unpdf.f801f444.mjs';
4
4
 
5
5
  const getPDFMeta = async (...args) => {
6
6
  await resolvePDFJSImports();
@@ -15,4 +15,4 @@ const getImagesFromPage = async (...args) => {
15
15
  return await getImagesFromPage$1(...args);
16
16
  };
17
17
 
18
- export { extractPDFText, getImagesFromPage, getPDFMeta };
18
+ export { extractPDFText, getImagesFromPage, getPDFMeta, resolvePDFJSImports };
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const utils = require('./shared/unpdf.54007d6f.cjs');
3
+ const utils = require('./shared/unpdf.9b5cc790.cjs');
4
4
 
5
5
  const getPDFMeta = async () => {
6
6
  throw new Error("Not implemented in worker context yet");
@@ -13,6 +13,9 @@ const getImagesFromPage = async () => {
13
13
  };
14
14
 
15
15
  exports.defineUnPDFConfig = utils.defineUnPDFConfig;
16
+ exports.getDocumentProxy = utils.getDocumentProxy;
17
+ exports.getResolvedPDFJS = utils.getResolvedPDFJS;
18
+ exports.resolvePDFJSImports = utils.resolvePDFJSImports;
16
19
  exports.extractPDFText = extractPDFText;
17
20
  exports.getImagesFromPage = getImagesFromPage;
18
21
  exports.getPDFMeta = getPDFMeta;
@@ -1,5 +1,6 @@
1
- import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.842960e8.cjs';
2
- export { d as defineUnPDFConfig } from './shared/unpdf.842960e8.cjs';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.0c92a4fe.cjs';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.0c92a4fe.cjs';
3
+ import 'pdfjs-dist/types/src/display/api';
3
4
  import 'pdfjs-dist';
4
5
 
5
6
  declare const getPDFMeta: typeof getPDFMeta$1;
@@ -1,5 +1,6 @@
1
- import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.842960e8.mjs';
2
- export { d as defineUnPDFConfig } from './shared/unpdf.842960e8.mjs';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.0c92a4fe.mjs';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.0c92a4fe.mjs';
3
+ import 'pdfjs-dist/types/src/display/api';
3
4
  import 'pdfjs-dist';
4
5
 
5
6
  declare const getPDFMeta: typeof getPDFMeta$1;
@@ -1,5 +1,6 @@
1
- import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.842960e8.js';
2
- export { d as defineUnPDFConfig } from './shared/unpdf.842960e8.js';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.0c92a4fe.js';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.0c92a4fe.js';
3
+ import 'pdfjs-dist/types/src/display/api';
3
4
  import 'pdfjs-dist';
4
5
 
5
6
  declare const getPDFMeta: typeof getPDFMeta$1;
@@ -1,4 +1,4 @@
1
- export { d as defineUnPDFConfig } from './shared/unpdf.5b4a24ed.mjs';
1
+ export { d as defineUnPDFConfig, g as getDocumentProxy, a as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.f801f444.mjs';
2
2
 
3
3
  const getPDFMeta = async () => {
4
4
  throw new Error("Not implemented in worker context yet");
@@ -0,0 +1,43 @@
1
+ import { BinaryData, PDFDocumentProxy, DocumentInitParameters } from 'pdfjs-dist/types/src/display/api';
2
+ import PDFJS from 'pdfjs-dist';
3
+
4
+ declare function getPDFMeta(data: BinaryData | PDFDocumentProxy): Promise<{
5
+ info: Record<string, any>;
6
+ metadata: Record<string, any>;
7
+ }>;
8
+
9
+ declare function extractPDFText(data: BinaryData | PDFDocumentProxy, { mergePages }?: {
10
+ mergePages?: boolean;
11
+ }): Promise<{
12
+ totalPages: number;
13
+ text: string | string[];
14
+ }>;
15
+
16
+ declare function getImagesFromPage(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<ArrayBuffer[]>;
17
+
18
+ interface UnPDFConfiguration {
19
+ /**
20
+ * By default, UnPDF will use the latest version of PDF.js. If you want to
21
+ * use an older version or the legacy build, set a promise that resolves to
22
+ * the PDF.js module.
23
+ *
24
+ * @example
25
+ * () => import('pdfjs-dist/legacy/build/pdf.js')
26
+ */
27
+ pdfjs?: () => Promise<typeof PDFJS>;
28
+ }
29
+
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>;
39
+ declare function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>;
40
+ declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
41
+ declare function resolvePDFJSImports(): Promise<void>;
42
+
43
+ export { getImagesFromPage as a, getDocumentProxy as b, getResolvedPDFJS as c, defineUnPDFConfig as d, extractPDFText as e, getPDFMeta as g, resolvePDFJSImports as r };
@@ -0,0 +1,43 @@
1
+ import { BinaryData, PDFDocumentProxy, DocumentInitParameters } from 'pdfjs-dist/types/src/display/api';
2
+ import PDFJS from 'pdfjs-dist';
3
+
4
+ declare function getPDFMeta(data: BinaryData | PDFDocumentProxy): Promise<{
5
+ info: Record<string, any>;
6
+ metadata: Record<string, any>;
7
+ }>;
8
+
9
+ declare function extractPDFText(data: BinaryData | PDFDocumentProxy, { mergePages }?: {
10
+ mergePages?: boolean;
11
+ }): Promise<{
12
+ totalPages: number;
13
+ text: string | string[];
14
+ }>;
15
+
16
+ declare function getImagesFromPage(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<ArrayBuffer[]>;
17
+
18
+ interface UnPDFConfiguration {
19
+ /**
20
+ * By default, UnPDF will use the latest version of PDF.js. If you want to
21
+ * use an older version or the legacy build, set a promise that resolves to
22
+ * the PDF.js module.
23
+ *
24
+ * @example
25
+ * () => import('pdfjs-dist/legacy/build/pdf.js')
26
+ */
27
+ pdfjs?: () => Promise<typeof PDFJS>;
28
+ }
29
+
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>;
39
+ declare function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>;
40
+ declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
41
+ declare function resolvePDFJSImports(): Promise<void>;
42
+
43
+ export { getImagesFromPage as a, getDocumentProxy as b, getResolvedPDFJS as c, defineUnPDFConfig as d, extractPDFText as e, getPDFMeta as g, resolvePDFJSImports as r };
@@ -0,0 +1,43 @@
1
+ import { BinaryData, PDFDocumentProxy, DocumentInitParameters } from 'pdfjs-dist/types/src/display/api';
2
+ import PDFJS from 'pdfjs-dist';
3
+
4
+ declare function getPDFMeta(data: BinaryData | PDFDocumentProxy): Promise<{
5
+ info: Record<string, any>;
6
+ metadata: Record<string, any>;
7
+ }>;
8
+
9
+ declare function extractPDFText(data: BinaryData | PDFDocumentProxy, { mergePages }?: {
10
+ mergePages?: boolean;
11
+ }): Promise<{
12
+ totalPages: number;
13
+ text: string | string[];
14
+ }>;
15
+
16
+ declare function getImagesFromPage(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<ArrayBuffer[]>;
17
+
18
+ interface UnPDFConfiguration {
19
+ /**
20
+ * By default, UnPDF will use the latest version of PDF.js. If you want to
21
+ * use an older version or the legacy build, set a promise that resolves to
22
+ * the PDF.js module.
23
+ *
24
+ * @example
25
+ * () => import('pdfjs-dist/legacy/build/pdf.js')
26
+ */
27
+ pdfjs?: () => Promise<typeof PDFJS>;
28
+ }
29
+
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>;
39
+ declare function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>;
40
+ declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
41
+ declare function resolvePDFJSImports(): Promise<void>;
42
+
43
+ export { getImagesFromPage as a, getDocumentProxy as b, getResolvedPDFJS as c, defineUnPDFConfig as d, extractPDFText as e, getPDFMeta as g, resolvePDFJSImports as r };
@@ -1,18 +1,18 @@
1
1
  'use strict';
2
2
 
3
- const utils = require('./unpdf.54007d6f.cjs');
3
+ const utils = require('./unpdf.9b5cc790.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
- info: meta?.info,
10
- metadata: meta?.metadata?.getAll()
9
+ info: meta?.info ?? {},
10
+ metadata: meta?.metadata?.getAll() ?? {}
11
11
  };
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,10 +32,10 @@ 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
- const { OPS } = utils.getResolvedPDFJS();
38
+ const { OPS } = await utils.getResolvedPDFJS();
39
39
  const images = [];
40
40
  for (const op of operatorList.fnArray) {
41
41
  if (op !== OPS.paintImageXObject)
@@ -1,13 +1,20 @@
1
1
  'use strict';
2
2
 
3
3
  let resolvedModule;
4
- async function getDocumentProxy(data) {
5
- const { getDocument } = getResolvedPDFJS();
4
+ async function getDocumentProxy(data, options = {}) {
5
+ const {
6
+ useWorkerFetch = false,
7
+ isEvalSupported = false,
8
+ useSystemFonts = true,
9
+ ...rest
10
+ } = options;
11
+ const { getDocument } = await getResolvedPDFJS();
6
12
  const pdf = await getDocument({
7
13
  data,
8
- useWorkerFetch: false,
9
- useSystemFonts: true,
10
- isEvalSupported: false
14
+ useWorkerFetch,
15
+ useSystemFonts,
16
+ isEvalSupported,
17
+ ...rest
11
18
  }).promise;
12
19
  return pdf;
13
20
  }
@@ -21,7 +28,9 @@ async function defineUnPDFConfig({ pdfjs }) {
21
28
  }
22
29
  }
23
30
  }
24
- function getResolvedPDFJS() {
31
+ async function getResolvedPDFJS() {
32
+ if (!resolvedModule)
33
+ await resolvePDFJSImports();
25
34
  return resolvedModule;
26
35
  }
27
36
  async function resolvePDFJSImports() {
@@ -34,8 +43,12 @@ 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
50
  exports.defineUnPDFConfig = defineUnPDFConfig;
39
51
  exports.getDocumentProxy = getDocumentProxy;
40
52
  exports.getResolvedPDFJS = getResolvedPDFJS;
53
+ exports.isPDFDocumentProxy = isPDFDocumentProxy;
41
54
  exports.resolvePDFJSImports = resolvePDFJSImports;
@@ -1,16 +1,16 @@
1
- import { g as getDocumentProxy, a as getResolvedPDFJS } from './unpdf.5b4a24ed.mjs';
1
+ import { i as isPDFDocumentProxy, g as getDocumentProxy, a as getResolvedPDFJS } from './unpdf.f801f444.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
- info: meta?.info,
8
- metadata: meta?.metadata?.getAll()
7
+ info: meta?.info ?? {},
8
+ metadata: meta?.metadata?.getAll() ?? {}
9
9
  };
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,10 +30,10 @@ 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
- const { OPS } = getResolvedPDFJS();
36
+ const { OPS } = await getResolvedPDFJS();
37
37
  const images = [];
38
38
  for (const op of operatorList.fnArray) {
39
39
  if (op !== OPS.paintImageXObject)
@@ -1,11 +1,18 @@
1
1
  let resolvedModule;
2
- async function getDocumentProxy(data) {
3
- const { getDocument } = getResolvedPDFJS();
2
+ async function getDocumentProxy(data, options = {}) {
3
+ const {
4
+ useWorkerFetch = false,
5
+ isEvalSupported = false,
6
+ useSystemFonts = true,
7
+ ...rest
8
+ } = options;
9
+ const { getDocument } = await getResolvedPDFJS();
4
10
  const pdf = await getDocument({
5
11
  data,
6
- useWorkerFetch: false,
7
- useSystemFonts: true,
8
- isEvalSupported: false
12
+ useWorkerFetch,
13
+ useSystemFonts,
14
+ isEvalSupported,
15
+ ...rest
9
16
  }).promise;
10
17
  return pdf;
11
18
  }
@@ -19,7 +26,9 @@ async function defineUnPDFConfig({ pdfjs }) {
19
26
  }
20
27
  }
21
28
  }
22
- function getResolvedPDFJS() {
29
+ async function getResolvedPDFJS() {
30
+ if (!resolvedModule)
31
+ await resolvePDFJSImports();
23
32
  return resolvedModule;
24
33
  }
25
34
  async function resolvePDFJSImports() {
@@ -32,5 +41,8 @@ async function resolvePDFJSImports() {
32
41
  throw new Error("PDF.js is not available. Please add the package as a dependency.");
33
42
  }
34
43
  }
44
+ function isPDFDocumentProxy(data) {
45
+ return typeof data === "object" && data !== null && "_pdfInfo" in data;
46
+ }
35
47
 
36
- export { getResolvedPDFJS as a, defineUnPDFConfig as d, getDocumentProxy as g, resolvePDFJSImports as r };
48
+ export { getResolvedPDFJS as a, defineUnPDFConfig as d, getDocumentProxy as g, isPDFDocumentProxy as i, resolvePDFJSImports as r };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unpdf",
3
3
  "type": "module",
4
- "version": "0.5.0",
4
+ "version": "0.5.2",
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>",
@@ -1,31 +0,0 @@
1
- import PDFJS from 'pdfjs-dist';
2
-
3
- declare function getPDFMeta(data: ArrayBuffer): Promise<{
4
- info: Record<string, any>;
5
- metadata: any;
6
- }>;
7
-
8
- declare function extractPDFText(data: ArrayBuffer, { mergePages }?: {
9
- mergePages?: boolean;
10
- }): Promise<{
11
- totalPages: number;
12
- text: string | string[];
13
- }>;
14
-
15
- declare function getImagesFromPage(data: ArrayBuffer, pageNumber: number): Promise<ArrayBuffer[]>;
16
-
17
- interface UnPDFConfiguration {
18
- /**
19
- * By default, UnPDF will use the latest version of PDF.js. If you want to
20
- * use an older version or the legacy build, set a promise that resolves to
21
- * the PDF.js module.
22
- *
23
- * @example
24
- * () => import('pdfjs-dist/legacy/build/pdf.js')
25
- */
26
- pdfjs?: () => Promise<typeof PDFJS>;
27
- }
28
-
29
- declare function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>;
30
-
31
- export { getImagesFromPage as a, defineUnPDFConfig as d, extractPDFText as e, getPDFMeta as g };
@@ -1,31 +0,0 @@
1
- import PDFJS from 'pdfjs-dist';
2
-
3
- declare function getPDFMeta(data: ArrayBuffer): Promise<{
4
- info: Record<string, any>;
5
- metadata: any;
6
- }>;
7
-
8
- declare function extractPDFText(data: ArrayBuffer, { mergePages }?: {
9
- mergePages?: boolean;
10
- }): Promise<{
11
- totalPages: number;
12
- text: string | string[];
13
- }>;
14
-
15
- declare function getImagesFromPage(data: ArrayBuffer, pageNumber: number): Promise<ArrayBuffer[]>;
16
-
17
- interface UnPDFConfiguration {
18
- /**
19
- * By default, UnPDF will use the latest version of PDF.js. If you want to
20
- * use an older version or the legacy build, set a promise that resolves to
21
- * the PDF.js module.
22
- *
23
- * @example
24
- * () => import('pdfjs-dist/legacy/build/pdf.js')
25
- */
26
- pdfjs?: () => Promise<typeof PDFJS>;
27
- }
28
-
29
- declare function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>;
30
-
31
- export { getImagesFromPage as a, defineUnPDFConfig as d, extractPDFText as e, getPDFMeta as g };
@@ -1,31 +0,0 @@
1
- import PDFJS from 'pdfjs-dist';
2
-
3
- declare function getPDFMeta(data: ArrayBuffer): Promise<{
4
- info: Record<string, any>;
5
- metadata: any;
6
- }>;
7
-
8
- declare function extractPDFText(data: ArrayBuffer, { mergePages }?: {
9
- mergePages?: boolean;
10
- }): Promise<{
11
- totalPages: number;
12
- text: string | string[];
13
- }>;
14
-
15
- declare function getImagesFromPage(data: ArrayBuffer, pageNumber: number): Promise<ArrayBuffer[]>;
16
-
17
- interface UnPDFConfiguration {
18
- /**
19
- * By default, UnPDF will use the latest version of PDF.js. If you want to
20
- * use an older version or the legacy build, set a promise that resolves to
21
- * the PDF.js module.
22
- *
23
- * @example
24
- * () => import('pdfjs-dist/legacy/build/pdf.js')
25
- */
26
- pdfjs?: () => Promise<typeof PDFJS>;
27
- }
28
-
29
- declare function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>;
30
-
31
- export { getImagesFromPage as a, defineUnPDFConfig as d, extractPDFText as e, getPDFMeta as g };