Linux.com

Home Learn Linux Linux Tutorials Creating PDF files with ps2pdf

Creating PDF files with ps2pdf

 

For better or for worse, Adobe's Portable Document Format (PDF) is a wildly popular way of exchanging information. On Windows and Mac OS, most people create PDF files by first creating a PostScript file and then using Adobe Acrobat Distiller to generate a PDF. Linux, however, has no version of Distiller. There are a number of ways to create a PDF in Linux, but one of the most popular methods is to use a utility called ps2pdf.

 

 

A number of Linux applications, most notably OpenOffice.org and TeX, can create PDFs without the need to create a PostScript file, so why use ps2pdf? Because there are times you need to create PDF files that these applications don't support.

According to the ps2pdf documentation, ps2pdf can "process complex and difficult PostScript files that Acrobat Distiller is not able to handle." I've never run into this situation myself, but I know of a few people who turned in desperation to ps2pdf because they could not get usable output from Acrobat Distiller.

ps2pdf is a script that comes bundled with GhostScript, a freeware PostScript interpreter. ps2pdf runs a PostScript file through GhostScript and outputs a PDF file. GhostScript is a standard part of most Linux systems. To find out if ps2pdf is installed on your system, type which ps2pdf at the command line. If a path is displayed (for example, /usr/bin/ps2pdf), then you're ready to go. If not, you can download and install GhostScript to get it.

Running ps2pdf

Let's assume that you've created a PostScript file and now want to convert it to PDF. If you are in a graphical environment like KDE or GNOME, open a terminal window and change to the directory containing the PostScript file that you want to convert. Then, type ps2pdf followed by the name of your PostScript file. For example:

ps2pdf gundam.ps HeavyArms.pdf

You don't need to include the name of the PDF file on the command line. If you leave it out ps2pdf gives the PDF file the name of the PostScript file and a .pdf file type.

If all goes well, you'll have yourself a PDF file that you can view in Acrobat Reader, xpdf, or any other PDF viewer. The PDF may have no navigation features or links, and it may also be quite large, but it can be used for creating proofs, for archiving, or for passing documents to family, friends, and colleagues.

If you are using Knoppix you may find that ps2pdf doesn't work. I've been told that version 3.2 of the Knoppix distribution doesn't come with the ps2pdf script. Instead, it uses a script called ps2pdfwr, which uses the same syntax as ps2pdf.

Using ps2pdf's parameters

One way to enhance your PDF files is to use ps2pdf with one of more of the 75 Acrobat Distiller parameters. The Distiller parameters are simply command-line options that enable you to change how your PDFs are produced, and they're documented in the file ps2pdf.htm in the doc folder where GhostScript is installed. ps2pdf can use all of the parameters available to Acrobat Distiller, but I have found four in particular to be the most useful, as we'll see in a moment.

Using the Distiller parameters with ps2pdf is simple. On the command line, type ps2pdf followed by -d, followed by the parameter name, followed by the name of the PostScript file:

ps2pdf -dParameterName myFile.ps

Setting PDF compatibility

As with many file formats, PDF gains new features with each new version of Acrobat. For example, PDFs created with Adobe Acrobat versions 4.0 and onward support enhanced security, transparency, compression, and annotations. However, you never know what version of a PDF reader someone will be using, or whether it will support the full feature set. To ensure that your PDFs can be viewed in both newer and older versions of PDF readers, you can set a compatibility level for the PDF by using the CompatibilityLevel=x.x parameter, where x.x is the version of the PDF specification. Note that the specification number does not correspond to the version of the Acrobat software. Rather, there are three values you can use with this parameter:

  • 1.2 -- equivalent to a PDF created with Acrobat version 3.0. This is the ps2pdf's default.
  • 1.3 -- equivalent to a PDF created with Acrobat version 4.0.
  • 1.4 -- equivalent to a PDF created with Acrobat version 5.0.

For example:

ps2pdf -dCompatibilityLevel=1.3 ps2pdf.ps

If you are unsure of what compatibility level to use, stick with the default. If you have version 7.0 or higher of GhostScript installed on your computer, you can use the ps2pdf13 or ps2pdf14 scripts to automatically set the compatibility level.

Optimizing your PDFs

No matter what size your PDF file is, you probably want to create it so that the file opens and displays as quickly as possible, especially if you plan to host the file on a network or on the Web. Optimizing a PDF tweaks the structure of the file so that a server sends a page only as it is requested, rather than loading the entire PDF file. To optimize your PDFs, use the Optimize=true parameter. For example:

ps2pdf -dOptimize=true report.ps

Embedding fonts

