HTML Applications: Printing PDFs from an Iframe
by David Halls
In a previous article, it was shown how to display a PDF in an iframe.
What we would like is a button called "Print" which, when clicked starts the printer printing the displayed document directly. However, this is not easy. A while ago, I wrote an article on "Printing options in HTML programmes", which described various possibilities for printing HTML documents from a programme. One of these is the window.print applied to an iframe.
Will the window.print method work also with a PDF document?
Would this work with a PDF document as the src of the iframe?
Programming Acrobat Reader to print a PDF
Adobe has command line parameters for Reader that enable printing (ref. 2). The information comes with a caution "These commands are unsupported, but have worked for some developers."
The syntax is:-
AcroRd32.exe /t / h filepath Where: /t Start Adobe Reader and print a file while suppressing the Print dialog box. /h Start Acrobat or Adobe Reader in a minimized window. filepath The full path of the file to be printed.
So for a file "Example.pdf" in our folder "C:\html progs\pdfprint\", the code would be as below. The switch "/h" has been omitted, because, in practice, it was found to make no difference. With the "/t" switch, Acrobat Reader opens minimised anyway. The command line (cmdline) is created and then run using the Wscript.Shell Object
In the head section of the HTML page: <script language="VBScript" type="text/vbscript"> Dim FileName FileName = "Example.pdf" sub printpdf set oWsh = CreateObject ("Wscript.Shell") oWsh.CurrentDirectory = "C:\html progs\pdfprint\" cmdline= "AcroRd32.exe /t " & chr(34) & FileName & chr(34) oWsh.run cmdline set oWsh = nothing end sub </script> In the body of the HTML page: <button onclick = "printpdf">Print</button>
Although this works of sorts, it is generally disappointing. With Reader 10, I could not get reproducible behaviour. On my computer, Adobe Reader has now updated to version 11, which is reproducible, but just as frustrating. It opens a minimised window of Reader but does not close it. If you want to repeat the print procedure, you have to first manually close Reader.
There is no way I would recommend this procedure. Fortunately, I have found two alternatives which I can recommend:-
These two options are now discussed.
Use of BioPDF's acrowrap.exe
The clever people at BioPDF have come up with a solution to the problem of Adobe Reader not shutting down. You can download a programme called "acrowrap.exe" from their website, which acts as a wrapper around Reader making it shut down after printing. The programme is free for personal use, but if you are intending using it commercially, you should check the licensing arrangements. It is normally installed in the "Program Files" directory.
The syntax to use it as a command line operation is given as:-
acrowrap.exe /t "pdf_file_name" ["printer_name"] ["printer_driver"] ["printer_port"]
In the example that follows, the use of the default printer, printer driver and printer port will be assumed. Although above we used "AcroRd32.exe" without specifying the path, I found it did not work with "acrowrap.exe" and had to put in the full path.
In the head section of the HTML page: <script language="VBScript" type="text/vbscript"> Dim FileName, oWsh, acrowrap, cmdline acrowrap = "C:\Program Files\bioPDF\Acrobat Wrapper\acrowrap.exe" sub printpdf FileName = "Example.pdf" set oWsh = CreateObject ("Wscript.Shell") oWsh.CurrentDirectory = "C:\html progs\pdfprint\" cmdline = chr(34) & acrowrap & chr(34) & " /t " & chr(34) & FileName & chr(34) oWsh.run cmdline set oWsh = nothing end sub </script> In the body of the HTML page: <button onclick = "printpdf">Print</button>
The variable acrowrap gives the full path to acrowrap.exe. The working directory containing the pdf files is set.
This script works fine. Adobe Reader opens briefly to display the file and then closes to open the print preview mode if used or, if not, the printer progress window as the printer whirrs into action.
Using Foxit Reader
Foxit Reader is a popular lightweight and fast alternative to Adobe Reader and it is worth exploring whether we could get this to behave as we would like.
The installation programme can be downloaded from the Foxit website. When running the installation programme, be wary about the additional software and changes it will try to give you - uncheck them, unless of course you want them.
You will find some information in the programme under Help and then "Command Line Help". The switch that worked for me was " /p". The code is shown below.
In the head section of the HTML page: <script language="VBScript" type="text/vbscript"> Dim FileName, oWsh, foxit, cmdline sub printpdf foxit= "C:\Program Files\Foxit Software\Foxit Reader\Foxit Reader.exe" FileName = "Example.pdf" set oWsh = CreateObject ("Wscript.Shell") oWsh.CurrentDirectory = "C:\html progs\pdfprint\" cmdline = chr(34) & foxit & chr(34) & " /p " & chr(34) & FileName & chr(34) oWsh.run cmdline end sub </script> In the body of the HTML page: <button onclick = "printpdf">Print</button>
The result is a very slick print routine. You do not get a glimpse of the document before printing. It goes directly to either the print preview or print progress window depending on your settings.
On the face of it, Foxit seems the best option but the aim of this project is to produce a document from data, convert it to PDF, display the PDF on an HTML page and then have a "Print" button to allow direct printing. Some of these options have been explored in other articles;
Whether Adobe or Foxit Reader proves to be the best for the overall project remains to be seen.
Article Date:19th March 2013.