56a8c934dd
Despite this patch removing the `disableWorker` option itself, please note that we'll still fallback to loading the worker file(s) on the main-thread when running in environments without proper Web Worker support. Furthermore it's still possible, even with this patch, to force the use of fake workers by manually loading the necessary file using a `<script>` tag on the main-thread.[1] That way, the functionality of the now removed `SINGLE_FILE` build target and the resulting `build/pdf.combined.js` file can still be achieved simply by adding e.g. `<script src="build/pdf.worker.js"></script>` to the HTML (obviously with the path adjusted as needed). Finally note that the `disableWorker` option is a performance footgun, and unfortunately many existing third-party examples actually use it without providing any sort of warning/justification. --- [1] This approach is used in the default viewer, since certain kind of debugging may be easier if the code is running directly on the main-thread.
130 lines
3.1 KiB
HTML
130 lines
3.1 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Previous/Next example</title>
|
|
</head>
|
|
<body>
|
|
|
|
<h1>'Previous/Next' example</h1>
|
|
|
|
<div>
|
|
<button id="prev">Previous</button>
|
|
<button id="next">Next</button>
|
|
|
|
<span>Page: <span id="page_num"></span> / <span id="page_count"></span></span>
|
|
</div>
|
|
|
|
<div>
|
|
<canvas id="the-canvas" style="border:1px solid black"></canvas>
|
|
</div>
|
|
|
|
<script src="../../node_modules/pdfjs-dist/build/pdf.js"></script>
|
|
|
|
<script id="script">
|
|
//
|
|
// If absolute URL from the remote server is provided, configure the CORS
|
|
// header on that server.
|
|
//
|
|
var url = '../../web/compressed.tracemonkey-pldi-09.pdf';
|
|
|
|
//
|
|
// In cases when the pdf.worker.js is located at the different folder than the
|
|
// pdf.js's one, or the pdf.js is executed via eval(), the workerSrc property
|
|
// shall be specified.
|
|
//
|
|
// PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
|
|
|
var pdfDoc = null,
|
|
pageNum = 1,
|
|
pageRendering = false,
|
|
pageNumPending = null,
|
|
scale = 0.8,
|
|
canvas = document.getElementById('the-canvas'),
|
|
ctx = canvas.getContext('2d');
|
|
|
|
/**
|
|
* Get page info from document, resize canvas accordingly, and render page.
|
|
* @param num Page number.
|
|
*/
|
|
function renderPage(num) {
|
|
pageRendering = true;
|
|
// Using promise to fetch the page
|
|
pdfDoc.getPage(num).then(function(page) {
|
|
var viewport = page.getViewport(scale);
|
|
canvas.height = viewport.height;
|
|
canvas.width = viewport.width;
|
|
|
|
// Render PDF page into canvas context
|
|
var renderContext = {
|
|
canvasContext: ctx,
|
|
viewport: viewport
|
|
};
|
|
var renderTask = page.render(renderContext);
|
|
|
|
// Wait for rendering to finish
|
|
renderTask.promise.then(function () {
|
|
pageRendering = false;
|
|
if (pageNumPending !== null) {
|
|
// New page rendering is pending
|
|
renderPage(pageNumPending);
|
|
pageNumPending = null;
|
|
}
|
|
});
|
|
});
|
|
|
|
// Update page counters
|
|
document.getElementById('page_num').textContent = num;
|
|
}
|
|
|
|
/**
|
|
* If another page rendering in progress, waits until the rendering is
|
|
* finised. Otherwise, executes rendering immediately.
|
|
*/
|
|
function queueRenderPage(num) {
|
|
if (pageRendering) {
|
|
pageNumPending = num;
|
|
} else {
|
|
renderPage(num);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Displays previous page.
|
|
*/
|
|
function onPrevPage() {
|
|
if (pageNum <= 1) {
|
|
return;
|
|
}
|
|
pageNum--;
|
|
queueRenderPage(pageNum);
|
|
}
|
|
document.getElementById('prev').addEventListener('click', onPrevPage);
|
|
|
|
/**
|
|
* Displays next page.
|
|
*/
|
|
function onNextPage() {
|
|
if (pageNum >= pdfDoc.numPages) {
|
|
return;
|
|
}
|
|
pageNum++;
|
|
queueRenderPage(pageNum);
|
|
}
|
|
document.getElementById('next').addEventListener('click', onNextPage);
|
|
|
|
/**
|
|
* Asynchronously downloads PDF.
|
|
*/
|
|
PDFJS.getDocument(url).then(function (pdfDoc_) {
|
|
pdfDoc = pdfDoc_;
|
|
document.getElementById('page_count').textContent = pdfDoc.numPages;
|
|
|
|
// Initial/first page rendering
|
|
renderPage(pageNum);
|
|
});
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|