Go to file
Jonas Jenwald dda6626f40 Attempt to cache repeated images at the document, rather than the page, level (issue 11878)
Currently image resources, as opposed to e.g. font resources, are handled exclusively on a page-specific basis. Generally speaking this makes sense, since pages are separate from each other, however there's PDF documents where many (or even all) pages actually references exactly the same image resources (through the XRef table). Hence, in some cases, we're decoding the *same* images over and over for every page which is obviously slow and wasting both CPU and memory resources better used elsewhere.[1]

Obviously we cannot simply treat all image resources as-if they're used throughout the entire PDF document, since that would end up increasing memory usage too much.[2]
However, by introducing a `GlobalImageCache` in the worker we can track image resources that appear on more than one page. Hence we can switch image resources from being page-specific to being document-specific, once the image resource has been seen on more than a certain number of pages.

In many cases, such as e.g. the referenced issue, this patch will thus lead to reduced memory usage for image resources. Scrolling through all pages of the document, there's now only a few main-thread copies of the same image data, as opposed to one for each rendered page (i.e. there could theoretically be *twenty* copies of the image data).
While this obviously benefit both CPU and memory usage in this case, for *very* large image data this patch *may* possibly increase persistent main-thread memory usage a tiny bit. Thus to avoid negatively affecting memory usage too much in general, particularly on the main-thread, the `GlobalImageCache` will *only* cache a certain number of image resources at the document level and simply fallback to the default behaviour.

Unfortunately the asynchronous nature of the code, with ranged/streamed loading of data, actually makes all of this much more complicated than if all data could be assumed to be immediately available.[3]

*Please note:* The patch will lead to *small* movement in some existing test-cases, since we're now using the built-in PDF.js JPEG decoder more. This was done in order to simplify the overall implementation, especially on the main-thread, by limiting it to only the `OPS.paintImageXObject` operator.

---
[1] There's e.g. PDF documents that use the same image as background on all pages.

[2] Given that data stored in the `commonObjs`, on the main-thread, are only cleared manually through `PDFDocumentProxy.cleanup`. This as opposed to data stored in the `objs` of each page, which is automatically removed when the page is cleaned-up e.g. by being evicted from the cache in the default viewer.

[3] If the latter case were true, we could simply check for repeat images *before* parsing started and thus avoid handling *any* duplicate image resources.
2020-05-21 18:13:45 +02:00
.github Update links from IRC to Matrix. 2020-02-27 16:26:17 -08:00
docs Update the getting started page of the website for the new release 2020-03-19 23:07:45 +01:00
examples Enable the ESLint grouped-accessor-pairs rule 2020-05-07 11:43:19 +02:00
extensions Update Prettier to version 2.0 2020-04-14 12:28:14 +02:00
external Reduce usage of SystemJS, in the development viewer, even further 2020-05-20 13:36:52 +02:00
l10n Update l10n files 2020-05-16 11:47:08 +02:00
src Attempt to cache repeated images at the document, rather than the page, level (issue 11878) 2020-05-21 18:13:45 +02:00
test Attempt to cache repeated images at the document, rather than the page, level (issue 11878) 2020-05-21 18:13:45 +02:00
web Reduce usage of SystemJS, in the development viewer, even further 2020-05-20 13:36:52 +02:00
.editorconfig Uses editorconfig to maintain consistent coding styles 2015-11-14 07:32:18 +05:30
.eslintignore Replace the bundled ReadableStream polyfill with the web-streams-polyfill npm package (issue 11157) 2019-09-23 22:16:59 +02:00
.eslintrc Reduce usage of SystemJS, in the development viewer, even further 2020-05-20 13:36:52 +02:00
.gitattributes Fixing C++,PHP and Pascal presence in the repo 2015-10-29 13:03:51 -05:00
.gitignore Include package-lock.json for reproducible builds 2018-06-02 20:29:47 +02:00
.gitmodules Update fonttools location and version (issue 6223) 2015-07-17 12:51:09 +02:00
.gitpod.Dockerfile Simplifies code contributions by automating the dev setup with gitpod.io 2019-11-06 04:12:19 +00:00
.gitpod.yml Simplifies code contributions by automating the dev setup with gitpod.io 2019-11-06 04:12:19 +00:00
.mailmap Add mgol's name to AUTHORS, add .mailmap 2017-11-22 10:46:11 +01:00
.prettierrc Update Prettier to version 2.0 2020-04-14 12:28:14 +02:00
.travis.yml Use Node LTS releases to fix Travis CI builds (issue 10790) 2020-04-22 00:06:27 +02:00
AUTHORS Add SehyunPark to AUTHORS 2017-11-29 22:24:08 +09:00
CODE_OF_CONDUCT.md Add Mozilla Code of Conduct file 2019-03-27 21:00:01 -07:00
EXPORT Adds ECCN response statement 2017-10-23 13:31:36 -05:00
gulpfile.js Add a minified-es5 gulp task (issue 11858) 2020-05-10 13:41:42 +02:00
LICENSE cleaned whitespace 2015-02-17 11:07:37 -05:00
package-lock.json Reduce usage of SystemJS, in the development viewer, even further 2020-05-20 13:36:52 +02:00
package.json Reduce usage of SystemJS, in the development viewer, even further 2020-05-20 13:36:52 +02:00
pdfjs.config Bump versions in pdfjs.config 2020-03-19 23:01:17 +01:00
README.md Remove any mention of Gitpod from the README (issue 11732) 2020-04-11 16:47:27 +02:00
systemjs.config.js docs: Fix simple typo, occurences -> occurrences 2020-04-18 07:53:18 +10:00

