unpdf 0.2.0 → 0.2.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
@@ -6,7 +6,7 @@ A collection of utilities to work with PDFs.
6
6
 
7
7
  **Why this package then?**
8
8
 
9
- - To circumvent build issues in serverless environments, where the `canvas` package is used by `PDF.js` not supported.
9
+ - To circumvent build issues in serverless environments, where the `canvas` package used by `PDF.js` is not supported.
10
10
  - WIP and more to come.
11
11
 
12
12
  ## Installation
@@ -36,6 +36,33 @@ const { totalPages, info, metadata, text } = await decodePDFText(
36
36
  )
37
37
  ```
38
38
 
39
+ ## Methods
40
+
41
+ ### `decodePDFText`
42
+
43
+ ```ts
44
+ interface PDFContent {
45
+ totalPages: number
46
+ info?: Record<string, any>
47
+ metadata?: any
48
+ text: string | string[]
49
+ }
50
+
51
+ function decodePDFText(
52
+ data: ArrayBuffer,
53
+ { mergePages }?: { mergePages?: boolean }
54
+ ): Promise<PDFContent>
55
+ ```
56
+
57
+ ### `getImagesFromPage`
58
+
59
+ ```ts
60
+ function getImagesFromPage(
61
+ data: ArrayBuffer,
62
+ pageNumber: number
63
+ ): Promise<ArrayBuffer[]>
64
+ ```
65
+
39
66
  ## License
40
67
 
41
68
  [MIT](./LICENSE) License © 2023-PRESENT [Johann Schopplich](https://github.com/johannschopplich)
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const pdfjsDist = require('pdfjs-dist');
4
-
5
3
  async function getDocumentProxy(data) {
6
4
  const { getDocument } = await getPDFJSImports();
7
5
  const pdf = await getDocument({
@@ -15,8 +13,7 @@ async function getDocumentProxy(data) {
15
13
  async function getPDFJSImports() {
16
14
  try {
17
15
  const { default: mod } = await import('pdfjs-dist/legacy/build/pdf');
18
- const { getDocument, version } = mod;
19
- return { getDocument, version };
16
+ return mod;
20
17
  } catch (e) {
21
18
  console.error(e);
22
19
  throw new Error(
@@ -52,9 +49,10 @@ async function getImagesFromPage(data, pageNumber) {
52
49
  const pdf = await getDocumentProxy(data);
53
50
  const page = await pdf.getPage(pageNumber);
54
51
  const operatorList = await page.getOperatorList();
52
+ const { OPS } = await getPDFJSImports();
55
53
  const images = [];
56
54
  for (const op of operatorList.fnArray) {
57
- if (op !== pdfjsDist.OPS.paintImageXObject)
55
+ if (op !== OPS.paintImageXObject)
58
56
  continue;
59
57
  const image = await page.objs.get(operatorList.argsArray[op][0]);
60
58
  if (image.data)
@@ -1,5 +1,3 @@
1
- import { OPS } from 'pdfjs-dist';
2
-
3
1
  async function getDocumentProxy(data) {
4
2
  const { getDocument } = await getPDFJSImports();
5
3
  const pdf = await getDocument({
@@ -13,8 +11,7 @@ async function getDocumentProxy(data) {
13
11
  async function getPDFJSImports() {
14
12
  try {
15
13
  const { default: mod } = await import('pdfjs-dist/legacy/build/pdf');
16
- const { getDocument, version } = mod;
17
- return { getDocument, version };
14
+ return mod;
18
15
  } catch (e) {
19
16
  console.error(e);
20
17
  throw new Error(
@@ -50,6 +47,7 @@ async function getImagesFromPage(data, pageNumber) {
50
47
  const pdf = await getDocumentProxy(data);
51
48
  const page = await pdf.getPage(pageNumber);
52
49
  const operatorList = await page.getOperatorList();
50
+ const { OPS } = await getPDFJSImports();
53
51
  const images = [];
54
52
  for (const op of operatorList.fnArray) {
55
53
  if (op !== OPS.paintImageXObject)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unpdf",
3
3
  "type": "module",
4
- "version": "0.2.0",
4
+ "version": "0.2.2",
5
5
  "packageManager": "pnpm@8.6.12",
6
6
  "description": "Utilities to work with PDFs",
7
7
  "author": "Johann Schopplich <pkg@johannschopplich.com>",