Printing one or more base64 encoded documents with feature control and progress callbacks

Activity log  ::  

Asynchronous printing of one or more base64 encoded PDF documents and/or files, along with monitoring the progress of each job in script is available with the  BatchPrintPDFEx API . With batch printing, files are downloaded and printed in the background, allowing users to continue with other tasks on the displayed page.

NOTE

ScriptX.Addon 8.3.0.4 or ScriptX.Services for Windows PC 2.10.0.16 or ScriptX.Services On Premise 2.10.8.2 (with ScriptX.Server 10.3.0.2 or later) is required.

Performance gotchas

BatchPrintPDFEx() provides an excellent way to print a single base64 encoded PDF while maintaining responsive behaviour in the browser. It is possible to queue more than one base64 encoded PDF but this may require significant quantities of RAM as each base64 representation must be stored in the browser javascript engine, the ScriptX queue and then memory required for decoding and finally the de-coded PDF.

Multiple base64 encoded documents and/or files may be queued for printing and as with  BatchPrintPDFEx API different options/features used for each print. For example, a different printer may be selected for each print, or different page ranges printed to different printers/trays.

Since the printing is asynchronous then the  IsSpooling API or the  WaitForSpoolingComplete API can be used to start and end a suitable UI to show the user that printing is in progress. WaitForSpoolingComplete() is used in this sample.

Progress monitoring is va a callback function that may used for example to alert the user to a failure in the print.

Use with ScriptX.Services

BatchPrintPDFEx() is an API provided by ScriptX.Add-on. An emulation of this API is implemented in  MeadCo ScriptX.Services print client modules . These are used throughtout these samples to allow "single source". An alterative is to use the  ScriptX.Services Print API 'directly'.

This is a simple example of printing a base64 encoded document and a range of pages from a "static" document to the chosen printer with BatchPrintPDFEx which results in two separate prints. A 'real life' example might use different trays for each print.

function log(s) {
    $("#logoutput").append(s + "
"); } function monitorPrint(status, statusData, myData) { // log("Job progress, status: " + status + ", data: [" + statusData + "], myData: " + myData); switch (status) { case 1: log("Job: " + myData + " has been queued"); break; case 2: log("Job: " + myData + " has started"); break; case 3: log("Job: " + myData + " has started downloading url: " + statusData); break; case 4: log("Job: " + myData + " has downloaded: " + statusData); break; case 5: log("Job: " + myData + " is printing"); break; case 6: log("Job: " + myData + " has completed"); break; case 7: log("Job: " + myData + " has been paused"); break; case 8: log("Job: " + myData + " is printing a PDF document: " + statusData); break; case -1: log("Job: " + myData + " has encountered an error: [" + statusData + "]"); break; case -2: log("Job: " + myData + " is being abandoned"); break; default: log("Job: " + myData + " unknown status: " + status); break; } } // dataUrlForPDF // // Return the base64 encoded PDF as a dataURL. Server side code would inline the encoded data // function dataUrlForPDF() { return "data:application/pdf;base64,JVBERi0xLjUKJYCBgoMKMSAwIG9iago8PC9GaWx0ZXIvRmxhdGVEZWNvZGUvRmlyc3QgMTQxL04gMjAvTGVuZ3==" } // setupPrinting // // Initialise print setup used by all jobs. // function setupPrinting() { var printer = MeadCo.ScriptX.Printing; printer.printer = $("#fld-pdfprinter").val(); printer.orientation = $('input[type=radio][name=fld-pdforientation]:checked').val(); printer.paperSize = $("#fld-pdfpapersize").val(); printer.paperSource2 = "" + $("#fld-pdfpapersource").val(); printer.copies = 1; } function PrintPDFFiles() { setupPrinting(); MeadCo.ScriptX.Printing.SetPrintScale(MeadCo.PDFOptionsUI.ShrinkToFitChecked ? -1 : 100); console.log("printing to: " + MeadCo.ScriptX.Printing.printer); var printing = MeadCo.ScriptX.Printing; printing.BatchPrintPDFEx(dataUrlForPDF(),monitorPrint,"Job1"); printing.SetPageRange(false, 3, 100); // large number for print to end of document printing.BatchPrintPDFEx("/Content/PDF/Fourpages.pdf",monitorPrint,"Job2"); // UI until both prints are spooled app.PrintSpooling.Show(); }
<body>
<button id="idPrint" onclick="PrintPDFFiles()">Print reports</button>
</body>