Registry / web-framework / pdfjs-dist

pdfjs-dist

JSON →
library5.6.205jsnpmunverified

pdfjs-dist is the generic pre-built distribution of Mozilla's PDF.js library, a powerful HTML5-based Portable Document Format (PDF) library designed for parsing and rendering PDFs directly in web browsers. It aims to provide a general-purpose, web standards-based platform for PDF handling. The current stable version is 5.6.205 (as of April 2026), with frequent patch and minor releases, typically on a monthly or bi-monthly cadence, containing improvements for accessibility, performance, annotation editing, and viewer functionalities. Its key differentiators include being an official Mozilla project, its robust support for various PDF features (like annotations, forms, and accessibility), and its ability to operate entirely client-side, making it a popular choice for in-browser PDF display and manipulation without requiring server-side rendering capabilities.

npm install pdfjs-dist
INSTALL
IMPORT
SIG · PDFJS-DIST
P
pdfjs-dist
web-frameworkjavascriptv5.6.205
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

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

pdfjsLib (namespace import)
✓ import * as pdfjsLib from 'pdfjs-dist';
✗ const pdfjsLib = require('pdfjs-dist/build/pdf.mjs');
This is the most common way to import `pdfjs-dist` in modern browser environments and Node.js with ESM, providing access to `getDocument`, `GlobalWorkerOptions`, and other core utilities. Older CommonJS Node.js projects might need `require('pdfjs-dist/build/pdf')`.
getDocument
✓ import { getDocument } from 'pdfjs-dist';
✗ import { PDFJS } from 'pdfjs-dist';
Named imports are supported for specific functions like `getDocument` from the main entry point. `PDFJS` was a common global object in much older versions of the library but is not the recommended import pattern for modern `pdfjs-dist`.
GlobalWorkerOptions
✓ import { GlobalWorkerOptions } from 'pdfjs-dist/build/pdf';
✗ pdfjsLib.workerSrc = '...';
While `pdfjsLib.GlobalWorkerOptions` works, directly importing `GlobalWorkerOptions` from `pdfjs-dist/build/pdf` can make the purpose of setting the worker path clearer. This object is used to configure critical worker properties like `workerSrc`.
pdf.worker.min.js (worker script)
✓ pdfjsLib.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjsLib.version}/build/pdf.worker.min.js`;
✗ pdfjsLib.GlobalWorkerOptions.workerSrc = '/node_modules/pdfjs-dist/build/pdf.worker.min.js';
This is a *path assignment* for the worker script, not a direct JavaScript import. The `pdf.worker.min.js` file must be publicly accessible via HTTP(S), typically by copying it to your static assets folder or referencing it from a CDN. Directly pointing to `node_modules` in a browser path is incorrect and will fail.

Demonstrates how to load a PDF from a URL, configure the necessary worker script, and render each page of the PDF onto individual HTML canvas elements within a specified container in a browser environment.

import * as pdfjsLib from 'pdfjs-dist'; // IMPORTANT: Configure the worker source. This path must be publicly accessible. // Using unpkg or similar CDN is common for browser environments. pdfjsLib.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjsLib.version}/build/pdf.worker.min.js`; // For Node.js environments, depending on your setup, you might need a different approach. // For example, using `require.resolve` if bundling for Node.js: // pdfjsLib.GlobalWorkerOptions.workerSrc = require.resolve('pdfjs-dist/build/pdf.worker.min.js'); async function loadAndRenderPdf(pdfUrl: string, containerId: string = 'pdf-viewer') { const container = document.getElementById(containerId); if (!container) { console.error(`Container with ID '${containerId}' not found.`); return; } container.innerHTML = ''; // Clear previous content try { const loadingTask = pdfjsLib.getDocument(pdfUrl); const pdfDocument = await loadingTask.promise; console.log(`PDF loaded: ${pdfDocument.numPages} pages`); for (let i = 1; i <= pdfDocument.numPages; i++) { const page = await pdfDocument.getPage(i); const scale = 1.5; // Adjust scale as needed for rendering quality const viewport = page.getViewport({ scale }); const canvas = document.createElement('canvas'); const context = canvas.getContext('2d'); if (!context) { throw new Error('Could not get canvas 2D rendering context'); } canvas.height = viewport.height; canvas.width = viewport.width; canvas.style.display = 'block'; canvas.style.margin = '10px auto'; canvas.style.border = '1px solid #ccc'; container.appendChild(canvas); const renderContext = { canvasContext: context, viewport: viewport, }; await page.render(renderContext).promise; console.log(`Page ${i} rendered to canvas.`); } } catch (error) { console.error('Error loading or rendering PDF:', error); container.innerHTML = `<p style="color: red;">Failed to load PDF: ${error instanceof Error ? error.message : String(error)}</p>`; } } // To run this code, you'll need an HTML file like: // <!DOCTYPE html> // <html lang="en"> // <head><meta charset="UTF-8"><title>PDF.js Quickstart</title></head> // <body><div id="pdf-viewer"></div><script type="module" src="./your-script.js"></script></body> // </html> // Ensure 'your-script.js' is the compiled output of this TypeScript. loadAndRenderPdf('https://raw.githubusercontent.com/mozilla/pdf.js/ba2ddd075de5c0529d8e788c86a1118129202521/web/compressed.tracemonkey-pldi-09.pdf', 'pdf-viewer');
Debug
Known issues
breakingMajor versions of `pdfjs-dist` (e.g., v2 to v3, v3 to v4) often introduce breaking changes, particularly regarding worker initialization, asset loading paths, and API signatures. Always consult the official release notes and migration guides when upgrading across major versions to ensure compatibility.
fix
Thoroughly review the official migration guides on the PDF.js GitHub wiki. For worker-related issues, verify that `GlobalWorkerOptions.workerSrc` is correctly set and points to the appropriate worker file for your specific `pdfjs-dist` version (e.g., `pdf.worker.min.js`).
affects: >=2.0
gotchaThe PDF.js worker script (`pdf.worker.min.js`) must be served from a publicly accessible URL and cannot typically be loaded directly from your `node_modules` directory in a browser environment due to security restrictions and typical web server configurations.
fix
Copy `pdf.worker.min.js` (and potentially `pdf.worker.mjs` for ESM-only contexts) from `node_modules/pdfjs-dist/build/` to a public static assets folder in your project, then reference this public path in `GlobalWorkerOptions.workerSrc`. Alternatively, use a CDN like `unpkg.com` or `jsdelivr.com`.
affects: >=1.0
gotchaWhen loading PDF documents from different origins (i.e., a different domain, protocol, or port than your web application), you are likely to encounter Cross-Origin Resource Sharing (CORS) errors. The server hosting the PDF must explicitly grant permission via `Access-Control-Allow-Origin` HTTP headers.
fix
Ensure the server hosting the PDF is configured to include appropriate CORS headers (e.g., `Access-Control-Allow-Origin: *` for public PDFs, or `Access-Control-Allow-Origin: your-app-origin.com` for specific origins). The most robust solution is often to serve the PDF from the same origin as your web application.
affects: >=1.0
gotchaPDF.js relies on modern JavaScript features (e.g., Promises, `async/await`, ES modules). For compatibility with older browsers or legacy environments that lack native support for these features, you may need to use the `legacy/` build variant or ensure your build pipeline transpiles the library and includes necessary polyfills.
fix
For broader compatibility, consider using `pdfjs-dist/legacy/build/pdf.js` and `pdfjs-dist/legacy/build/pdf.worker.js`. Alternatively, configure your bundler (e.g., Webpack, Rollup) to transpile `pdfjs-dist` to an older JavaScript target and include polyfills for `Promise` and `async/await`.
affects: >=2.0
Errors
Common errors & fixes
Uncaught (in promise) Error: The "workerSrc" property must be specified.
The path to the PDF.js worker script was not set or was set incorrectly (e.g., to an inaccessible URL) before `pdfjsLib.getDocument` was called.
fix
At the very beginning of your application, before any PDF loading attempts, set `pdfjsLib.GlobalWorkerOptions.workerSrc` to the correct, publicly accessible URL of `pdf.worker.min.js` (e.g., from a CDN or your static assets folder).
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at [PDF URL]. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
The browser prevented the PDF.js library from fetching a PDF file because it originated from a different domain, and the server hosting the PDF did not provide the necessary CORS (Cross-Origin Resource Sharing) headers.
fix
Ensure the server hosting the PDF is configured to send `Access-Control-Allow-Origin` headers that permit requests from your web application's origin. Alternatively, serve the PDF from the same domain as your application.
TypeError: pdfjsLib.getDocument is not a function
This error typically occurs because `pdfjsLib` was imported incorrectly, or the specific `pdfjs-dist` build that includes the `getDocument` function was not loaded, or the environment is incompatible (e.g., attempting browser-specific API usage in a Node.js context without proper setup).
fix
Verify that you are using `import * as pdfjsLib from 'pdfjs-dist';` for modern browser/ESM environments. If in Node.js, ensure you are using a build suitable for Node, which might involve different import paths or worker setup, or consider `pdfjs-dist/build/pdf.mjs`.
Upgrade
Version history
5.6.205latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
pdfjs-dist — npm install pdfjs-dist · libregistry