Ghostscript PDF writer tips

ps2pdf
Distiller parameters
Target audience
Page Size
Landscape
CropBox
Document Properties
Initial view
Bookmarks
Link
Image compression
Document Security
References
Example Files

ps2pdf

ps2pdf is a shell script (Unix) or batch file (Windows) that invokes ghostscript to convert PostScript to PDF. A simple conversion is

    ps2pdf infile.ps out.pdf

The default at the moment is PDF 1.3 output (compatible with Acrobat 4 and later). If you want PDF 1.4 output (compatible Acrobat 5 and later) then use

    ps2pdf14 infile.ps out.pdf

To optimize output for the screen

    ps2pdf14 -dPDFSETTINGS=/screen infile.ps out.pdf

ps2pdf can handle simple conversions. If you need to set some particular parameters, you may need to create your own ghostscript command:

    gs -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite
      -sOutputFile=out.pdf -dCompatibilityLevel=1.3
      -dPDFSETTINGS=/screen -c .setpdfwrite -f infile.ps

On Windows you will need to add two directories to your PATH before running ps2pdf.bat. Subsitute the appropriate directories into the command below.

    path %path%;c:\gs\gs8.14\bin;c:\gs\gs8.14\lib

Distiller parameters

There are two ways to set distiller parameters. The first method is from the command line

    -dAutoRotatePages=/PageByPage

On Windows, if the command line is passed to a batch file (e.g. ps2pdf.bat) then you will need to replace the = with #. This is a problem with the Windows command interpreter, not ghostscript.

    -dAutoRotatePages#/PageByPage

The second method is from PostScript code

    << /AutoRotatePages /PageByPage >> setdistillerparams

Of course you can combine the two, which is required if the parameter value can't be expressed as a simple name, string or number.

    -c "<< /AutoRotatePages /PageByPage >> setdistillerparams" -f

You can also put a large number of arguments in a file and then add it to the command line with "gs @argfile infile.ps", or put it in a PostScript file and load it prior to the input file with "gs settings.ps infile.ps".

There is some documentation of distiller parameters in the ghostscript doc/Ps2pdf.htm. More detailed documentation is in the Acrobat Distiller Parameters.

GSview provides a crude graphical user interface for setting distiller parameters. Select "File | Convert", the pdfwrite device, then "Properties".

Target audience

The distiller parameters can be set to one of five predefined settings:

    -dPDFSETTINGS=/screen
    -dPDFSETTINGS=/ebook
    -dPDFSETTINGS=/printer
    -dPDFSETTINGS=/prepress
    -dPDFSETTINGS=/default

The /screen setting will produce the smallest files, but images will be low resolution. The other settings give progressively larger and higher quality output. doc/Ps2pdf.htm for details of these settings.

How to set the page size.

Use -sPAPERSIZE=a4 or the PostScript setpagedevice operator

    << /PageSize [595 842] >> setpagedevice

How to create landscape pages.

The simple method (but usually undesirable) is to set the page size to landscape format (wider than high).

     << /PageSize [842 595] >> setpagedevice

This will display correctly, but may have problems with printing.

The best method is to use portrait format, but to set the Orientation to landscape. This will cause pdfwrite to write the PDF /Rotate key, which causes viewers to rotate the page, but it will be printed unrotated.

    << /PageSize [595 842] /Orientation 3 >> setpagedevice
    90 rotate 0 -595 translate

There is a distiller setting for controlling automatic detection of orientation. The default is -dAutoRotatePages=/PageByPage. This can be set to /None if you want to manually determine the orientation.

How to make the viewer crop a page

