Print to PDF with Ghostscript and RedMon on Windows





Print to PDF with Ghostscript and RedMon on Windows

figs/expert.gif figs/hack39.gif

Enjoy a convenient path to PDF, free of charge.

Any program that prints can also create PDFs. Adobe Acrobat sets the standard for PDF creation, but it can be too expensive. Ghostscript is a free PostScript interpreter that can also create PDFs, much like Acrobat's Distiller. Use RedMon to plug the power of Ghostscript into the convenience of a Windows printer, and you'll have "Print to PDF"!

For a prepackaged Ghostscript/RedMon installer, try PDFCreator (http://sourceforge.net/projects/pdfcreator/) or PrimoPDF (http://www.primopdf.com).


1 Download and Install Ghostscript

The Ghostscript home page is http://www.cs.wisc.edu/~ghost/. As of this writing, the current version of AFPL Ghostscript is 8.14. Download and run the corresponding installer, gs814w32.exe. Our configuration, which follows, assumes Ghostscript is installed in the default location, C:\gs\. If you ever need to reinstall Ghostscript, first uninstall it from the Add or Remove Programs dialog in the Windows Control Panel.

2 Download and Install RedMon

The RedMon home page is http://www.cs.wisc.edu/~ghost/redmon/. As of this writing, the current version of RedMon is 1.7. Download the corresponding zip file, redmon17.zip, into a new directory called redmon17. Unzip it and double-click setup.exe to install.

3 Download and Unpack Our Virtual Printer Kit

The PDF Hacks Virtual Printer Kit (VPK) has printer driver files, PPD files, and sample configuration files. Download it from http://www.pdfhacks.com/virtual_printer/. Unzip it into a convenient directory and note its contents. We'll call on these pieces as we need them. The README file might contain updates to these instructions.

You can download a PostScript driver installer for Windows from the Adobe web site. However, its license suggests that you can use it only in conjunction with other Adobe products, such as a PostScript printer. Review this license. If it suits you, download the latest installer (currently 1.0.6) from http://www.adobe.com/support/downloads/product.jsp?product=44&platform=Windows

You can use the ADIST5.PPD printer description from the Virtual Printer Kit during setup to create a GS Pdf Printer. Continue with Section 4.9.5.2.


The rest of this hack is divided by platform: Windows XP or 2000, and Windows 98.

4 Install and Configure a PDF Printer: Windows XP, 2000

You already installed Ghostscript, RedMon, and our Virtual Printer Kit, right? All that remains is to install and configure your PDF printer.

4.1 Install a Virtual PostScript printer

Any number of PostScript (PS) printers can use the same, core PS driver. To create a complete printer driver, we must combine this core with a printer's PPD file, which describes its capabilities in detail.

Windows XP comes with an up-to-date core PS driver. Our Virtual PostScript Printer driver is simply an INF file instructing Windows to combine this core with the (freely distributable) PPD from Adobe that describes the Acrobat 5 Distiller printer. The result is a printer that is almost ready to create a PDF.

  1. From the Printers and Faxes folder (Start Settings Printers and Faxes) click Add a Printer under Printer Tasks and the wizard will open. Click Next to begin.

  2. Select Local Printer, uncheck Automatically Detect, and click Next.

  3. Select Create a New Port and choose the Redirected Port from the drop-down list. Click Next. A dialog will open asking for the port name. Enter RPTPDF: and click OK. When you installed RedMon, it created this Redirected Port option.

  4. The wizard will present you with a list of printers and manufacturers, but we don't want these. Click Have Disk . . . and then Browse . . . . Navigate to the driver\WinXP\ directory in our Virtual Printer Kit, open ADIST5GS.INF, and then click OK. The wizard should now display only one printer, our Virtual PostScript Printer. Select this printer, as shown in Figure. Windows might complain that this printer driver is not signed. Click Next.

    Our Virtual PostScript Printer driver, which has the exact same printer profile as Acrobat's PDF printer
    figs/pdfh_0407.gif


  5. If the wizard remarks that a driver already is installed for this printer, select Keep Existing Driver and click Next.

  6. Name the printer GS Pdf Printer and select No default printer. Click Next.

  7. Select No to keep it from printing a test page and click Next.

  8. Click Finish to complete the installation. Windows might complain that our driver is not Windows Logo-certified. If so, confirm that you want to install our driver.

The GS Pdf Printer printer should now appear in your Printers and Faxes folder.

4.2 Configure the RedMon redirected port to print PDF

Now, wire your new printer to the Ghostscript program by configuring the redirected port you created, RPTPDF:.

  1. Right-click your new GS Pdf Printer printer and select Properties. Click the Ports tab.

  2. Make sure the "Enable bidirectional support" checkbox is unchecked.

  3. Select the Redirected Port you just created, RPTPDF:, and click Configure Port . . . .

  4. Set the port properties as shown in Figure, changing the paths to suit your setup. Be careful not to overlook the solitary hyphen at the end of the program arguments.

    RedMon port properties

    Field

    Value

    Redirect this port to the program:

    
    
    C:\gs\gs8.14
    
    \bin\gswin32c.exe

    Arguments for this program are:

    -sOutputFile="%1"@
    
    C:\gs
    
    \pdf_printer.cfg-f -

    Output:

    Prompt for Filename

    Run:

    Minimized


  5. Click the Log File button to configure the Log File Properties. Set Use Log File and enter a log filename, such as C:\gs\pdf_printer.log. Don't set Debug.

  6. Click OK to close the Log File Properties. Click OK to accept the new port settings. Click Close to accept the new printer settings, as shown in Figure, and close the GS Pdf Printer Properties dialog.

RedMon passing the PostScript created by your printer driver to Ghostscript
figs/pdfh_0408.gif


To complete the port configuration, you must create the file C:\gs\pdf_printer.cfg, referenced earlier. It is a text file of additional arguments passed to Ghostscript. An example is included with our Virtual Printer Kit. Change the paths to suit your Ghostscript and system setup.

-dSAFER

-dBATCH

-dNOPAUSE

-Ic:\gs\gs8.14\Resource

-Ic:\gs\fonts

-Ic:\gs\gs8.14\lib

-sFONTPATH=c:\WINDOWS\FONTS

-sDEVICE=pdfwrite

-r1200

-c save pop

Voilà! Now, let's test it. Jump down to Section 4.9.6, later in this hack.

5 Install and Configure a PDF Printer: Windows 98

You already installed Ghostscript, RedMon, and our Virtual Printer Kit, right? All that remains is to install and configure your PDF printer.

5.1 Install our Virtual PostScript printer

Any number of PostScript (PS) printers can use the same core PS driver. To create a complete printer driver, we must combine this core with a printer's PPD file, which describes its capabilities in detail.

The trouble is, we need an up-to-date PS core. Search your computer hard drive for ADOBEPS4.DRV to see if a recent PS core is installed. If you find it, you will be able to copy our required files, listed later in this section, from your hard drive. If you don't find it, you have a couple options.

One option is to use the Adobe PostScript driver installer, as described in the earlier note. However, its license severely restricts its use. The other option is to download a printer driver from some manufacturer, a driver that contains all the core files we need and employs a permissive license.

For this latter purpose, I like the printer driver download for the Phaser 550 (driver version 4.52) from http://www.Xerox.com. It is a self-extracting executable, win9xadb.exe, which provides all the necessary files in a neat directory. Check the Virtual Printer Kit README for other possible sources.

Whichever driver you use, these are the files you ultimately must collect. You should collect them all from a single source to ensure they're compatible:

ADOBEPS4.DRV
ADOBEPS4.HLP
ADFONTS.MFM
ICONLIB.DLL
PSMON.DLL

Copy these files into the Virtual Printer Kit directory driver\Win9x\. That directory already contains the final two ingredients: our Virtual PostScript Printer PPD file and the Windows INF file used by the Add Printer install wizard to tie everything together.

  1. From the Printers folder (Start Settings Printers) double-click the Add Printer icon. Click Next to begin.

  2. Select Local Printer and click Next.

  3. The wizard will present you with a list of printers and manufacturers, but we don't want these. Click Have Disk . . . and then Browse . . . . Navigate to the driver\Win9x\ directory in our Virtual Printer Kit and click Open and then OK. The wizard should now display only one printer, our Virtual PostScript Printer. Select this printer. Click Next.

  4. If the wizard remarks that a driver is installed for this printer, select Replace Existing Driver and click Next.

  5. Under Available Ports, select FILE: and click Next.

  6. Name the printer GS Pdf Printer and select No default printer. Click Next.

  7. The wizard should be finished. If a Version Conflict dialog opens to complain that you are overwriting newer files with older files, the safest thing to do is to keep the newer files.

    The GS Pdf Printer printer now should appear in your Printers and Faxes folder. Wire your new printer to the Ghostscript program by adding and configuring a redirected port.

5.2 Add and configure the RedMon redirected port

Let's wire your new GS Pdf Printer printer to the Ghostscript program with a RedMon redirected port.

  1. Right-click your new GS Pdf Printer printer and select Properties. Click the Details tab.

  2. Click Add Port . . . and a dialog opens. Select Other, select Redirected Port, and click OK. Name the port RPTPDF: and click OK.

  3. On the Details tab, click Spool Settings . . . . Select "Disable bi-directional support for this printer" and click OK.

  4. On the Details tab, click Port Settings . . . . Set the port properties as shown in Figure, changing the paths to suit your Ghostscript setup. Be careful not to overlook the solitary hyphen at the end of the program arguments.

    RedMon port properties

    Field

    Value

    Redirect this port to the program:

    
    
    C:\gs\gs8.14
    
    \bin\gswin32c.exe

    Arguments for this program are:

    -sOutputFile="%1"@"
    
    C:\gs
    
    \pdf_printer.cfg"-f -

    Output:

    Prompt for Filename

    Run:

    Minimized


  5. Click the Log File button to configure the Log File Properties. Set Use Log File and enter a log filename, such as C:\gs\pdf_printer.log. Don't set Debug.

  6. Click OK to close the Log File Properties. Click OK to accept the new port settings. Click OK to accept the new printer settings and close the GS Pdf Printer Properties dialog.

To complete the port configuration, you must create the file pdf_printer.cfg, referenced earlier. It is a text file of additional arguments passed to Ghostscript. An example is included with the Virtual Printer Kit. Change the paths to suit your Ghostscript and system setup.

-dSAFER

-dBATCH

-dNOPAUSE

-I"c:\gs\gs8.14\Resource"

-I"c:\gs\fonts"

-I"c:\gs\gs8.14\lib"

-sFONTPATH=c:\WINDOWS\FONTS

-sDEVICE=pdfwrite

-r1200

-c save pop

Voilà! Now, let's test it.

6 Test Your GS Pdf Printer

Open the GS Pdf Printer properties dialog, click the General tab, and click Print Test Page. After a pause, a dialog will open where you can enter the PDF's filename. Select a suitable filename and click OK. Note that it will overwrite a file without asking and that it does not automatically add the PDF extension to the filename.

When it is done, open the PDF in Reader. How does it look? The next few hacks discuss how to tune Ghostscript for your purposes.

If an error occurs or the PDF file isn't created, carefully double-check the printer configuration and consult the log file:

  • Is bi-directional printing disabled?

  • Does the redirected port setting match those given earlier? Mind the separate, trailing hyphen in the program arguments.

  • Is pdf_printer.cfg in the correct location? Do its contents match those given earlier?

  • Have all C:\gs\ paths been updated to reflect the location of your Ghostscript installation?


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows