Silent PDF Printing from a PHP-based Web Application

A client asked me if it was possible to have the PDF’s generated by the PHP-based internal web application I’ve been working on to print to the user’s default printer. The user’s use both networked and directly attached printers so implementing something that used *NIX’s lpr command to send the PDF to a network printer wasn’t possible.

This led to figuring out how to have the PDF print without user interaction (e.g. having the Adobe Acrobat Reader print dialog appear and have the user click the print button).  Since Acrobat Reader supports Javascript I needed to have PHP take web server generated PDF files and add a simple javascript statements to them to have them automatically print to the user’s default printer when opened.

PDFlib is a commercial product that can be used to work with PDF’s on the web server, however the FPDF library is a free PHP class that does the same thing.  I had found a forum post where someone had extended the FPDF class to easily add Javascript to the PDF.  The code modifying the FPDF class is as follows:

// Make the FPDF class available.

// Extend it with some new methods
class paperpdf extends FPDF {
    var $javascript;
    var $n_js;

    function IncludeJS($script) {

    function _putjavascript() {
        $this->_out('/Names [(EmbeddedJS) '.($this->n+1).' 0 R]');
        $this->_out('/S /JavaScript');
        $this->_out('/JS '.$this->_textstring($this->javascript));

    function _putresources() {
        if (!empty($this->javascript)) {

    function _putcatalog() {
        if (!empty($this->javascript)) {
            $this->_out('/Names <n_js).' 0 R>>');

To use this code to generate a new PDF and have it print when opened by Adobe Acrobat Reader.

// Create a new FPDF instance and set parameters
$paperpdf = new paperpdf;
$paperpdf->Write(5,'I will print when opened.');

// Add the javascript with parameters to the Adobe API print method to do silent printing
$paperpdf->IncludeJS("this.print({bUI: false, bSilent: true, bShrinkToFit: true});");

// Have FPDF create and stream the auto-printing PDF to the browser

The limitation of the approach above is that without the user having a specific javascript file added to the Adobe scripts directory on their computer they will shown a warning that says This document is trying to print. Do you want to allow this? To get around they need to add the following SDKSilentPrint.js file placed on their local computer in the Adobe Reader Javascripts directory (e.g. c:\Program Files\Adobe\Acrobat 8\Reader\Javascripts).

The  line J in the snippet above would have been changed to:

// Silent print the document provided user has script locally installed

And the Adobe Acrobat Javascript API provided code is:

 Copyright 1994-2008 Adobe Systems Incorporated
 All rights reserved.
 NOTICE: Adobe permits you to use, modify, and distribute this file
 in accordance with the terms of the Adobe license agreement
 accompanying it. If you have received this file from a source other
 than Adobe, then your use, modification, or distribution of it
 requires the prior written permission of Adobe.
 - Folder level Javascript code created by Adobe Acrobat SDK.
  * The sample will add "Silent Print" under Acrobat File menu.
  * Click it to print the front document to the default printer without user interface.
  * It works for Reader, too.
  * See Acrobat JavaScript reference and guide SDK documents for further information.
if ( typeof sdkMenuItem == "undefined")
	var sdkMenuItem = false;
if (!sdkMenuItem) {
	sdkMenuItem = true;
	app.addSubMenu( {
		cUser: "Acrobat SDK JavaScript",
		cParent: "Tools",
		nPos: 0
 * Add a menu item
 * you can execute this menu item from other programs such as IAC VB / VC code.
app.addMenuItem( {
	cName: "ACROSDK:SilentPrint",
	cUser: "Silent Print",
	cParent: "ACROSDK:JSSubMenu",
   cEnable: "event.rc = ( != null);",
   cExec: "JSSilentPrint("
* trustedPrint function: Exercise the print function in the privileged context
* @param doc The Doc object of the target document
* @param pparam The PrintParams object containing print settings
trustedPrint = app.trustedFunction(
function(doc, pparams) {
* Main function: Print silently
* @param theDoc The event target of executing the menu item of this sample.
function JSSilentPrint(theDoc)
// get the printParams object of the default printer
var pp = theDoc.getPrintParams();
// print all pages.
// You can print certain pages using code:
// pp.firstPage = theDoc.pageNum1;
// pp.lastPage = theDoc.pageNum2;
// silent print,
// you can also try .automatic instead of .silent
pp.interactive = pp.constants.interactionLevel.silent;
pp.pageHandling = pp.constants.handling.none;
// set flag value which may include many options.
// here we enable automatic paper tray selection
var fv = pp.constants.flagValues;
pp.flags |= fv.setPageSize;
pp.flags |= (fv.suppressCenter | fv.suppressRotate);
// Print to the default printer without invoking the print dialog
trustedPrint(theDoc, pp);

In order to hide the printing process more from the user the PDF can be sent to the user’s browser in a hidden iframe. As an example, the iframe src property would be the URL that contained the code in the second code snippet above and the iframe‘s style attribute would be style="display: none;".

My effort to understand how to do this was helped very much by a post of SanJBee’s that explains silent printing in a Java web application using iText.

Tags: , , ,

No comments yet.

Leave a Reply