Troubleshooters.Com and T.C Linux Library Present

Ghostscript:
Taking PDF Creation to the Next Level

Copyright (C) 2007 by Steve Litt, All rights reserved. Material provided as-is, use at your own risk. 



Contents

    
Other PDF Related Pages

Disclaimer

This page is an introduction to ghostscript -- not an authoritative text. This page may have errors -- in fact it probably does. DO NOT trust what you see on this page without verifying it for yourself. I am not responsible for any damage or injury caused by your use of this document, or caused by errors and/or omissions in this document. If that's not acceptable to you, you may not use this document. By using this document you are accepting this disclaimer.

Introduction

There's a program called ps2pdf that's very easy to use:
ps2pdf mydocument.ps
Easy, clean, fast. But sometimes not enough.

Perhaps you need to make a landscape PDF from a postscript file lacking the proper orientation cues. Sounds easy, but it can be ruinously difficult, requiring ghostscript (gs). Or perhaps you need to create a multipage PDF from several single page source files. Once again, gv to the rescue.

Viewing Device Names

Ghostscript uses various devices to accomplish its magic. Some devices are printer drivers, while others print to specialized files (there's a PDF device). Here's how you see all the device names, in alphabetical order:
echo 'devicenames == ' | gs -q | tr " " "\n" |  sort
The preceding pipes the command 'devicenames ==' into the ghostview interpreter with the -q (quiet) switch, assuring that only device names are output. The interpreter's output is all devices on a single line, which of course isn't what you want, so you split it into multiple lines with the tr command, and then sort the lines with the sort command.

Once you've viewed the list of devices, you're in a much better position to know what to do. In my case, I use the device named /pdfwrite to write PDFs (whoda thunkit?).

Viewing Paper Sizes

You'll need paper sizes for some ghostscript (gv) commands. The following script (let's call it papersizes.sh) reads a .ppd file and outputs a list of all paper size names and their sizes. The .ppd file will probably be on your system, so use the following command to locate it:
locate -r \.ppd$ | grep -i pdf
In my case, the preceding command returned a single file, /usr/share/ghostscript/8.15/lib/ghostpdf.ppd. You will use this filename to set a variable in the script, which follows:

#!/bin/bash
pdfppd=/usr/share/ghostscript/8.15/lib/ghostpdf.ppd

grep ^*PageSize $pdfppd | cut -d " " -f 2-10 \
| sed -e "s/:[^\[]*/ : /" \
| sed -e "s/\].*/]/"

Obviously, you'll change your $pdfppd variable to reflect the file on your system. If you can't find one, you can download this file., or if that file no longer exists when you read this paper, perform a Google search on the terms ghostscript, A4 A3 Letter and Legal without quotes. That should bring you a suitable .ppd file, which you can download.

Running the preceding script lists all paper size names followed by a space and colon, followed by a width/length pair of dimensions in points (there are 72 Postscript points per inch). The two dimensions are enclosed in square brackets, separated by a space, which is how you use them in a ghostscript command. Remember, if you want to specify landscape, you switch the two dimensions.

Making a Simple PDF from a Portrait Postscript File

If you have a file called myfile.ps, here's how you convert it into a PDF called myfile.pdf:
gs -sDEVICE=pdfwrite -sOutputFile=myfile.pdf -dBATCH -dNOPAUSE myfile.ps
This is a pretty complex command, so here's the explanation:

gs
    The ghostscript program
-sDEVICE=pdfwrite

Write a PDF
-sOutputFile=myfile.pdf

Name the output file myfile.pdf
-dBATCH

Exit the interpreter upon processing of the last file. Otherwise you'd end up in the Ghostscript environment.
-dNOPAUSE

Do not wait for user input, but instead just execute the commands
myfile.ps

The postscript file to convert

Making a Landscape PDF

