Registry / ai-ml / unpdf
library1.6.2jsnpmunverified

unpdf is a modern, actively maintained PDF extraction and rendering library that runs across all JavaScript runtimes: Node.js, Deno, Bun, browsers, and serverless environments like Cloudflare Workers. Version 1.6.2 ships a serverless build of Mozilla's PDF.js v5.6.205, optimized for edge computing. It provides high-level APIs for extracting text, links, and images from PDF files, making it ideal for AI applications that require PDF summarization or analysis. Unlike older libraries like pdf-parse, unpdf supports ESM, TypeScript natively, and eliminates the need for external worker files in serverless contexts. It offers opt-in support for official or legacy PDF.js builds and includes a peer dependency on @napi-rs/canvas for rendering operations.

npm install unpdf
INSTALL
IMPORT
SIG · UNPDF
U
unpdf
ai-mljavascriptv1.6.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

extractText
import { extractText } from 'unpdf'
import extractText from 'unpdf'
Default import is not supported; all exports are named.
getDocumentProxy
import { getDocumentProxy } from 'unpdf'
const { getDocumentProxy } = require('unpdf')
unpdf is ESM-only; CommonJS require() will not work. Use dynamic import() if needed in CJS.
definePDFJSModule
import { definePDFJSModule } from 'unpdf'
Must be awaited before using any other unpdf method to ensure custom PDF.js build is used.
getResolvedPDFJS
import { getResolvedPDFJS } from 'unpdf'
Returns a promise of the PDF.js module; use await to access its methods like getDocument.

Shows basic usage of unpdf to fetch a PDF, load it, extract text, and log the result. Assumes an environment with fetch and async/await.

import { extractText, getDocumentProxy } from 'unpdf' async function main() { // Fetch a sample PDF const response = await fetch('https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf') const arrayBuffer = await response.arrayBuffer() const uint8Array = new Uint8Array(arrayBuffer) const pdf = await getDocumentProxy(uint8Array) const { totalPages, text } = await extractText(pdf, { mergePages: true }) console.log(`Total pages: ${totalPages}`) console.log(text) } main().catch(console.error)
Debug
Known issues
breakingPDF.js v5.x uses Promise.withResolvers, which is not supported in Node.js < 22. The bundled serverless build includes a polyfill, but custom official PDF.js build may fail.
fix
Use the bundled serverless build (default) or upgrade Node.js to >=22.
affects: >=1.0.0
deprecatedThe 'unpdf/pdfjs' subpath import is internal; direct usage may break in future versions.
fix
Use getResolvedPDFJS() instead of importing from 'unpdf/pdfjs'.
affects: >=1.0.0
gotchaAll async methods (getDocumentProxy, extractText, etc.) must be awaited; forgetting await leads to unresolved promises.
fix
Ensure you await every call: const pdf = await getDocumentProxy(...); const { text } = await extractText(pdf);
affects: >=1.0.0
gotchaThe package is ESM-only. Using require('unpdf') will throw a runtime error.
fix
Use import statements or dynamic import() in CommonJS modules.
affects: >=1.0.0
gotchaIf you use a custom PDF.js build via definePDFJSModule, you must call it before any other unpdf method; otherwise, the serverless build is used by default.
fix
Always await definePDFJSModule(...) at the top of your module.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'getDocument')
Using getResolvedPDFJS() without awaiting or importing incorrectly.
fix
Use: const { getDocument } = await getResolvedPDFJS();
Error: The module './node_modules/unpdf/dist/index.mjs' does not provide an export named 'default'
Trying default import when package only exports named exports.
fix
Use: import { extractText } from 'unpdf'
Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/unpdf/dist/index.mjs not supported.
Using CommonJS require() on an ESM-only package.
fix
Switch to import syntax or use dynamic import: const unpdf = await import('unpdf')
TypeError: Promise.withResolvers is not a function
Using official PDF.js build (not serverless) in Node.js <22.
fix
Use the bundled serverless build (default) or upgrade Node.js version.
Upgrade
Version history
1.6.2latest on npm
Audit
Dependencies
@napi-rs/canvasoptionalRequired for PDF rendering (e.g., converting PDF pages to images). Not needed if only extracting text.
Agent activity
35 hits · last 30 days
node
28
OpenAI (training)
1
Resources
packageunpdf
unpdf — npm install unpdf · libregistry