PDF.js Build Status

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

PDF.js is community-driven and supported by Mozilla Labs. Our goal is to create a general-purpose, web standards-based platform for parsing and rendering PDFs.

Contributing

PDF.js is an open source project and always looking for more contributors. To get involved, visit:

Feel free to stop by our Matrix room for questions or guidance.

Getting Started

Online demo

Please note that the "Modern browsers" version assumes native support for features such as e.g. async/await, Promise, and ReadableStream.

Browser Extensions

Firefox

PDF.js is built into version 19+ of Firefox.

Chrome

  • The official extension for Chrome can be installed from the Chrome Web Store. This extension is maintained by @Rob--W.
  • Build Your Own - Get the code as explained below and issue gulp chromium. Then open Chrome, go to Tools > Extension and load the (unpackaged) extension from the directory build/chromium.

Getting the Code

To get a local copy of the current code, clone it using git:

$ git clone https://github.com/mozilla/pdf.js.git
$ cd pdf.js

Next, install Node.js via the official package or via nvm. You need to install the gulp package globally (see also gulp's getting started):

$ npm install -g gulp-cli

If everything worked out, install all dependencies for PDF.js:

$ npm install

Finally, you need to start a local web server as some browsers do not allow opening PDF files using a file:// URL. Run:

$ gulp server

and then you can open:

Please keep in mind that this requires an ES6 compatible browser; refer to Building PDF.js for usage with older browsers.

It is also possible to view all test PDF files on the right side by opening:

Building PDF.js

In order to bundle all src/ files into two production scripts and build the generic viewer, run:

$ gulp generic

This will generate pdf.js and pdf.worker.js in the build/generic/build/ directory. Both scripts are needed but only pdf.js needs to be included since pdf.worker.js will be loaded by pdf.js. The PDF.js files are large and should be minified for production.

Using PDF.js in a web application

To use PDF.js in a web application you can choose to use a pre-built version of the library or to build it from source. We supply pre-built versions for usage with NPM and Bower under the pdfjs-dist name. For more information and examples please refer to the wiki page on this subject.

Including via a CDN

PDF.js is hosted on several free CDNs:

Learning

You can play with the PDF.js API directly from your browser using the live demos below:

More examples can be found in the examples folder. Some of them are using the pdfjs-dist package, which can be built and installed in this repo directory via gulp dist-install command.

For an introduction to the PDF.js code, check out the presentation by our contributor Julian Viereck:

More learning resources can be found at:

The API documentation can be found at:

Questions

Check out our FAQs and get answers to common questions:

Talk to us on Matrix:

File an issue:

Follow us on twitter: @pdfjs