Registry / web-framework / pdfjs-dist-es5

pdfjs-dist-es5

JSON →
library2.13.216jsnpmunverified

pdfjs-dist-es5 is a pre-built, ES5-compatible distribution of Mozilla's PDF.js library, designed for environments that lack support for modern JavaScript features like `async`/`await`, optional chaining, or private class fields. It aims to provide a general-purpose, web standards-based platform for parsing and rendering PDF documents directly in the browser using HTML5. The current stable version is 2.13.216, though the main `pdfjs-dist` package has much more recent releases. This package serves as a specific compatibility layer rather than the bleeding edge, focusing on broader browser support. It differentiates itself by offering a transpiled version of the core PDF.js engine, making it suitable for legacy applications or older browser targets that cannot handle the ES2015+ syntax of the primary `pdfjs-dist` package. Its release cadence is tied to specific compatibility needs, making it less frequent than the main library.

npm install pdfjs-dist-es5
INSTALL
IMPORT
SIG · PDFJS-DIST-ES5
P
pdfjs-dist-es5
web-frameworkjavascriptv2.13.216
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

pdfjsLib
import * as pdfjsLib from 'pdfjs-dist-es5/build/pdf';
import pdfjsLib from 'pdfjs-dist-es5'; // This is for 'pdfjs-dist' main entry point, not the ES5 build directly.
For ES modules, the main library functions like getDocument and GlobalWorkerOptions are often accessed via a wildcard import from the build/pdf entry point. The root 'pdfjs-dist-es5' might not export them directly in a usable way for some bundlers.
GlobalWorkerOptions
import { GlobalWorkerOptions } from 'pdfjs-dist-es5/build/pdf'; GlobalWorkerOptions.workerSrc = `//cdn.jsdelivr.net/npm/pdfjs-dist-es5@${pdfjsLib.version}/build/pdf.worker.min.js`;
import pdfjsWorker from 'pdfjs-dist-es5/build/pdf.worker.entry';
Directly importing `pdf.worker.entry` often requires specific webpack configurations (e.g., `worker-loader`) and might lead to bundling issues. The recommended approach for browser environments is to set `GlobalWorkerOptions.workerSrc` to a path of the worker file, often from a CDN or a locally copied asset.
getDocument
import { getDocument } from 'pdfjs-dist-es5/build/pdf';
const { getDocument } = require('pdfjs-dist-es5');
While this package is ES5-compatible, it primarily ships as an ES module. CommonJS `require` might work with transpilers but is generally not the idiomatic way for direct consumption without specific build configurations. For older Node.js versions, dynamic `import()` might be needed.

This snippet demonstrates how to load a PDF from a URL and render its first page onto a specified HTML canvas element, setting up the worker correctly.

