Merge pull request #12315 from emilio/more-resilient-print-callback
print: Make the firefox printing code able to fail and be re-invoked.
This commit is contained in:
commit
84da13b487
@ -41,6 +41,14 @@ function composePage(
|
|||||||
canvasWrapper.appendChild(canvas);
|
canvasWrapper.appendChild(canvas);
|
||||||
printContainer.appendChild(canvasWrapper);
|
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;
|
||||||
canvas.mozPrintCallback = function (obj) {
|
canvas.mozPrintCallback = function (obj) {
|
||||||
// Printing/rendering the page.
|
// Printing/rendering the page.
|
||||||
const ctx = obj.context;
|
const ctx = obj.context;
|
||||||
@ -50,9 +58,14 @@ function composePage(
|
|||||||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||||
ctx.restore();
|
ctx.restore();
|
||||||
|
|
||||||
|
let thisRenderTask = null;
|
||||||
pdfDocument
|
pdfDocument
|
||||||
.getPage(pageNumber)
|
.getPage(pageNumber)
|
||||||
.then(function (pdfPage) {
|
.then(function (pdfPage) {
|
||||||
|
if (currentRenderTask) {
|
||||||
|
currentRenderTask.cancel();
|
||||||
|
currentRenderTask = null;
|
||||||
|
}
|
||||||
const renderContext = {
|
const renderContext = {
|
||||||
canvasContext: ctx,
|
canvasContext: ctx,
|
||||||
transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
|
transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
|
||||||
@ -61,15 +74,25 @@ function composePage(
|
|||||||
annotationStorage: pdfDocument.annotationStorage,
|
annotationStorage: pdfDocument.annotationStorage,
|
||||||
optionalContentConfigPromise,
|
optionalContentConfigPromise,
|
||||||
};
|
};
|
||||||
return pdfPage.render(renderContext).promise;
|
currentRenderTask = thisRenderTask = pdfPage.render(renderContext);
|
||||||
|
return thisRenderTask.promise;
|
||||||
})
|
})
|
||||||
.then(
|
.then(
|
||||||
function () {
|
function () {
|
||||||
// Tell the printEngine that rendering this canvas/page has finished.
|
// Tell the printEngine that rendering this canvas/page has finished.
|
||||||
|
if (currentRenderTask === thisRenderTask) {
|
||||||
|
currentRenderTask = null;
|
||||||
|
}
|
||||||
obj.done();
|
obj.done();
|
||||||
},
|
},
|
||||||
function (error) {
|
function (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
||||||
|
if (currentRenderTask === thisRenderTask) {
|
||||||
|
currentRenderTask.cancel();
|
||||||
|
currentRenderTask = null;
|
||||||
|
}
|
||||||
|
|
||||||
// Tell the printEngine that rendering this canvas/page has failed.
|
// Tell the printEngine that rendering this canvas/page has failed.
|
||||||
// This will make the print process stop.
|
// This will make the print process stop.
|
||||||
if ("abort" in obj) {
|
if ("abort" in obj) {
|
||||||
|
Loading…
Reference in New Issue
Block a user