Printing pages in batches

37 views
Skip to first unread message

Matt Parizeau

unread,
Mar 14, 2018, 2:33:42 PM3/14/18
to PDFTron WebViewer
Q:

Users may print documents that have many (> 100) pages and if we set the print quality higher than 1 there can be memory issues. Is there a way we could reduce the amount of memory used?

A:

Since there currently is no browser API to stream pages to the printer you could print pages in batches that automatically run one after the other. The trade-off is that the user will have to press "print" on the native print dialog for each batch.

To print pages in batches you'll need to find the print function in ReaderControl.js and make a change there. Inside the print function you'll find the setup of the #printDialog and then the click handler inside the buttons array. Change it from:

me.startPrintJob(printPageNumbers, me.pageOrientations.Auto, false, function() {
    window
.print();
});

to:

var batchSize = 30;

var runBatch = function(startIndex) {
   
if (startIndex >= printPageNumbers.length) {
       
return;
   
}

    me
.endPrintJob();
    me
.startPrintJob(printPageNumbers.slice(startIndex, startIndex + batchSize), me.pageOrientations.Auto, false, function() {
        window
.print();

       
if (window.utils.isEdge || window.utils.ie || window.utils.isSafari) {
            $
(window).one('focus', function() {
                runBatch
(startIndex + batchSize);
           
});
       
} else {
            setTimeout
(function() {
                runBatch
(startIndex + batchSize);
           
}, 0);
       
}
   
});
};

runBatch
(0);

It seems that different browsers have slightly different handling of window.print which is why we need the separate cases. In Firefox and Chrome the execution of JavaScript is suspended whereas in IE, Edge and Safari it is not. However in those browsers the window is focused after the print dialog is closed so we can listen for that event to trigger the run of the next batch. One other thing to note is that in Firefox and Safari the second and following times that the print dialog is displayed they will display their own dialog basically asking if you want to let the browser open another print dialog. So in those cases there will be two clicks required.
Reply all
Reply to author
Forward
0 new messages