The goal is to have users able to click a link, and get a new tab/window with the Print dialog open, ready to go, in the same manner as how Google Docs' print function works. It would also be nice if the new tab closed after the print dialog did (i.e. after user has either printed or cancelled).
Turns out, with a little bit of javascript and a beta of DOMPDF, this is very possible! I used 0.6.0 beta 3 for this. Earlier versions may work, but I didn't try them.
For this to work, we need two outputs. The first script (or action) will output the raw PDF document. The second is just some "wrapper" HTML, to render the PDF as an inline object and handle closing the tab/window after the printing dialog is dismissed.
PDF output: (for this example, we'll assume this is at http://localhost/pdf.php)
require_once('path/to/dompdf/dompdf_config.inc.php');
header('Content-type: application/pdf');
$trigger = "
<script type='text/javascript'>
this.print();
this.closeDoc(true);
</script>
";
$dompdf = new DOMPDF();
$dompdf->load_html("<html><body><b>Hello, web printing!</b>{$trigger}</body></html>");
$dompdf->render();
echo $dompdf->output();
Wrapper html:
<html>
<body marginwidth="0" marginheight="0" style="background-color: rgb(38,38,38)">
<embed width="100%" height="100%" name="pdfObject" id="pdfObject" src="http://localhost/pdf.php" type="application/pdf">
</body>
<script>
window.onfocus=function(){ window.close();}
</script>
</html>
Opening the wrapper in a browser, you'll get an embedded copy of the PDF file, and the browser's print window should open automatically. Once you've finished with the print dialog, the tab/window should close. I've only tested this with Chrome, so your mileage may vary. IE does seem to have issues with the window.close in the wrapper.The way it works, is by embedding a little bit of javascript in the PDF itself - that's the $trigger variable in the first output. This causes the PDF viewer to open the print dialog. In the second output, putting a window.close in the window.onfocus event is what causes the window/tab to close out once the print dialog is dismissed. This is a little hackish, but seems to work OK in Chrome.
If the <script> tag in the wrapper is placed inside the body, the focus event does seem to fire prior to the print dialog closing. Having a script tag outside the body isn't exactly kosher, so, it's up to you and your user's needs whether or not it's worth it.
http://www.snazzware.com/blog/pdfprint/index.html
The full source for the example (including dompdf 0.6.0 beta) can be downloaded here:
http://www.snazzware.com/blog/pdfprint.tar.gz