import { getDocument, GlobalWorkerOptions } from 'pdfjs-dist-es5/build/pdf'; // NOTE: This assumes pdf.worker.min.js is accessible via a CDN or copied to a public path. // For optimal performance, it's recommended to host the worker file alongside your app // or use a reliable CDN. Replace with the actual worker path if self-hosting. GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.13.216/pdf.worker.min.js`; const loadAndRenderPdf = async (url: string, canvasId: string) => { const loadingTask = getDocument(url); const pdfDocument = await loadingTask.promise; console.log(`PDF loaded: ${pdfDocument.numPages} pages.`); const page = await pdfDocument.getPage(1); // Get the first page const viewport = page.getViewport({ scale: 1.5 }); const canvas = document.getElementById(canvasId) as HTMLCanvasElement; if (!canvas) { console.error(`Canvas element with ID '${canvasId}' not found.`); return; } const context = canvas.getContext('2d'); if (!context) { console.error('Failed to get 2D context from canvas.'); return; } canvas.height = viewport.height; canvas.width = viewport.width; const renderContext = { canvasContext: context, viewport: viewport, }; await page.render(renderContext).promise; console.log('Page rendered to canvas.'); }; // Example usage (replace with a real PDF URL and a canvas element ID) const pdfUrl = 'https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf'; const targetCanvasId = 'pdf-render-canvas'; // In a real HTML file, you would have: // <canvas id="pdf-render-canvas"></canvas> if (typeof document !== 'undefined') { // Ensure this runs only in a browser environment document.addEventListener('DOMContentLoaded', () => { const canvasElement = document.createElement('canvas'); canvasElement.id = targetCanvasId; document.body.appendChild(canvasElement); loadAndRenderPdf(pdfUrl, targetCanvasId).catch(console.error); }); }
Debug
Known issues
gotchaThis `pdfjs-dist-es5` package is an older, ES5-compatible build of the main `pdfjs-dist` library (current stable version 2.13.216). It is specifically for environments lacking modern JavaScript features (e.g., `async`/`await`). For modern browsers and environments, the `pdfjs-dist` package is recommended for better performance and features.
fix
For modern environments, prefer `npm install pdfjs-dist`. Only use `pdfjs-dist-es5` if strict ES5 compatibility is a hard requirement for your target browsers.
affects: >=2.0.0
breakingThe directory structure for ES5 builds within `pdfjs-dist` changed around version 2.8.335. The `es5` folder was renamed to `legacy`. While `pdfjs-dist-es5` is a standalone package, its underlying upstream might reflect similar changes in how 'legacy' builds are organized, potentially affecting direct file path imports if not using the main entry points.
fix
Ensure you are importing from `pdfjs-dist-es5/build/pdf` or `pdfjs-dist-es5/legacy/build/pdf` as appropriate for your specific setup. Always test imports after major version upgrades of the underlying `pdfjs-dist` if you're using custom paths.
affects: >=2.8.335
gotchaThe PDF.js web worker (`pdf.worker.js`) must be correctly located and accessible at runtime. Misconfiguring `GlobalWorkerOptions.workerSrc` is a very common source of errors, especially in bundled applications. The `worker-loader` peer dependency suggests specific webpack handling may be expected.
fix
Set `GlobalWorkerOptions.workerSrc` to a public URL where `pdf.worker.min.js` is hosted (e.g., CDN, or a file copied to your app's public directory). Avoid direct `import pdfjsWorker from 'pdfjs-dist-es5/build/pdf.worker.entry'` without proper webpack configuration including `worker-loader`.
affects: >=2.0.0
Errors
Common errors & fixes
Uncaught TypeError: Cannot read properties of undefined (reading 'GlobalWorkerOptions')
The main PDF.js library object, often aliased as `pdfjsLib` or imported as a namespace, was not correctly loaded or accessed before trying to set worker options. This often happens with incorrect `import` statements.
fix
Ensure you are importing with `import * as pdfjsLib from 'pdfjs-dist-es5/build/pdf';` or similar named imports `import { GlobalWorkerOptions, getDocument } from 'pdfjs-dist-es5/build/pdf';`.
Module not found: Error: Can't resolve 'pdfjs-dist-es5/build/pdf' in '[your-project-path]'
The bundler cannot find the specified module path. This can happen if the package is not installed, or the import path is incorrect for the `pdfjs-dist-es5` package.
fix
Verify `pdfjs-dist-es5` is installed via `npm list pdfjs-dist-es5`. Check the exact import path, it should typically be `pdfjs-dist-es5/build/pdf` for the main library functions.
Worker src not set
The `GlobalWorkerOptions.workerSrc` property, which tells PDF.js where to load its worker script, has not been defined before attempting to load a PDF. This is crucial for performance and proper PDF parsing.
fix
Before calling `getDocument()`, set `GlobalWorkerOptions.workerSrc = 'path/to/pdf.worker.min.js';`. This path should be publicly accessible by the browser.
Upgrade
Version history
2.13.216latest on npm
Audit
Dependencies
worker-loaderrequiredNeeded for integrating the PDF.js web worker into webpack-based projects, especially for older configurations.
Agent activity
4 hits · last 30 days
node
4
Resources
pdfjs-dist-es5 — npm install pdfjs-dist-es5 · libregistry