Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print: Make the firefox printing code able to fail and be re-invoked. #12315

Merged
merged 1 commit into from
Sep 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion web/firefox_print_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ function composePage(
canvasWrapper.appendChild(canvas);
printContainer.appendChild(canvasWrapper);

// A callback for a given page may be executed multiple times for different
// print operations (think of changing the print settings in the browser).
//
// Since we don't support queueing multiple render tasks for the same page
// (and it'd be racy anyways if painting the page is not done in one go) we
// keep track of the last scheduled task in order to properly cancel it before
// starting the next one.
let currentRenderTask = null;
emilio marked this conversation as resolved.
Show resolved Hide resolved
canvas.mozPrintCallback = function (obj) {
// Printing/rendering the page.
const ctx = obj.context;
Expand All @@ -50,9 +58,14 @@ function composePage(
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.restore();

let thisRenderTask = null;
pdfDocument
.getPage(pageNumber)
.then(function (pdfPage) {
if (currentRenderTask) {
currentRenderTask.cancel();
currentRenderTask = null;
}
const renderContext = {
canvasContext: ctx,
transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
Expand All @@ -61,15 +74,25 @@ function composePage(
annotationStorage: pdfDocument.annotationStorage,
optionalContentConfigPromise,
};
return pdfPage.render(renderContext).promise;
currentRenderTask = thisRenderTask = pdfPage.render(renderContext);
return thisRenderTask.promise;
})
.then(
function () {
// Tell the printEngine that rendering this canvas/page has finished.
if (currentRenderTask === thisRenderTask) {
currentRenderTask = null;
}
obj.done();
},
function (error) {
console.error(error);

if (currentRenderTask === thisRenderTask) {
currentRenderTask.cancel();
currentRenderTask = null;
}

// Tell the printEngine that rendering this canvas/page has failed.
// This will make the print process stop.
if ("abort" in obj) {
Expand Down