#260 More user friendly printing

Erik Huelsmann

There's a trick on stackoverflow to use PDF documents as good printing interface from webapplications. (See http://stackoverflow.com/questions/687675/can-a-pdf-files-print-dialog-be-opened-with-javascript for the whole story.)

Summary extract:

I usually do something similar to the approach given by How to Use JavaScript to Print a PDF (eHow.com), using an iframe.

a function to house the print trigger...

function printTrigger(elementId) {
var getMyFrame = document.getElementById(elementId);
an button to give the user access...

(an onClick on an a or button or input or whatever you wish)

<input type="button" value="Print" onclick="printTrigger('iFramePdf');" />
an iframe pointing to your PDF...

<iframe id="iFramePdf" src="myPdfUrl.pdf" style="dispaly:none;"></iframe>
Bonus Idea #1 - Create the iframe and add it to your page within the printTrigger(); so that the PDF isn't loaded until the user clicks your "Print" button, then the javascript can attack! the iframe and trigger the print dialog.

Bonus Idea #2 - Extra credit if you disable your "Print" button and give the user a little loading spinner or something after they click it, so that they know something's in process instead of clicking it repeatedly!


Remaining issue is how to avoid the complexity of using LaTeX/XeLaTeX for every individual print job. Havard Sorli suggests the use of CUPS-PDF to that extent.