ViewerJS toolbar preview of a multi-page PDF in an overlay

ViewerJS adds a field formatter that previews files without leaving the page. PDFs are rendered with
PDF.js in a sandboxed frame with its own toolbar (page indicator, zoom, fit width,
fullscreen, download, print); JPEG, PNG, GIF and WebP images are shown natively. Other file types get a download link.

Features

  • Works with file and image fields. Choose "ViewerJS" in Manage display.
  • Three modes: overlay (modal dialog), new window, or inline.
  • Private files supported: previews follow Drupal file access.
  • Strict Content-Security-Policy on the viewer frame, no third-party services, works offline.
  • No Libraries module, no jQuery.

Requirements

  • Drupal 10.3 or 11 and the core File module.
  • pdfjs-dist 4.2.67 or newer in libraries/pdfjs-dist
    (see Installation; drush viewerjs:install-library does it for you). Without it PDFs are download-only. The Status report shows whether it is detected.

Installation

  1. Install the module: composer require drupal/viewerjs:^2.0@alpha and enable it.
  2. Install PDF.js: drush viewerjs:install-library. It downloads pdfjs-dist from the npm registry, verifies its checksum and puts it in libraries/pdfjs-dist. Run it again to update PDF.js.

    Without Drush: unpack the pdfjs-dist package into libraries/pdfjs-dist, or, if your site uses asset-packagist, composer require npm-asset/pdfjs-dist.
  3. Check Reports > Status report for the PDF.js entry.
  4. If you installed PDF.js with Composer or by hand, block libraries/pdfjs-dist/web/viewer.html at the web server or delete it. The module does not use it. (The Drush command never installs it.)
  5. In Manage display of a file or image field, choose the "ViewerJS" formatter and pick the preview type (overlay, new window, inline) and size.

Versions

2.0.x (Drupal 10.3+/11) is a complete rewrite and the only supported branch. The 7.x and 8.x branches used the
abandoned ViewerJS library (bundling a 2014 PDF.js with known vulnerabilities) and are no longer supported.
Re-select the formatter when moving to 2.0.x; the color, thumbnail and template settings were removed.

Credits

Preview engine: PDF.js by Mozilla (Apache-2.0).

Project information

Releases