The difficulty of making a landscape PDF file depends on the postscript file from which it comes. Correctly made landscape postscript files can be converted with the ps2pdf program. However, if the postscript file doesn't contain the necessary orientation cues, even though the postscript file is viewable and printable as landscape, the resulting PDF looks like a landscape drawing on a portrait paper, with the rightmost area of the image cut off. When this happens, you have four choices:
  1. Find a different way to convert the source to postscript
  2. Convert the source directly to PDF
  3. Hand tweak the PDF with an editor or other tool (pdftk?)
  4. Use ghostscript
Obviously the first two alternatives are the best and easiest, but sometimes they're not available. As far as the third, good luck with that, because after an hour of trying I gave up trying to correct the postscript file with Vim.

If you cannot directly convert source to PDF, and your postscript or encapsulated postscript file won't create a landscape PDF, then you'll need to use ghostscript to twist the paper without twisting the image. There are two ways to strongarm an unwilling .ps file into a landscape PDF:
  1. Leave the paper portrait and twist the image. Therefore, when you look at it on the screen, the image is sideways, but printing it portrait will do exactly what you want.
  2. Make the paper landscape and leave the image alone. This makes the on-screen image exactly what you want, and is the best for projector presentations, but from what I've read it can have some unpleasant surprises when printed.
Let's look at how you do each of these alternatives:

Twist the Image

Once again, this is great for printing but you'll need to turn your head 90 degrees to correctly view it on the screen. Here's the command:
gs -dBATCH -dNOPAUSE -sOutputFile=myfile.pdf -sDEVICE=pdfwrite -dAutoRotatePages=/None \
-c "<< /PageSize [612 792] /Orientation 3 >> setpagedevice" 90 rotate 0 -612 translate -f myfile.eps

Make the Paper Landscape

The preceding method was wonderful from a printing perspective, but you'll need to turn your head 90 degrees to correctly view it on the screen. For a PDF that looks great on the screen, especially for presentations, you leave the image orientation alone and make the paper landscape. Here's how it'd done:
gs -dBATCH -dNOPAUSE -sOutputFile=myfile.pdf -sDEVICE=pdfwrite  \
-c "<< /PageSize [792 612] >> setpagedevice" -f myfile.eps

Making a Multipage PDF From Multiple .ps or .pdf Files

Ghostview can't work miracles. All the pages must be the same orientation, and must match in some other ways. In an ideal world, all source pages would be made in the same way with the same application. For instance, let's say you created four separate portrait Gimp drawings and exported each to an .eps file. Here's how you might merge them into one:
gs -sDEVICE=pdfwrite -sOutputFile=myfile.pdf -f myfile1.eps myfile2.eps myfile3.eps myfile4.eps
If all the drawings were landscape in source, but one or more have the problem of converting to portrait in ps2pdf, then you need to strongarm the paper orientation while leaving the image orientation alone:
gs -sDEVICE=pdfwrite -sOutputFile=myfile.pdf -c "<< /PageSize [792 612] >> setpagedevice" -f myfile1.eps myfile2.eps myfile3.eps myfile4.eps
If you have four "similar enough" PDF files but don't have the source to them, you can combine them by using PDF files as building blocks:
gs -sDEVICE=pdfwrite -sOutputFile=myfile.pdf -f myfile1.pdf myfile2.pdf myfile3.pdf myfile4.pdf

Other Uses of Ghostscript

Ghostscript has many devices besides pdfwrite. There's a device to write .png files, another to write .pnm files, several for .tiff files, one for .xcf files, a few for .bmp files,  .cif files, and cups printing. There even appear to be fax related outputs. In addition to this, you can create printer files for many, though certainly not all, popular printers.

Summary

For simplicity, it's best to either export from source directly to .pdf, or if that's not possible or practical, from source to .ps or .eps and then use ps2pdf. But when the going get tough, the tough use Ghostscript to do those unusual tasks like creating landscape .pdfs from unruly postscript files, and creating multipage .pdfs from source apps not supporting multipage documents.






Back to Troubleshooters.Com * Back to Linux Library