unpdf 0.5.2 → 0.5.4

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
@@ -32,11 +32,16 @@ yarn add unpdf
32
32
  ```ts
33
33
  import { extractPDFText } from 'unpdf'
34
34
 
35
- const pdfBuffer = await fetch('https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf')
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(pdfBuffer), { mergePages: true }
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,7 +88,7 @@ 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
94
  function defineUnPDFConfig(config: UnPDFConfiguration): Promise<void>
@@ -91,7 +96,7 @@ function defineUnPDFConfig(config: UnPDFConfiguration): Promise<void>
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')>
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const image = require('./shared/unpdf.6502e2c8.cjs');
4
- const utils = require('./shared/unpdf.9b5cc790.cjs');
3
+ const image = require('./shared/unpdf.eba9a3dc.cjs');
4
+ const utils = require('./shared/unpdf.42987ff1.cjs');
5
5
 
6
6
  const getPDFMeta = async (...args) => {
7
7
  await utils.resolvePDFJSImports();
@@ -1,5 +1,5 @@
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';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.6363738c.cjs';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.6363738c.cjs';
3
3
  import 'pdfjs-dist/types/src/display/api';
4
4
  import 'pdfjs-dist';
5
5
 
@@ -1,5 +1,5 @@
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';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.6363738c.mjs';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.6363738c.mjs';
3
3
  import 'pdfjs-dist/types/src/display/api';
4
4
  import 'pdfjs-dist';
5
5
 
@@ -1,5 +1,5 @@
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';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.6363738c.js';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.6363738c.js';
3
3
  import 'pdfjs-dist/types/src/display/api';
4
4
  import 'pdfjs-dist';
5
5
 
@@ -1,6 +1,6 @@
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';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.3dcacb97.mjs';
2
+ import { r as resolvePDFJSImports } from './shared/unpdf.ab6a2535.mjs';
3
+ export { d as defineUnPDFConfig, g as getDocumentProxy, a as getResolvedPDFJS } from './shared/unpdf.ab6a2535.mjs';
4
4
 
5
5
  const getPDFMeta = async (...args) => {
6
6
  await resolvePDFJSImports();
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const image = require('./shared/unpdf.6502e2c8.cjs');
4
- const utils = require('./shared/unpdf.9b5cc790.cjs');
3
+ const image = require('./shared/unpdf.eba9a3dc.cjs');
4
+ const utils = require('./shared/unpdf.42987ff1.cjs');
5
5
 
6
6
  const getPDFMeta = async (...args) => {
7
7
  await utils.resolvePDFJSImports();
@@ -1,5 +1,5 @@
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';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.6363738c.cjs';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.6363738c.cjs';
3
3
  import 'pdfjs-dist/types/src/display/api';
4
4
  import 'pdfjs-dist';
5
5
 
@@ -1,5 +1,5 @@
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';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.6363738c.mjs';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.6363738c.mjs';
3
3
  import 'pdfjs-dist/types/src/display/api';
4
4
  import 'pdfjs-dist';
5
5
 
@@ -1,5 +1,5 @@
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';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.6363738c.js';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.6363738c.js';
3
3
  import 'pdfjs-dist/types/src/display/api';
4
4
  import 'pdfjs-dist';
5
5
 
@@ -1,6 +1,6 @@
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';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.3dcacb97.mjs';
2
+ import { r as resolvePDFJSImports } from './shared/unpdf.ab6a2535.mjs';
3
+ export { d as defineUnPDFConfig, g as getDocumentProxy, a as getResolvedPDFJS } from './shared/unpdf.ab6a2535.mjs';
4
4
 
5
5
  const getPDFMeta = async (...args) => {
6
6
  await resolvePDFJSImports();
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const utils = require('./shared/unpdf.9b5cc790.cjs');
3
+ const utils = require('./shared/unpdf.42987ff1.cjs');
4
4
 
5
5
  const getPDFMeta = async () => {
6
6
  throw new Error("Not implemented in worker context yet");
@@ -1,5 +1,5 @@
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';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.6363738c.cjs';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.6363738c.cjs';
3
3
  import 'pdfjs-dist/types/src/display/api';
4
4
  import 'pdfjs-dist';
5
5
 
@@ -1,5 +1,5 @@
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';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.6363738c.mjs';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.6363738c.mjs';
3
3
  import 'pdfjs-dist/types/src/display/api';
4
4
  import 'pdfjs-dist';
5
5
 
@@ -1,5 +1,5 @@
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';
1
+ import { g as getPDFMeta$1, e as extractPDFText$1, a as getImagesFromPage$1 } from './shared/unpdf.6363738c.js';
2
+ export { d as defineUnPDFConfig, b as getDocumentProxy, c as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.6363738c.js';
3
3
  import 'pdfjs-dist/types/src/display/api';
4
4
  import 'pdfjs-dist';
5
5
 
@@ -1,4 +1,4 @@
1
- export { d as defineUnPDFConfig, g as getDocumentProxy, a as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.f801f444.mjs';
1
+ export { d as defineUnPDFConfig, g as getDocumentProxy, a as getResolvedPDFJS, r as resolvePDFJSImports } from './shared/unpdf.ab6a2535.mjs';
2
2
 
3
3
  const getPDFMeta = async () => {
4
4
  throw new Error("Not implemented in worker context yet");
@@ -1,4 +1,4 @@
1
- import { i as isPDFDocumentProxy, g as getDocumentProxy, a as getResolvedPDFJS } from './unpdf.f801f444.mjs';
1
+ import { i as isPDFDocumentProxy, g as getDocumentProxy, a as getResolvedPDFJS } from './unpdf.ab6a2535.mjs';
2
2
 
3
3
  async function getPDFMeta(data) {
4
4
  const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
@@ -9,7 +9,8 @@ async function getPDFMeta(data) {
9
9
  };
10
10
  }
11
11
 
12
- async function extractPDFText(data, { mergePages = false } = {}) {
12
+ async function extractPDFText(data, options = {}) {
13
+ const { mergePages = false } = { ...options };
13
14
  const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
14
15
  const texts = await Promise.all(
15
16
  Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
@@ -2,40 +2,38 @@
2
2
 
3
3
  let resolvedModule;
4
4
  async function getDocumentProxy(data, options = {}) {
5
- const {
6
- useWorkerFetch = false,
7
- isEvalSupported = false,
8
- useSystemFonts = true,
9
- ...rest
10
- } = options;
11
5
  const { getDocument } = await getResolvedPDFJS();
12
6
  const pdf = await getDocument({
13
7
  data,
14
- useWorkerFetch,
15
- useSystemFonts,
16
- isEvalSupported,
17
- ...rest
8
+ useWorkerFetch: false,
9
+ isEvalSupported: false,
10
+ useSystemFonts: true,
11
+ ...options
18
12
  }).promise;
19
13
  return pdf;
20
14
  }
21
- async function defineUnPDFConfig({ pdfjs }) {
22
- if (pdfjs) {
23
- try {
24
- const { default: mod } = await pdfjs();
25
- resolvedModule = mod;
26
- } catch (error) {
27
- throw new Error("Resolving the PDF.js module failed. Please check your configuration.");
28
- }
29
- }
15
+ async function defineUnPDFConfig(options) {
16
+ const { pdfjs } = { ...options };
17
+ if (pdfjs)
18
+ await resolvePDFJSImports(pdfjs);
30
19
  }
31
20
  async function getResolvedPDFJS() {
32
21
  if (!resolvedModule)
33
22
  await resolvePDFJSImports();
34
23
  return resolvedModule;
35
24
  }
36
- async function resolvePDFJSImports() {
25
+ async function resolvePDFJSImports(pdfjs) {
37
26
  if (resolvedModule)
38
27
  return;
28
+ if (pdfjs) {
29
+ try {
30
+ const { default: mod } = await pdfjs();
31
+ resolvedModule = mod;
32
+ return;
33
+ } catch (error) {
34
+ throw new Error("Resolving PDF.js failed. Please check the provided configuration.");
35
+ }
36
+ }
39
37
  try {
40
38
  const { default: mod } = await import('pdfjs-dist');
41
39
  resolvedModule = mod;
@@ -6,7 +6,7 @@ declare function getPDFMeta(data: BinaryData | PDFDocumentProxy): Promise<{
6
6
  metadata: Record<string, any>;
7
7
  }>;
8
8
 
9
- declare function extractPDFText(data: BinaryData | PDFDocumentProxy, { mergePages }?: {
9
+ declare function extractPDFText(data: BinaryData | PDFDocumentProxy, options?: {
10
10
  mergePages?: boolean;
11
11
  }): Promise<{
12
12
  totalPages: number;
@@ -36,8 +36,8 @@ interface UnPDFConfiguration {
36
36
  * - `useSystemFonts: true`
37
37
  */
38
38
  declare function getDocumentProxy(data: BinaryData, options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
39
- declare function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>;
39
+ declare function defineUnPDFConfig(options: UnPDFConfiguration): Promise<void>;
40
40
  declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
41
- declare function resolvePDFJSImports(): Promise<void>;
41
+ declare function resolvePDFJSImports(pdfjs?: () => Promise<typeof PDFJS>): Promise<void>;
42
42
 
43
43
  export { getImagesFromPage as a, getDocumentProxy as b, getResolvedPDFJS as c, defineUnPDFConfig as d, extractPDFText as e, getPDFMeta as g, resolvePDFJSImports as r };
@@ -6,7 +6,7 @@ declare function getPDFMeta(data: BinaryData | PDFDocumentProxy): Promise<{
6
6
  metadata: Record<string, any>;
7
7
  }>;
8
8
 
9
- declare function extractPDFText(data: BinaryData | PDFDocumentProxy, { mergePages }?: {
9
+ declare function extractPDFText(data: BinaryData | PDFDocumentProxy, options?: {
10
10
  mergePages?: boolean;
11
11
  }): Promise<{
12
12
  totalPages: number;
@@ -36,8 +36,8 @@ interface UnPDFConfiguration {
36
36
  * - `useSystemFonts: true`
37
37
  */
38
38
  declare function getDocumentProxy(data: BinaryData, options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
39
- declare function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>;
39
+ declare function defineUnPDFConfig(options: UnPDFConfiguration): Promise<void>;
40
40
  declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
41
- declare function resolvePDFJSImports(): Promise<void>;
41
+ declare function resolvePDFJSImports(pdfjs?: () => Promise<typeof PDFJS>): Promise<void>;
42
42
 
43
43
  export { getImagesFromPage as a, getDocumentProxy as b, getResolvedPDFJS as c, defineUnPDFConfig as d, extractPDFText as e, getPDFMeta as g, resolvePDFJSImports as r };
@@ -6,7 +6,7 @@ declare function getPDFMeta(data: BinaryData | PDFDocumentProxy): Promise<{
6
6
  metadata: Record<string, any>;
7
7
  }>;
8
8
 
9
- declare function extractPDFText(data: BinaryData | PDFDocumentProxy, { mergePages }?: {
9
+ declare function extractPDFText(data: BinaryData | PDFDocumentProxy, options?: {
10
10
  mergePages?: boolean;
11
11
  }): Promise<{
12
12
  totalPages: number;
@@ -36,8 +36,8 @@ interface UnPDFConfiguration {
36
36
  * - `useSystemFonts: true`
37
37
  */
38
38
  declare function getDocumentProxy(data: BinaryData, options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
39
- declare function defineUnPDFConfig({ pdfjs }: UnPDFConfiguration): Promise<void>;
39
+ declare function defineUnPDFConfig(options: UnPDFConfiguration): Promise<void>;
40
40
  declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
41
- declare function resolvePDFJSImports(): Promise<void>;
41
+ declare function resolvePDFJSImports(pdfjs?: () => Promise<typeof PDFJS>): Promise<void>;
42
42
 
43
43
  export { getImagesFromPage as a, getDocumentProxy as b, getResolvedPDFJS as c, defineUnPDFConfig as d, extractPDFText as e, getPDFMeta as g, resolvePDFJSImports as r };
@@ -1,39 +1,37 @@
1
1
  let resolvedModule;
2
2
  async function getDocumentProxy(data, options = {}) {
3
- const {
4
- useWorkerFetch = false,
5
- isEvalSupported = false,
6
- useSystemFonts = true,
7
- ...rest
8
- } = options;
9
3
  const { getDocument } = await getResolvedPDFJS();
10
4
  const pdf = await getDocument({
11
5
  data,
12
- useWorkerFetch,
13
- useSystemFonts,
14
- isEvalSupported,
15
- ...rest
6
+ useWorkerFetch: false,
7
+ isEvalSupported: false,
8
+ useSystemFonts: true,
9
+ ...options
16
10
  }).promise;
17
11
  return pdf;
18
12
  }
19
- async function defineUnPDFConfig({ pdfjs }) {
20
- if (pdfjs) {
21
- try {
22
- const { default: mod } = await pdfjs();
23
- resolvedModule = mod;
24
- } catch (error) {
25
- throw new Error("Resolving the PDF.js module failed. Please check your configuration.");
26
- }
27
- }
13
+ async function defineUnPDFConfig(options) {
14
+ const { pdfjs } = { ...options };
15
+ if (pdfjs)
16
+ await resolvePDFJSImports(pdfjs);
28
17
  }
29
18
  async function getResolvedPDFJS() {
30
19
  if (!resolvedModule)
31
20
  await resolvePDFJSImports();
32
21
  return resolvedModule;
33
22
  }
34
- async function resolvePDFJSImports() {
23
+ async function resolvePDFJSImports(pdfjs) {
35
24
  if (resolvedModule)
36
25
  return;
26
+ if (pdfjs) {
27
+ try {
28
+ const { default: mod } = await pdfjs();
29
+ resolvedModule = mod;
30
+ return;
31
+ } catch (error) {
32
+ throw new Error("Resolving PDF.js failed. Please check the provided configuration.");
33
+ }
34
+ }
37
35
  try {
38
36
  const { default: mod } = await import('pdfjs-dist');
39
37
  resolvedModule = mod;
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const utils = require('./unpdf.9b5cc790.cjs');
3
+ const utils = require('./unpdf.42987ff1.cjs');
4
4
 
5
5
  async function getPDFMeta(data) {
6
6
  const pdf = utils.isPDFDocumentProxy(data) ? data : await utils.getDocumentProxy(data);
@@ -11,7 +11,8 @@ async function getPDFMeta(data) {
11
11
  };
12
12
  }
13
13
 
14
- async function extractPDFText(data, { mergePages = false } = {}) {
14
+ async function extractPDFText(data, options = {}) {
15
+ const { mergePages = false } = { ...options };
15
16
  const pdf = utils.isPDFDocumentProxy(data) ? data : await utils.getDocumentProxy(data);
16
17
  const texts = await Promise.all(
17
18
  Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unpdf",
3
3
  "type": "module",
4
- "version": "0.5.2",
4
+ "version": "0.5.4",
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>",