In some PDF files the fonts appear fuzzy. I have found this is especially true of PostScript files created with the TeX and LaTeX typesetting systems, which by default use a unique set of fonts. If you find that the fonts in your PDF file are fuzzy, you can embed the fonts used in the source document with the PDF. Embedding fonts ensures that:

  • Your PDF is created with the standard 12 PostScript fonts, and
  • Any fonts that may not be installed on the computer on which your PDF is being viewed are available.

Embed fonts by adding the EmbedAllFonts=true parameter to the command line:

ps2pdf -dEmbedAllFonts=true WonderDog.ps

The main drawback to using EmbedAllFonts is that your PDF will become noticeably larger.

Compressing the PDF

You can shrink large PDF files by as much as half of their original size using the UseFlateCompression=true parameter. Flate compression, also called Zip compression, does a good job shrinking text and graphics without distorting the content.

To use flate compression with ps2pdf, type the following at the command line:

ps2pdf -dUseFlateCompression=true greatNovel.ps

Specifying the target device

One of the great things about PDF files is that they can be used for a variety of purposes. They can be viewed on screen, downloaded as electronic books, or printed on a laser, ink jet, or professional printer. You can specify five types of target device for your PDF using the PDFSETTINGS parameter:

  • /screen -- creates a low-resolution PDF optimized for reading on a monitor.
  • /ebook -- the PDF has a slightly higher resolution, for better reading of electronic books on a monitor or on a smaller device like a PDA or an electronic book reader.
  • /printer -- the PDF is generated for printing on a laser or ink jet printer, or to be distributed on a disk or CD-ROM.
  • /prepress -- the PDF is generated for a high-quality printing press.
  • /default -- the PDF can be used for all the output listed above. However, the file will be larger than it would if you used a single type.

To use this parameter with ps2pdf, type the following at the command line:

ps2pdf -dPDFSETTINGS=/printer galleyProof.ps

Troubleshooting

While ps2pdf generally does a good job, I've found it susceptible to a couple of common problems. Sometimes, ps2pdf has trouble with a PostScript file and fails to perform the conversion. If something goes wrong, a set of indecipherable messages appears in the terminal window as shown below:

Error message

If you see this, try recreating the PostScript file and running ps2pdf again. This kind of error message also rears its head when you mistype the name of the PostScript file. Check your typing against the name of the PostScript file and try running it through ps2pdf again.

A second problem with ps2pdf is that the PDFs produced by ps2pdf vary in quality from production level to fuzzy. The quality of the output really depends on the source PostScript file and the fonts that you use. Often the output looks fuzzy because the fonts in a document aren't the standard 12 PostScript fonts. Instead, ps2pdf uses lower-quality bitmapped fonts. While the document may appear distorted in a PDF reader, it actually prints clearly and cleanly. To get around this problem, try using the EmbedAllFonts=true parameter.

Conclusion

ps2pdf is a viable alternative to Acrobat Distiller. It's easy to use, fast, and in most cases creates a nice PDF file. With a few additions to the command line you can use ps2pdf to make handsome PDF files, and you don't have to spend hundreds of dollars on proprietary software to do it.

Scott Nesbitt, a devoted Linux user, writes from Toronto, Canada.

 

Comments

