Guide
unpdf 1 to 2 Migration for Node.js and Workers

Migrate unpdf on Node.js and Cloudflare Workers. Replace the PDF.js loader, then test text output, blank pages, and Worker imports before upgrading your app.
Contents
Contents
Upgrade your unpdf text extraction job once its Node.js entry point can import ESM on Node.js 24 or later and its Worker bundle passes a local extraction test. Keep the existing pin while you move an older Node.js deployment or a build that depends on a CommonJS entry. In our comparison of unpdf 1.9.1 and 2.2.3, both releases returned identical page-by-page text on Node.js and local Cloudflare workerd, including an empty string for the blank page. unpdf upgrade notes
Quick decision
| What your app does | What to verify before upgrading |
|---|---|
| Extracts text using the default PDF.js build | Keep the ESM import. On the same fixture, compare page count, page order and the text form your caller expects. |
| Supplies its own PDF.js loader | Replace the old configuration call before changing the package pin. Check whether your application expects a failed import at startup or on the first request. |
| Extracts text in a Cloudflare Worker | Build and call the actual Worker handler locally. Send PDF bytes through the request path your app uses. |
| Extracts text and generates previews in a Worker | Test those operations separately. The text handler passed locally, and the blank-page render failed for lack of a canvas. |
Run the same PDF through both pins
Keep the input fixed while you change dependencies. Our fixture has three pages. The first says “Intake complete,” the middle is blank, and the last says “Archive ready.” With page text returned as an array, an empty element in the middle keeps downstream page numbers aligned. Assert that element before any operation that filters empty strings.
Use an empty scratch project with Node.js 24 or later. Install pdf-lib@1.17.1, save this as make-fixture.mjs, and run node make-fixture.mjs. It writes fixture.pdf locally.
import { writeFile } from 'node:fs/promises'
import { PDFDocument, StandardFonts } from 'pdf-lib'
const pdf = await PDFDocument.create({ updateMetadata: false })
const font = await pdf.embedFont(StandardFonts.Helvetica)
pdf.addPage().drawText('Intake complete', { font })
pdf.addPage()
pdf.addPage().drawText('Archive ready', { font })
await writeFile('fixture.pdf', await pdf.save())
Install unpdf@1.9.1 in that project and save the following as check-node.mjs. Run it with node check-node.mjs, then install unpdf@2.2.3 and run it again against the same file. The registry listed the latter pin as latest for this test. npm registry · unpdf release notes
import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import { extractText, getDocumentProxy } from 'unpdf'
const bytes = new Uint8Array(await readFile('fixture.pdf'))
const pdf = await getDocumentProxy(bytes)
try {
const byPage = await extractText(pdf)
const merged = await extractText(pdf, { mergePages: true })
assert.deepEqual(byPage, {
totalPages: 3,
text: ['Intake complete', '', 'Archive ready'],
})
assert.equal(merged.totalPages, byPage.totalPages)
assert.equal(typeof merged.text, 'string')
console.log(JSON.stringify({ byPage, merged }))
} finally {
await pdf.loadingTask.destroy()
}
Methodology and results
On Node.js 26.13.0, both pins printed the expected array and the merged string Intake complete\n\nArchive ready. The merged form also kept the page count. Local workerd returned the same array and page count on both pins. We used unpdf’s bundled PDF.js build in both runtimes. The fixture contains plain Latin text and a blank page.
The test reuses a document proxy for both calls and closes its loading task afterward. If your code passes a byte array directly to extractText, open a fresh copy before sending the same bytes to a second helper. PDF.js may take ownership of the input. A shared proxy also lets you compare page results and merged results without parsing the file again. Versioned unpdf API
Migration notes for PDF.js setup
The major release ships ESM. Check the built job and its launcher as well as the source file. A transpiler can turn a source import into require(), so run the built artifact the way your scheduler or service starts it. For the runtime upgrade itself, see the Node.js migration checklist. unpdf upgrade notes
The default PDF.js build needs no loader registration. If you previously called configureUnPDF, call definePDFJSModule before using unpdf. Registration stores the loader. The import happens when the library first needs PDF.js. If your service should fail to start when a configured module cannot load, explicitly await getResolvedPDFJS() during startup. Our Node.js check confirmed that the old setup called its loader during configuration. The new registration left it uncalled until PDF.js was resolved. That test supplied the bundled PDF.js module. Upstream documents the optional pdfjs-dist/legacy/build/pdf.mjs import for a custom Node.js build. Custom PDF.js build reference
Check extractLinks independently if your job calls it as well as extractText. On the fixture without link annotations, the old pin returned links: [] and the new pin returned links: [[], [], []]. Keep the nested array if you persist page attribution. If your caller expects a single list, flatten it at the point where you adapt the result. unpdf upgrade notes
Smoke-test the Worker entry point
Use a Fetch handler like the one below as the entry point for a local Worker. POST the generated PDF as the request body. We bundled this handler with esbuild using the workerd condition, then dispatched the fixture with Miniflare. Miniflare started local workerd with nodejs_compat disabled. The handler returned HTTP 200 and the same per-page result on both pins. Run your own bundle with the compatibility date and flags you ship. Cloudflare’s Miniflare documentation · unpdf’s Worker example
import { extractText, getDocumentProxy } from 'unpdf'
export default {
async fetch(request) {
const pdf = await getDocumentProxy(await request.arrayBuffer())
try {
return Response.json(await extractText(pdf))
} finally {
await pdf.loadingTask.destroy()
}
},
}
A separate local handler called renderPageAsImage on the blank page. Both pins returned the error “Unsupported environment for canvas creation.” A PDF can be readable as text in the Worker while preview generation still needs a runtime with a canvas. The unpdf documentation describes canvas requirements and CJK character map options. Our local run covers the fixture and the default import path. Hosted Cloudflare execution, character map fetching, scanned documents and native canvas rendering remain unverified. Versioned rendering and document proxy documentation
Before switching the lockfile used by your extraction job, send a representative fixture through each deployed code path in a staging environment. Compare page text and page IDs where you store them, and compare merged text where you index it. If the local test passes and a staging import fails, inspect the deployed bundle and runtime settings before changing the PDF handling. Keep the previous lockfile and runtime configuration available together so you can restore the tested pair.
Source notes
The tagged release notes cover changes to module loading and result types. The results above come from the local fixture shown in this guide. Major-release migration notes · Pinned API reference
Related guides
- Compare extraction libraries in unpdf vs pdf-parse vs pdfjs-dist if you still need to choose a library.
- Build representative PDF fixtures with the PDF generation library comparison.
- Review module-format checks in the ESM-only package migration guide.