HTML Applications: Printing PDFs from an Iframe

by David Halls



Example.pdf ---->          ----> Document

Introduction

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?

The block below shows the HTML code for a page in which is an iframe. The source, src, of the iframe is in this case an HTML document. Above this is a "Print" button which when clicked invokes the Javascript function, printdoc.

<html>
<head>
<script language="Javascript" type="text/javascript">
function printdoc() {
     pdfframe.focus(); 
     pdfframe.print();
	 }
</script>
</head>
<body>
   <center>
   
   <<button onclick = "printdoc">Print</button>
   <br />
   <br />
   <iframe name = "myframe" align = "middle" scrolling = "yes" frameborder = "1" width = "700" 
         src= "mydoc.html"></iframe>
   </center>
</body>
</html>

Would this work with a PDF document as the src of the iframe?

If you do a Google search, you might believe it would (see Ref.1). I have tried the many variations shown on the web that are variations of the above javascript function and I could not get any of them to work in Internet Explorer 7 with a PDF document. Nor should they! After all, if you display a PDF document in Internet Explorer, it is Adobe Reader that is controlling the display and the printing. So, if we want to get a "Print" button to work, we have to programme Adobe Reader to do the work.

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:-

  1. Use of BioPDF's acrowrap.exe to close Reader after printing.
  2. Forgetting Adobe Reader and using Foxit Reader instead.

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.

Conclusions

  1. If you are displaying a PDF in an iframe in Internet Explorer, it seems extremely unlikely that you can use a variant of the "window.print" method to print it out.
  2. Command line operations can be used to print out a PDF with Adobe Reader, but unfortunately a minimised window of Reader is left behind and this must be closed before another PDF is printed.
  3. BioPDF have made available a wrapper (acrowrap.exe) for Reader which solves the above problem. This gives the best option for printing with Adobe Reader.
  4. Command line printing with Foxit Reader does not have the same problem as the Adobe product and produces a fast operation.

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.

References

  1. How to print pdf in iframe using javascript in ie6 - Stack Overflow
  2. How do I use the Windows command line with Acrobat and Adobe Reader? - Adobe
  3. BioPDF - close Adobe Reader (acrowrap.exe)
  4. Foxit Reader
  5. Foxit Reader Manual

Article Date:19th March 2013.