2016-04-05 01:32:01 +09:00
|
|
|
// Any copyright is dedicated to the Public Domain.
|
|
|
|
// http://creativecommons.org/licenses/publicdomain/
|
|
|
|
|
|
|
|
// Hello world example for browserify.
|
|
|
|
|
2018-02-18 07:51:24 +09:00
|
|
|
var pdfjsLib = require('pdfjs-dist');
|
2016-04-05 01:32:01 +09:00
|
|
|
|
[api-minor] Simplify the *fallback* fake worker loader code in `src/display/api.js`
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).
2019-12-20 00:37:03 +09:00
|
|
|
var pdfPath = '../learning/helloworld.pdf';
|
2016-04-05 01:32:01 +09:00
|
|
|
|
2016-04-13 22:24:25 +09:00
|
|
|
// Setting worker path to worker bundle.
|
2018-02-18 07:51:24 +09:00
|
|
|
pdfjsLib.GlobalWorkerOptions.workerSrc =
|
2018-02-14 22:49:24 +09:00
|
|
|
'../../build/browserify/pdf.worker.bundle.js';
|
2016-04-05 01:32:01 +09:00
|
|
|
|
|
|
|
// Loading a document.
|
2018-02-18 07:51:24 +09:00
|
|
|
var loadingTask = pdfjsLib.getDocument(pdfPath);
|
2016-04-05 01:32:01 +09:00
|
|
|
loadingTask.promise.then(function (pdfDocument) {
|
|
|
|
// Request a first page
|
|
|
|
return pdfDocument.getPage(1).then(function (pdfPage) {
|
|
|
|
// Display page on the existing canvas with 100% scale.
|
2018-12-21 19:47:37 +09:00
|
|
|
var viewport = pdfPage.getViewport({ scale: 1.0, });
|
2016-04-05 01:32:01 +09:00
|
|
|
var canvas = document.getElementById('theCanvas');
|
|
|
|
canvas.width = viewport.width;
|
|
|
|
canvas.height = viewport.height;
|
|
|
|
var ctx = canvas.getContext('2d');
|
|
|
|
var renderTask = pdfPage.render({
|
|
|
|
canvasContext: ctx,
|
2018-12-06 21:55:15 +09:00
|
|
|
viewport: viewport,
|
2016-04-05 01:32:01 +09:00
|
|
|
});
|
|
|
|
return renderTask.promise;
|
|
|
|
});
|
|
|
|
}).catch(function (reason) {
|
|
|
|
console.error('Error: ' + reason);
|
|
|
|
});
|