This project is not covered by Drupal’s security advisory policy.

PDF Lazy Viewer adds a field formatter that displays PDF files with the PDF.js viewer, and only downloads the pages being read.

By default, the PDF.js viewer downloads the whole file, even when the visitor only reads the first page. With documents of several dozen megabytes, such as reports or catalogs, this wastes bandwidth and slows down the page. This module keeps the native PDF.js viewer, but makes it load the document progressively.

The same features are proposed for the PDF module in #3629209. If they are added to it, this module will be deprecated in its favor.

Features

  • Loading when visible: nothing is downloaded when the page loads. Each viewer loads when it becomes visible, and a PDF lower in the page downloads nothing until the visitor scrolls to it.
  • Page by page download: the viewer downloads the pages with HTTP range requests, as they are read, instead of the whole file.
  • Independent viewers: each viewer of the page loads its own pages, independently of the others.
  • Native PDF.js toolbar: zoom, search, thumbnails, full screen, print and download are kept.
  • Settings: viewer height, initial zoom, download link with the file size, and an option to turn off the progressive loading.
  • Fallback: without JavaScript, or when PDF.js is not installed, a download link is displayed instead of the viewer.
  • Respects the cache metadata and the access control of files, like the file formatters of Drupal core.
  • PDF.js is not modified: the module sets the options of the viewer when it starts.

Requirements

  • Drupal 11.4 or later, or Drupal 12.
  • The PDF.js viewer ("modern browsers" build), installed in the libraries/pdf.js directory of the web root. The README explains how to install it with Composer.
  • A web server answering HTTP range requests on PDF files (206 Partial Content), which is the case by default for public files served by Apache or nginx.
  • The PDF files must be served from the same domain as the site: files on a CDN or on a remote storage, such as Amazon S3, are not supported.

Installation

composer require 'drupal/pdf_lazy:^1.0@alpha'
drush pm:install pdf_lazy

Then go to the "Manage display" page of an entity type with a File field, and select the PDF: Lazy viewer format.

Known limitation

When it opens a document, PDF.js checks that its last page can be loaded. When the page tree of the file is flat, as written by Ghostscript and most scanners, PDF.js reads the description of every page first: for a document of several hundred pages, this downloads several megabytes before the first page is displayed. The pages are then downloaded as they are read. This is a design choice of PDF.js (pdf.js#21859), which this module cannot avoid.

Similar projects

  • PDF displays PDF files with PDF.js too, with the default options of the viewer: the whole file is downloaded. PDF Lazy Viewer focuses on the progressive loading of large documents, and does not depend on it. Its features are proposed for the PDF module in #3629209.

Documentation

See the README file of the module for the installation of PDF.js, the configuration and the troubleshooting, in particular to check that your web server answers range requests.

Supporting organizations: 

Project information

  • Project categories: Media
  • Created by pogfra on , updated
  • shield alertThis project is not covered by the security advisory policy.
    Use at your own risk! It may have publicly disclosed vulnerabilities.

Releases