Registry / web-framework / pdfjs-dist-dj

pdfjs-dist-dj

JSON →
library2.5.2jsnpmunverified

PDF.js is an open-source, HTML5-based Portable Document Format (PDF) library developed by Mozilla, designed for parsing and rendering PDF files directly in the browser. The `pdfjs-dist-dj` package provides a generic, pre-built distribution of PDF.js, specifically based on version 2.5.2 of the core library. This version, while functional, is several major versions behind the actively maintained official `pdfjs-dist` package. Its primary purpose is to offer a readily consumable package for integrating PDF viewing capabilities into web applications without requiring a separate build process. Key differentiators include its web-standards-based approach and direct client-side rendering, avoiding server-side processing for basic PDF display. Due to its age, it may lack modern features, performance optimizations, and security updates found in more recent PDF.js releases.

npm install pdfjs-dist-dj
INSTALL
IMPORT
SIG · PDFJS-DIST-DJ
P
pdfjs-dist-dj
web-frameworkjavascriptv2.5.2
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/build/pdf';
const pdfjsLib = require('pdfjs-dist');
The main entry point for PDF.js functionality. Direct `require('pdfjs-dist')` often points to an incorrect or incomplete build, especially in mixed CJS/ESM environments or older versions.
GlobalWorkerOptions
import { GlobalWorkerOptions } from 'pdfjs-dist/build/pdf';
pdfjsLib.workerSrc = '...';
Since PDF.js v2, worker options are managed via `GlobalWorkerOptions`. Directly setting `pdfjsLib.workerSrc` is an older, less reliable pattern. This is crucial for configuring the worker path.
Worker Entry Point (for bundlers)
import 'pdfjs-dist/build/pdf.worker.entry';
import { PDFWorker } from 'pdfjs-dist/build/pdf.worker';
This specific import is a common pattern for modern bundlers (like Webpack or Rollup) to automatically handle the worker script. For non-bundled or older environments, you *must* explicitly set `GlobalWorkerOptions.workerSrc` to the URL of `pdf.worker.js`.

This code snippet demonstrates how to load a PDF from a URL and render its first page onto a specified HTML canvas element using PDF.js v2.5.2. It includes worker configuration considerations.

