HTML Applications: Automated conversion of documents to PDFs
by David Halls
In this project, we are aiming at having a button called something like "Create PDF" which, when clicked, will create a PDF of a specified document.
Creating PDFs with Bullzip
There are a number of free PDF writers available, mostly based around the Ghostscript engine to convert Postscript files to PDF. They act as an alternative printer: you set your document to print and then set the PDF Printer as the printer. The document is created and sent to the Folder you selected in the dialog box that intervened.
I have used CutePDF in the past as a PDF Printer but here selected Bullzip based on its good reputation and its ability to be programmed. I found it able to produce a PDF from a large text document with amazing speed. I am pleased to report that you can download Bullzip from the Bullzip website without worry, unlike other free offerings (e.g. Adobe Reader, Foxit Reader) that try to foist unnecessary toolbars or software on you. It is free for private or commercial use for up to 10 users. There is a supported licensed version called BioPDF but the Bullzip version is much the same with only minor limitations.
Bullzip and BioPDF Printers offer command line, batch file and COM automation possibilities. The command line and batch file approaches use a programme called "Printto.exe" which can be downloaded with the examples on this BioPDF page. In the next section, we explore what you can do with "Printto.exe", as this seemed the simplest approach. I copied this file into the working folder that I was going to use for the conversions.
Printing with "Printto.exe"
If you right-click on a file in Windows Explorer, you get a list of options, one of which is "Print", which will allow printing through the default programme for that file type. The executable "Printto.exe" levers the same kind of functionality using Dynamic Data Exchange (DDE). The syntax given by BioPDF is as below
printto "file name" ["printer name"]
I have to confess that I had problems with this as I thought the square brackets should be included in the command line. I think it is intended that the "printer name" is optional; in its absence, the file is printed to the default printer.
Now we will look at some examples and record what happens:-
|printto "example.doc"||Briefly opens Microsoft Word with document which then closes and document prints on default printer or goes to Printer Preview (depending on settings).|
|printto "example.html"||Goes straight to the Print Dialog Box of Internet Explorer (Files containing script can cause problems)|
|printto "example.pdf"||Document prints on default printer or goes to Printer Preview (depending on settings). Leaves behind a minimised window of Adobe Reader.||printto "example.doc" "Bullzip PDF Printer"||Briefly opens Microsoft Word with document which then closes and Bullzip Dialog Box opens. You select the folder and name of the destination file and then click on "Save". The pdf is created and displayed in Adobe Reader. The display can be switched off in the Dialog Box.||printto "example.html" "Bullzip PDF Printer"||Goes straight to the Print Dialog Box of Internet Explorer with Bullzip PDF Printer highlighted as printer. (Files containing script can cause problems). On clicking on "Print", the Bullzip Dialog Box opens. You select the folder and name of the destination file and then click on "Save". The pdf is created and displayed in Adobe Reader. The display can be switched off in the Dialog Box.|
The examples shown are the reactions on my computer. If you are using a different default web browser, Office application or PDF reader, then the results could be different.
Implementing with VBScript
As we have seen above, the simplest to implement is the direct printing of a Word document as it goes directly to the default printer without further intervention. Here, we create a WScript.Shell object so that we can use its Run method to run the command line code. We need to set the directory to the folder that contains the printto.exe file and the Word document.
<html> <head> <script language="VBScript" type="text/vbscript"> Dim FileName sub printfile FileName = "Example.doc" Dim oWsh set oWsh = CreateObject("Wscript.Shell") oWsh.CurrentDirectory = "C:\html progs\pdfprint\" cmdline= "printto.exe " & FileName oWsh.run cmdline set oWsh = nothing end sub </script> </head> <body> <center> <button onclick = "printfile">Print</button> </center> </body> </html>
However, this article is supposed to be about creating pdfs. To do this we set the printer to "Bullzip PDF Printer".
<html> <head> <script language="VBScript" type="text/vbscript"> Dim FileName Dim PrinterName sub makepdf FileName = "Example.doc" PrinterName = "Bullzip PDF Printer" Dim oWsh set oWsh = CreateObject("Wscript.Shell") oWsh.CurrentDirectory = "C:\html progs\pdfprint\" cmdline= "printto.exe " & chr(34) & FileName & chr(34) & " " & chr(34) & PrinterName & chr(34) oWsh.run cmdline set oWsh = nothing end sub </script> </head> <body> <center> <button onclick = "makepdf">Create PDF</button> </center> </body> </html>
It is necessary to put inverted commas round the PrinterName and, for general use, around the FileName. These are added as chr(34).
This will produce a PDF but we need to interact with the Bullzip interface to specify the destination and whether the PDF document is to be displayed.
Creating a "Runonce.ini" file with VBScript
According to the BioPDF Guide, we need to create a file "Runonce.ini" which contains the destination file and other parameters. Once Bullzip PDF Printer has used it, it is automatically deleted.The guide gives an example as a batch file which I have converted into VBScript.
The problem is that this file needs to go into the Application Data folder. There are differences in how Windows XP reaches this. Later versions of Windows (Vista, 7 and presumably 8) do it differently. The complete paths in the two cases take the form shown below.
|Windows XP||C:\Documents and Settings\[User]\Local Settings\Application Data\PDF Writer\Bullzip PDF Printer\runonce.ini|
|C:\Users\[User]\App Data\Local\PDF Writer\Bullzip PDF Printer\runonce.ini|
We can make use of environment variables to find most of the path on the host computer. %LOCALAPPDATA% will give the path (up to "\PDF Printer" for Windows Vista and Windows 7, but this variable will not be recognised by Windows XP. For this operating system, we can use %USERPROFILE% and then add in the rest of the path.
<script language="VBScript" type="text/vbscript"> Dim FileName Dim PrinterName Dim Runoncefile Dim filetxt Const ForReading = 1, ForWriting = 2, ForAppending = 8 sub makepdf FileName = "Example.doc" PrinterName = "Bullzip PDF Printer" set oWsh = CreateObject("Wscript.Shell") set oFso = CreateObject("Scripting.FileSystemObject") 'Distinguish Windows XP from Vista and Windows 7 APPDATA = oWsh.ExpandEnvironmentStrings("%LOCALAPPDATA%") If APPDATA = "" then 'Windows XP. No %LocalAppData% found. APPDATA = oWsh.ExpandEnvironmentStrings("%USERPROFILE%") Runoncefile = APPDATA & "\Local Settings\Application Data\PDF Writer\" & PrinterName & "\" & "runonce.ini" Else 'Vista and Windows7. %LocalAppData% found Runoncefile = APPDATA & "\PDF Writer\" & PrinterName & "\" & "runonce.ini" End If Set filetxt = oFso.OpenTextFile(Runoncefile, ForWriting, True) With filetxt .WriteLine("[PDF Printer]") .WriteLine("output=C:\html progs\pdfprint\Example.pdf") .WriteLine("showsettings=never") .WriteLine("showpdf=no") .Close end with oWsh.CurrentDirectory = "C:\html progs\pdfprint\" cmdline= "printto.exe " & FileName & " " & chr(34) & PrinterName & chr(34) oWsh.run cmdline set oWsh = nothing msgbox "File transferred" end sub </script> <center> <button onclick = "makepdf">Create PDF</button> </center>
The script uses the File System Object to open a file "Runonce.ini" in the appropriate location and then writes in the parameters. These define the name and path of the pdf to be created and arrange for a direct conversion without showing the settings or the final pdf. If you want the pdf to be displayed, set "showpdf=yes"; the product will be displayed in your default pdf viewer.
This article shows how you can use Bullzip PDF Writer in an HTML application to automate the production of a PDF from a specified Word document through clicking on a command button. The script can be used as the basis of further development, e.g. batch conversion of documents.
Article Date:12th March 2013