A PDF file has several page sizes. The first is the MediaBox, which is set by the PostScript PageSize. Another interesting one is CropBox, which affects how much of the page is shown in a viewer. This can be set for all pages using

    [/CropBox [87 123 508 718] /PAGES pdfmark

or for an individual page using

    [/CropBox [92 194 184 274] /PAGE pdfmark

How to set the document properties

The document information is set using the pdfmark operator

  % Document information
  [/CreationDate (D:20020628) 
   /Creator (Hand crafted)
   % /Title (demopdfm.ps) % note that GS can get title from DSC %%Title
   /Subject (Example of CropBox, DOCINFO and links)
   /Keywords (pdfmark example Ghostscript CropBox DOCINFO Link Bookmark)
   /Author (Ghostgum Software Pty Ltd) 
  /DOCINFO pdfmark

See the pdfmark reference manual for more details.

How to set the initial view

  % Initial view on opening the document
  [/View [/XYZ null null 1]  % unspecified x and y offset, 100% zoom
   /Page 1
   /PageMode /UseOutlines % /UseNone /UserOutlines /UseThumbs /FullScreen
  /DOCVIEW pdfmark 

See the pdfmark reference manual for more details.

How to create a bookmark.

  % Bookmarks
  [/Title (Page 1 at 100%) /Page 1 /View [/XYZ null null 1] /Count 1 
  /OUT pdfmark
    % previous bookmark has one child which follows
    [/Title (Page 1 Fit Horizontal) /Page 1 /View [/FitH] 
    /OUT pdfmark
  [/Title (Page 2 at 200%) /Page 2 /View [/XYZ 100 200 2] 
  /OUT pdfmark
  [/Title (Page 3 is smaller) /Page 3 /View [/XYZ null null 1] 
  /OUT pdfmark

See the pdfmark reference manual for more details.

How to create a link (annotation).

You need to know the destination page number and the rectangle to be the active area of the link.

  % links to the 3 pages
  /Helvetica findfont 36 scalefont setfont
  100 300 moveto (1) show
  [/Rect [100 295 124 330] /Page 1 /Border [0 0 0] /Subtype /Link /ANN pdfmark
  150 300 moveto (2) show
  [/Rect [150 295 174 330] /Page 2 /Subtype /Link /ANN pdfmark
  200 300 moveto (3) show
  [/Rect [200 295 224 330] /Page 3 /Color [0 1 0] /Subtype /Link /ANN pdfmark
  showpage

See the pdfmark reference manual for more details.

Image compression.

The ghostscript defaults -dAutoFilterColorImages=true and -dAutoFilterGrayImage=true cause ghostscript to automatically detect whether JPEG or Flate compression is most suitable for each image. JPEG is good for photo images. Flate is good for line drawings, cartoons and computer screen shots.

The compression can be forced to JPEG with

    -dAutoFilterColorImages=false -dColorImageFilter=/DCTEncode

Other filters are /FlateEncode (zlib/gzip/pkzip) and /CCITTFaxEncode (ITU-T group 3 fax suitable for monochrome images).

To get smaller file sizes, enable image downsampling.

    -dDownsampleColorImages=true -dColorImageDownsampleType=/Average 
    -dColorImageDownsampleThreshold=1.5 -dColorImageResolution=72

This says that if the image resolution is greater than 72*1.5=108dpi, it should be resampled to 72dpi by averaging the pixels. There are similar settings for Gray and Mono images.

Using -dPDFSETTINGS=/screen will set color and gray image downsampling to 72dpi, -dPDFSETTINGS=/ebook will downsample to 150dpi, and -dPDFSETTINGS=/printer will downsample to 300dpi.

PDF encryption.

There are two revisions of the security handler.

Revision 2

Revision 2 does not work with GS 8.14, but is as follows

    ps2pdf13 -sOwnerPassword#owner -sUserPassword#user -dEncryptionR#2 -dKeyLength#40 -dPermissions#-64 in.ps out.pdf

Document Security can be set with the Permissions flag. For EncryptionR=2, subtract these values from -4 to disable an access.

4 = Print document
8 = Modify contents of document
16 = Copy text and graphics from document
32 = Add or modify text annotations

To allow printing and copying, but disable modifying the content and annotations, the value is -4-8-32 so use -dPermissions=-44. To enable all, use -dPermissions=-4. To disable all, use -dPermissions=-64.

Revision 3

Revision 3 does work with GS 8.14. You need to use PDF 1.4 output

    ps2pdf14 -sOwnerPassword#owner -sUserPassword#user -dEncryptionR#3 -dKeyLength#128 -dPermissions#-3904 in.ps out.pdf

Read the Adobe PDF Reference, Third Edition, table 3.15 for full details of the user access permission values.

4 = Print document (possibly not at the highest quality level)i.
8 = Modify contents of document, except as controlled by 32, 256 and 1024.
16 = Copy text and graphics from document other than that controlled by 512
32 = Add or modify text annotations, fill in interactive form fields, and if 256 is set, create or modify interactive form fields
256 = Fill in existing interacive form fields, even if 32 is clear
512 = Extract text and graphics (in support of accessibility to disabled users or for other purposes).
1024 = Assemble the document (insert, rotate, or delete pages and create bookmarks or thumbnail images), even when 16 is clear
2048 = Add or modify text annotations
To enable all, use -dPermissions=-4. To disable everything apart from viewing, combine the following -4 (base) -4 (print) -8 (modify) -16 (copy) -32 (annotate) -256 (interactive fields) -512 (copy for disability access) -1024 (assemble) -2048 (high quality print), so -dPermisions=-3904

References

Adobe PDF specifications includes the Adobe PDF Reference.

Acrobat SDK documentation. Previously this could be downloaded directly. Now you need to be a member of the "ASN Developer Program". The documents of interest are the "Acrobat Distiller Parameters" and the "pdfmark Reference Manual"

Example files

An example of setting the DOCINFO, CropBox, initial view, bookmarks and links is demopdfm.ps.

An example of setting one page to landscape is landpdf.ps.


Russell Lang, Ghostgum Software Pty Ltd, 2004-03-25