← Back to archive

Repo of the Day

mozilla/pdf.js

Published: Dec 29, 2021

Open repository ↗

PDF.js is a Portable Document Format (PDF) viewer that is built with HTML5.

Summary

Mozilla's PDF.js is a JavaScript PDF viewer built on HTML5 that parses and renders PDFs in the browser without plugins. It is Apache-2.0 licensed, community-driven, and ships both a ready-made viewer and an embeddable library (pdfjs-dist) usable from a CDN or npm.

What it is useful for

  • Embedding PDF viewing in a web application without third-party plugins or native code.
  • Building browser-based PDF tools that parse content (text extraction, page rendering) entirely in JavaScript.
  • Powering the PDF experience in Firefox (it is built into Firefox 19+, per the README) and as a Chrome extension maintained by a community contributor.
  • Inspecting the internal structure of a PDF document using the included PDF debugger at mozilla.github.io/pdf.js/internal-viewer/web/debugger.html.
  • Supporting older browsers, via the generic-legacy build documented in the "Building PDF.js" section.

How engineers can use it

The README documents three practical entry points.

1. Drop in via CDN. PDF.js is hosted on jsDelivr, cdnjs, and unpkg under the pdfjs-dist name. This is the fastest path for prototypes.

2. Install as a package. Use the pdfjs-dist npm package. The README points to the wiki page "Setup pdf.js in a website" for usage examples and notes that examples in the examples/ folder can be installed locally with npx gulp dist-install.

3. Build from source. Documented commands from the README:

git clone https://github.com/mozilla/pdf.js.git
cd pdf.js
npm install
npx gulp server          # local dev server at http://localhost:8888/web/viewer.html
npx gulp generic         # production build -> build/generic/build/pdf.js + pdf.worker.js
npx gulp generic-legacy  # build for older browsers
npx gulp chromium        # build the Chrome extension into build/chromium

Only pdf.js needs to be included in a page; pdf.worker.js is loaded by it automatically. The README warns that browsers will not open PDFs over a file:// URL, so a local server is required during development, and that production builds should be minified since the files are large.

For learning the API, the README links to interactive examples at mozilla.github.io/pdf.js/examples/index.html#interactive-examples and the API reference at mozilla.github.io/pdf.js/api/. Limitations and known issues are not itemized in the README; the "Frequently Asked Questions" wiki page (linked from the README) is the documented place to check support expectations before committing to it for a project.