LiquidOffice and Printing PDFs Using JavaScript

I recently helped a client work through a particularly tricky issue with a Liquid Office PDF form.  Normally a form is completed by an end user, possibly processed through a workflow and  finally released into an EDMS system with the help of a Connect Agent (for instance, ImageSource’s ILINX Connect for eForms product).  However in this case, the form is simply used for printing.

The form’s field elements consist of a drop down with a list of titles, text boxes, barcodes, and a big shiny print button.  When the user presses the print button, the form goes through each title defined in the drop down list, sets the few bar-code values based on the text boxes values, then calls the JavaScript print method.  The first print request displays the standard Print Dialog, and the rest print silently.  To print one batch of cover sheets, print is called ten times.  Using Form Designer’s preview functionality yields the expected results.  However, when the form is published to the Liquid Office server and the form launched from the Web Desktop, the first cover sheet is never printed.  Instead, two copies of the second cover page are.

So what is going wrong?

After judicious use of the JavaScript alert method, I was able to determine that there is a bug when scripting a print dialog while Adobe Reader is hosted inside of a browser window.  When this.print(true) is invoked, Adobe Reader continues executing any remaining scripts until the dialog is displayed or it reaches a statement that blocks script execution, like a message box or another print statement.  I tested multiple versions of Adobe (7,8 & 9) and several different browsers (IE, FireFox, Safari), but with each combination ran into the same bug.  A side effect of this bug, is that if you assign a variable to the return value of print, that value is always true, even if the print job was canceled!

To get around this bug, I took a page from the AJAX playbook.  Since I needed a way to block script execution until the print dialog was displayed, I used the JavaScript setTimeOut method.  If you don’t know what setTimeOut does, it invokes a function after a specified number of milliseconds.  You can find a more complete explanation here.  Once the print dialog has been displayed to the user, the Print Properties object must be set and passed into each successive print request.  Otherwise the settings selected by the user from the dialog will be ignored.  Here is the JavaScript code that makes the solution work.

setTimeOut sample code.

There are a number of tricks I tried that did not work.  For example, I wrote a function to pause execution by looping for a specified amount of time, but that didn’t work.  JavaScript also does not have a built in method to put the current thread to sleep.  A message box after the first print statement would have done the trick, but would have been confusing for the user.

Tyson Magney
Senior Developer
ImageSource, Inc.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s