unpdf

Utilities to work with PDFs, like extracting text

README

unpdf


A collection of utilities to work with PDFs. Uses Mozilla's PDF.js under the hood and lazily initializes the library.

unpdf takes advantage of export conditions to circumvent build issues in serverless environments. For example, PDF.js depends on the optionalcanvas module, which doesn't work inside worker threads.

This library is also intended as a modern alternative to the unmaintained but still popular [pdf-parse](https://www.npmjs.com/package/pdf-parse).

Features


- 🏗️ Conditional exports for Node.js, worker and browser environments
- 💬 Extract text and images from PDFs
- 🧱 Opt-in to legacy PDF.js build

Installation


Run the following command to add unpdf to your project.

  1. ```bash
  2. # pnpm
  3. pnpm add unpdf

  4. # npm
  5. npm install unpdf

  6. # yarn
  7. yarn add unpdf
  8. ```

Usage


  1. ```ts
  2. import { extractPDFText } from 'unpdf'

  3. // Fetch a PDF file from the web
  4. const pdf = await fetch('https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf')
  5.   .then(res => res.arrayBuffer())

  6. // Or load it from the filesystem
  7. const pdf = await readFile('./dummy.pdf')

  8. // Pass the PDF buffer to the relevant method
  9. const { totalPages, text } = await extractPDFText(
  10.   new Uint8Array(pdf), { mergePages: true }
  11. )
  12. ```

Use Legacy Or Custom PDF.js Build


  1. ```ts
  2. // Before using any other methods, define the PDF.js module
  3. import { defineUnPDFConfig } from 'unpdf'

  4. // Use the legacy build
  5. defineUnPDFConfig({
  6.   pdfjs: () => import('pdfjs-dist/legacy/build/pdf.js')
  7. })

  8. // Now, you can use the other methods
  9. // …
  10. ```

Access the PDF.js Module


  1. ```ts
  2. import { getResolvedPDFJS } from 'unpdf'

  3. const { version } = await getResolvedPDFJS()
  4. ```

Config


  1. ```ts
  2. interface UnPDFConfiguration {
  3.   /**
  4.    * By default, UnPDF will use the latest version of PDF.js. If you want to
  5.    * use an older version or the legacy build, set a promise that resolves to
  6.    * the PDF.js module.
  7.    *
  8.    * @example
  9.    * () => import('pdfjs-dist/legacy/build/pdf.js')
  10.    */
  11.   pdfjs?: () => Promise<typeof PDFJS>
  12. }
  13. ```

Methods


defineUnPDFConfig


Define a custom PDF.js module, like the legacy build. Make sure to call this method before using any other methods.

  1. ```ts
  2. function defineUnPDFConfig(config: UnPDFConfiguration): Promise<void>
  3. ```

getResolvedPDFJS


Returns the resolved PDF.js module. If no build is defined, the latest version will be initialized.

  1. ```ts
  2. function getResolvedPDFJS(): Promise<typeof import('pdfjs-dist')>
  3. ```

getPDFMeta


  1. ```ts
  2. function getPDFMeta(
  3.   data: BinaryData | PDFDocumentProxy
  4. ): Promise<{
  5.   info: Record<string, any>
  6.   metadata: Record<string, any>
  7. }>
  8. ```

extractPDFText


  1. ```ts
  2. function extractPDFText(
  3.   data: BinaryData | PDFDocumentProxy,
  4.   { mergePages }?: { mergePages?: boolean }
  5. ): Promise<{
  6.   totalPages: number
  7.   text: string | string[]
  8. }>
  9. ```

getImagesFromPage


  1. ```ts
  2. function getImagesFromPage(
  3.   data: BinaryData | PDFDocumentProxy,
  4.   pageNumber: number
  5. ): Promise<ArrayBuffer[]>
  6. ```

License


MIT License © 2023-PRESENT Johann Schopplich