aab0f91740
For performance reasons, and to avoid hanging the browser UI, the PDF.js library should *always* be used with web workers enabled. At this point in time all of the supported browsers should have proper worker support, and Node.js is thus the only environment where workers aren't supported. Hence it no longer seems relevant/necessary to provide, by default, fake worker loaders for various JS builders/bundlers/frameworks in the PDF.js code itself.[1] In order to simplify things, the fake worker loader code is thus simplified to now *only* support Node.js usage respectively "normal" browser usage out-of-the-box.[2] *Please note:* The officially intended way of using the PDF.js library is with workers enabled, which can be done by setting `GlobalWorkerOptions.workerSrc`, `GlobalWorkerOptions.workerPort`, or manually providing a `PDFWorker` instance when calling `getDocument`. --- [1] Note that it's still possible to *manually* disable workers, simply my manually loading the built `pdf.worker.js` file into the (current) global scope, however this's mostly intended for testing/debugging purposes. [2] Unfortunately some bundlers such as Webpack, when used with third-party deployments of the PDF.js library, will start to print `Critical dependency: ...` warnings when run against the built `pdf.js` file from this patch. The reason is that despite the `require` calls being protected by *runtime* `isNodeJS` checks, it's not possible to simply tell Webpack to just ignore the `require`; please see [Webpack issue 8826](https://github.com/webpack/webpack) and libraries such as [require-fool-webpack](https://github.com/sindresorhus/require-fool-webpack). |
||
---|---|---|
.. | ||
.eslintrc | ||
.gitignore | ||
index.html | ||
main.js | ||
package.json | ||
README.md | ||
webpack.config.js |
Overview
Example to demonstrate PDF.js library usage with Webpack.
Getting started
Install the example dependencies and build the project:
$ gulp dist-install
$ cd examples/webpack
$ npm install
$ ./node_modules/webpack/bin/webpack.js
You can observe the build results by running gulp server
and navigating to
http://localhost:8888/examples/webpack/index.html.
Refer to the main.js
and webpack.config.js
files for the source code.
Note that PDF.js packaging requires packaging of the main application and
the worker code, and the workerSrc
path shall be set to the latter file.
Worker loading
If you are getting the Setting up fake worker
warning, make sure you are importing pdfjs-dist/webpack
which is the zero-configuration method for Webpack users:
import pdfjsLib from 'pdfjs-dist/webpack';
For a full working example refer to this repository.