Subscribe to Comments Feed
  • Umesh Said:

    Hi I cropped existing pdf some area and i converted pdf2ps. Because i want to remove Cropped area, for that i make pdf2ps from ps to pdf i am creating, But i am getting additional objects. When i print that ps file into my Adobe distiller only croped area is there, how can i do in the ps2pdf need help... Thanks, Umesh

  • Jason Said:

    How do you create a pdf from the simple utf-8 text file in linux using the command line? I tried a2ps, which does not support utf-8 and paps, which creates 100 times bigger file than the original text file. Due to the pdf size, the paps is impractical. Is there any other way in linux? Thank you.

  • Ed Berry Said:

    A .pdf will always be bigger than a text file because it has more information. But it could be far bigger if it specifies position of each character, prints as an image, or defines a million definitions that you wont use. The following is a very economical test to ps converter: prepended to the beginning of a file, it makes the file a postscript file. save the rest of this message starting with "%!PS-Adobe-" cat imprint.ps test.txt | ps2pdf - test.pdf (don't skip the hyphen which tels ghostscript to take input from the pipe) (you could alias txt2ps "cat imprint.ps \!$ | ps2pdf - \!$2" ) (There are provisions for changing font, font size, and lots of formatting) %!PS-Adobe- /in. {72 mul} def /line 512 string def /buff 10 string def /leftmargin where not {/leftmargin .75 in. def} {pop} ifelse /bottom where not {/bottom .5 in. def} {pop} ifelse /startX where not {/startX leftmargin def} {pop} ifelse /startY where not {/startY 10 in. def} {pop} ifelse /fontname where not {/fontname /Courier-Bold def} {pop} ifelse /pointsize where not {/pointsize 12 def} {pop} ifelse /leading where not {/leading pointsize 2 add def} {pop} ifelse /headerfontname where not {/headerfontname /Times-Roman def} {pop} ifelse /headerpointsize where not {/headerpointsize 9 def} {pop} ifelse /headerfont headerfontname findfont headerpointsize scalefont def /page where not {/page 1 def} {pop} ifelse /pagenumber where not {/pagenumber true def} {pop} ifelse /printheader { gsave currentdict /header known { 36 startY 0.5 in. add moveto headerfont setfont header show } if pagenumber { 8 in. startY 0.5 in. add moveto headerfont setfont page buff cvs show /page page 1 add def } if grestore startX startY moveto } bind def /crlf { leftmargin currentpoint exch pop leading sub dup bottom le { pop pop showpage printheader } { moveto } ifelse } bind def /printfile { line readline { { show crlf currentfile cvlit line readline not {exit} if } loop } if showpage } bind def fontname findfont pointsize scalefont setfont printheader currentfile cvlit printfile

  • ed berry Said:

    This was meant as a reply to Jason's question, for anyone googling to here. But the newline characters got stripped which may be OK for the postscript but makes it hard to read.

  • Jason Said:

    Ed Berry, thank you very much for the comment. I am now digesting your comment to see if I can solve the problem with your comment. It will take some time since I have no knowledge in this. I will post the result. Thank you.

  • Jason Said:

    Ed, by the way, I think the pdf prints each character as an image, and that is why the pdf size is more than 100 times bigger.

  • Jason` Said:

    Ed, I get the following error. As you suggested, I placed the text starting with %!PS at the beginning of the utf8 text file, and I ran the following command. Am I doing something wrong? Could you help on this? Thank you very much. ++++++++++++ [jmin@jmin-f13 ~]$ cat span_korUTF8_test.txt | ps2pdf - span_korUTF8_test.pdf Error: /undefined in S Operand stack: Execution stack: %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1878 1 3 %oparray_pop 1877 1 3 %oparray_pop 1861 1 3 %oparray_pop 1755 1 3 %oparray_pop --nostringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- Dictionary stack: --dict:1154/1684(ro)(G)-- --dict:0/20(G)-- --dict:70/200(L)-- Current allocation mode is local GPL Ghostscript 8.71: Unrecoverable error, exit code 1 ++++++++++++++++++++++++ span_korUTF8_test.txt file contents ++++++++++++++++++++++++++ %!PS-Adobe- /in. {72 mul} def /line 512 string def /buff 10 string def /leftmargin where not {/leftmargin .75 in. def} {pop} ifelse /bottom where not {/bottom .5 in. def} {pop} ifelse /startX where not {/startX leftmargin def} {pop} ifelse /startY where not {/startY 10 in. def} {pop} ifelse /fontname where not {/fontname /Courier-Bold def} {pop} ifelse /pointsize where not {/pointsize 12 def} {pop} ifelse /leading where not {/leading pointsize 2 add def} {pop} ifelse /headerfontname where not {/headerfontname /Times-Roman def} {pop} ifelse /headerpointsize where not {/headerpointsize 9 def} {pop} ifelse /headerfont headerfontname findfont headerpointsize scalefont def /page where not {/page 1 def} {pop} ifelse /pagenumber where not {/pagenumber true def} {pop} ifelse /printheader { gsave currentdict /header known { 36 startY 0.5 in. add moveto headerfont setfont header show } if pagenumber { 8 in. startY 0.5 in. add moveto headerfont setfont page buff cvs show /page page 1 add def } if grestore startX startY moveto } bind def /crlf { leftmargin currentpoint exch pop leading sub dup bottom le { pop pop showpage printheader } { moveto } ifelse } bind def /printfile { line readline { { show crlf currentfile cvlit line readline not {exit} if } loop } if showpage } bind def fontname findfont pointsize scalefont setfont printheader currentfile cvlit printfile S 863 G216Be Bell-Villada, Gene H., 1941- García Márquez : el hombre y su obra / 5110146 37244164598824 03/29/13 ADLT 2012 S 929.44 A3255 Albaigès i Olivart, Josep M., 1940- El libro de los nombres de niño y de niña : [su K C5461333 Chŏn, Hye-jŏng, 1975- Haehyŏp ŭi pit : Chŏn Hye-jŏng sosŏl. 5149883 37244207163891 03/16/13 ADLT 2012 K C548391 Chŏng, To-gŏn. Nae k'ang salcha : Chŏng To-gŏn changp'yŏn sosŏl


Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board