import * as pdfjsLib from 'pdfjs-dist/build/pdf'; // For modern bundlers, importing the worker entry point can automatically configure the worker. // If this does not work or for more control, explicitly set pdfjsLib.GlobalWorkerOptions.workerSrc. import 'pdfjs-dist/build/pdf.worker.entry'; async function renderPdf(pdfUrl: string, canvasId: string) { // For production deployments, ensure `pdf.worker.js` is accessible in your public directory // and set its path here if `pdf.worker.entry` import isn't sufficient. // Example: pdfjsLib.GlobalWorkerOptions.workerSrc = '/static/js/pdf.worker.js'; try { const loadingTask = pdfjsLib.getDocument(pdfUrl); const pdf = await loadingTask.promise; const pageNumber = 1; const page = await pdf.getPage(pageNumber); const scale = 1.5; const viewport = page.getViewport({ scale: scale }); // Prepare canvas using PDF page dimensions const canvas = document.getElementById(canvasId) as HTMLCanvasElement; if (!canvas) { console.error(`Canvas element with ID '${canvasId}' not found. Ensure it exists in your HTML.`); return; } const context = canvas.getContext('2d'); if (!context) { console.error(`Could not get 2D rendering context for canvas '${canvasId}'.`); return; } canvas.height = viewport.height; canvas.width = viewport.width; const renderContext = { canvasContext: context, viewport: viewport, }; await page.render(renderContext).promise; console.log(`PDF page ${pageNumber} rendered successfully to canvas: ${canvasId}`); } catch (error) { console.error('Error rendering PDF:', error); } } // To run this, add `<canvas id="pdf-canvas"></canvas>` to your HTML body. // Replace the example URL with your desired PDF document's URL. const examplePdfUrl = 'https://raw.githubusercontent.com/mozilla/pdf.js/ba24304899580b080f5d54030d321d5c22501d51/web/compressed.tracemonkey-pldi-09.pdf'; renderPdf(examplePdfUrl, 'pdf-canvas');
Debug
Known issues
breakingThe `pdfjs-dist-dj` package is based on PDF.js v2.5.2, which is an older major version. This means it lacks features, performance improvements, and critical security patches present in current official `pdfjs-dist` releases (v3.x and v4.x as of late 2024).
fix
For new projects or existing applications requiring modern features and security, it is highly recommended to migrate to the latest official `pdfjs-dist` package from npm (`npm install pdfjs-dist`). Review its documentation for updated API changes and worker setup.
affects: >=2.0.0
gotchaProperly configuring the Web Worker for PDF.js is a frequent pitfall. The worker script (`pdf.worker.js`) must be accessible via a URL from the browser, and its path explicitly set via `pdfjsLib.GlobalWorkerOptions.workerSrc`.
fix
Ensure `pdf.worker.js` (found in `node_modules/pdfjs-dist/build/`) is copied to your public assets directory and set `pdfjsLib.GlobalWorkerOptions.workerSrc = '/your-public-path/pdf.worker.js';` before calling `getDocument()`. Modern bundler-specific imports like `import 'pdfjs-dist/build/pdf.worker.entry'` may simplify this but require correct bundler configuration.
affects: >=2.0.0
gotchaRendering large or complex PDF documents can consume significant memory and CPU resources, potentially leading to slow performance, high battery usage, or browser crashes, especially on older devices or when many pages are loaded concurrently.
fix
Implement strategies like lazy loading pages, rendering pages on demand (e.g., only visible pages in a scrollable view), virtualizing scrollable views, and carefully managing memory by releasing resources (e.g., `pdf.destroy()`) when documents or pages are no longer needed.
affects: >=2.0.0
deprecatedDirect CommonJS `require('pdfjs-dist')` or similar patterns without specifying the full path (e.g., `/build/pdf`) may load an incorrect or incomplete bundle, leading to missing functionality or runtime errors, particularly in environments with strict module resolution or mixed CJS/ESM usage.
fix
Always use specific import paths like `import * as pdfjsLib from 'pdfjs-dist/build/pdf';` for ESM, or ensure your CommonJS setup explicitly requires the appropriate build artifact (e.g., `require('pdfjs-dist/build/pdf')`).
affects: <3.0.0
Errors
Common errors & fixes
Unhandled Rejection (Error): Loading PDF.js worker failed
The browser could not find or load the PDF.js worker script at the path specified by `GlobalWorkerOptions.workerSrc`.
fix
Verify that `pdf.worker.js` is correctly placed in a web-accessible directory on your server or CDN, and that `pdfjsLib.GlobalWorkerOptions.workerSrc` (or your bundler's configuration) points to its exact public URL. Check browser console for network errors during worker script loading.
ReferenceError: pdfjsLib is not defined
The main PDF.js library object (`pdfjsLib`) was not correctly imported, required, or made globally available before being used in the consuming script or module.
fix
Ensure you have `import * as pdfjsLib from 'pdfjs-dist/build/pdf';` (or the appropriate `require` statement for CommonJS) at the top of your module, or if using a global script, that PDF.js has been loaded and initialized before your code attempts to use it.
TypeError: Cannot read properties of null (reading 'getContext')
The HTML canvas element targeted for rendering was not found in the Document Object Model (DOM) or was not fully loaded/ready when `getContext('2d')` was called on it.
fix
Verify that the `canvasId` used in your JavaScript corresponds to an existing `<canvas>` element in your HTML. Ensure the rendering logic runs only after the DOM is fully loaded (e.g., within a `DOMContentLoaded` event listener, or after the element is mounted in a component lifecycle method of a UI framework).
Upgrade
Version history
2.5.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

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