Linux.com

Feature

Creating PDF files with ps2pdf

By Scott Nesbitt on March 23, 2004 (8:00:00 AM)

Share    Print    Comments   

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.

Scott Nesbitt is a freelance journalist and technical writer based in Toronto, Canada.

Share    Print    Comments   

Comments

on Creating PDF files with ps2pdf

Note: Comments are owned by the poster. We are not responsible for their content.

Another method for producing PDFs dynamically

Posted by: Anonymous Coward on March 23, 2004 06:47 PM
ps2pdf's real nice - I would often use this to convert my Postscript files to PDFs for the Windows-using friends/fiends<nobr> <wbr></nobr>:-)

Another method for producing PDF's is through the Reportlab library for Python. The approach is quite different compared to the typeset-oriented nature of most postscript software - it's a programming oriented approach which yields PDF's on the fly. Quite useful if you want to embed say a PDF generation tool in your web-based applications.

#

From KDE

Posted by: Anonymous Coward on March 23, 2004 07:27 PM
Are you running a KDE application you don't have to bother with all this. With the exelent Kprinter framework, all KDE applications can nativly print to PDF. With non KDE applications you can also use kprinter, by changing the printer to kprint from cups or whatever.

#

Re:From KDE

Posted by: Anonymous Coward on March 23, 2004 09:00 PM
But if you're not using KDE the ps2pdf is a pretty good alternative. As is a program called PStill (http://www.pstill.com), although it can be harder to configure than ps2pdf.

#

OpenOffice.org

Posted by: Anonymous Coward on March 23, 2004 09:46 PM
If you are using the latest version of OpenOffice.org then you can use the one-click print to PDF feature. My experience with it has been excellent.

#

Re:OpenOffice.org

Posted by: Anonymous Coward on March 23, 2004 10:17 PM
The extendedPDF macro from http://www.jdisoftware.co.uk/pages/epdf-home.php is even better than OOo's built-in PDFer. With the macro, you can compress the PDF, add bookmarks and links, and more.

#

wrong (I am really sorry to say)

Posted by: Anonymous Coward on March 24, 2004 04:19 AM
I use OpenOffice daily and I love it. But the PDF export features is buggy to the point of being useless. It works well only for 1-2 page documents, after that it usually places one paragraph at at totally wrong place (on my machine on the upper right hand side), and it does this over and over again. Kword's PDF export works much better. So what I end up doing is using OpenOffice to write me text, export it to RTF (I really hate this part, but RTF is the only format which OpenOffice and Kword can share and keep text formatting - which is a crying shame for two free softwares!) import the RTF into Kword and then procude my PDF.



Yo! Anyone at OpenOffice paying attention to this?

(but really - thank you guys for OpenOffice - its an otherwise great ap!)

#

Re:OpenOffice.org

Posted by: kirkjobsluder on March 25, 2004 01:37 AM

I've experienced a number of problems with OpenOffice PDF export including:




  1. Bad handling of imported graphics. EPS in particular does not work.

  2. OOo export file sizes tend to be %50 larger than ps2pdf.

#

Re:OpenOffice.org

Posted by: Anonymous Coward on March 25, 2004 02:42 AM
What about creating PS fr OO and then convert by ps2pdf?

#

Re:OpenOffice.org

Posted by: kirkjobsluder on March 25, 2004 03:38 AM
that's what I usually do.

#

But what about editing?

Posted by: Anonymous Coward on March 23, 2004 10:14 PM
Being able to create PDFs is all very well, but what would be really neat would be something to edit them (or at least import them to another app).

On a few occasions I've been sent a PDF to which I needed to make a correction when the original author was not available...

#

Re:But what about editing?

Posted by: Anonymous Coward on March 23, 2004 10:28 PM
Recent versions of Kword can do this with varying degrees of success. Even if the import goes well, graphics and text frames will have to be dragged around until everything looks just so.

PDFs are not word processor documents and weren't really meant to be edited. Even the full version of Acrobat will only let you do so much with a pdf it didn't create.

#

Two easier methods

Posted by: Anonymous Coward on March 23, 2004 10:41 PM
1) Use GSview - which has a drop down menu for selecting the command line options.

2) Use Scribus - it generates superb PDF's

#

Re:Two easier methods

Posted by: scottn on March 23, 2004 10:55 PM
Actually, in an earlier draft I included a section on using GSView and a little utility called xps2pdf. But the article was getting a bit long so I chopped that section.

#

What about pdf2ps?

Posted by: Fonze on March 24, 2004 01:55 AM
As other commenters have brought up, editing a pdf is an issue. Is there a utility that converts from pdf to ps? There is a man page for pdftops that describes it as a utility for printing a pdf file..



While I have read an article on pdfs a while back that explained the difficulties in editing a pdf (it was about a proprietary pdf editor for companies that cost thousands), I would assume that if a utility like ps2pdf was used to create the original pdf, it may be possible to back out of the pdf, going back to postscript for re-editing, then use ps2pdf to return it back to pdf. I would also assume that if a different utility or app was used to create the original pdf, it would be much more complicated or impossible for a pdftops utility to re-edit the document.



I haven't tried pdftops, just discovering it on my computer now. I know that on an older Linux distro, it wasn't installed when I tried it.



Any info on whether the other utility, pdftops works, and can it be used to edit a pdf document after conversion, then pstopdf to get it back?

#

Problems with this mechanism

Posted by: Anonymous Coward on March 24, 2004 12:22 AM
Usually, the resulting pdf can't be searched, and you can't copy text from it.

If you are starting from a text file, and want it to be useful as a text, it's better to use a formatting tool that outputs pdf directly (like, say, docutils with the reportlabs writer, if you can make it work)

#

Re:Problems with this mechanism

Posted by: Anonymous Coward on March 24, 2004 12:34 AM
Really? I've been using ps2pdf for a long time and, except some rare cases, have been able to copy text and search my PDF files.

#

Re:Problems with this mechanism

Posted by: Anonymous Coward on March 24, 2004 04:37 AM
It depends mostly on what you use to generate your ps files.

#

Latex to pdf

Posted by: Anonymous Coward on March 24, 2004 01:11 AM
You can make really nice looking pdf files with the latex package pdftex. Fonts look great and links actually work (e.g. you can click on chapters in the contents listing). Thumbnails can be generated as well using thumbpdf.

#

Another excellent article

Posted by: Fonze on March 24, 2004 01:45 AM
Attention Newsforge management:



It is articles such as this one, and the one yesterday on <A HREF="http://www.newsforge.com/article.pl?sid=04/03/22/216233" TITLE="newsforge.com">Video Production with Linux</a newsforge.com> (even though it appeared on OSNews) that enhance and really bring value to Newsforge. I found an application (Audacity) that even though was on my system, I didn't know I had, and didn't know anything about, even though I needed something exactly like it. Not only have I discovered it thanks to the article (and have received leads on other useful apps), but I get a decent explanation of features and flags.



The same applies to this excellent article on ps2pdf. I have been using it regularly since I discovered that pdf files printed through OpenOffice.org fail to open, with an error message, when I send them to a Windows user. So my early attempts at getting documents to a Windows user using OpenOffice.org was failing (and KDE's Kword crashes regularly and is absolutely useless for me). I switched to printing OpenOffice.org's Write files as postscript, then using ps2pdf to convert to pdf, and then sending those pdf files to a Windows user for printing. Works without any problems whatsoever every time. I may not have checked ps2pdf's functionality on Knoppix 3.2, but it works without any problems at all on Knoppix 3.3



As is usual, with a command line that just works, I fail to check --help or the man pages for other options/flags that will allow me to get more use out of the command. It is articles such as this excellent one today that opens my mind, and informs me and other Newsforge readers to the other possibilities out there with these commands.



I want to send a big thanks to Scott for writing this article. I will put it to good use.



Roblimo, OSDN management, you need more articles like this, both here and on Linux.com, to grow the readership, which is one of your goals for this site, right? Do whatever you have to, raising payments, advertising for articles (more than you already are) or whatever else you can to get quality submissions such as this. I have seen the command line articles that have been appearing lately. I haven't been able to read them all, but I know that I can come back to Newsforge (and Linux.com) and find the articles and read them later when I'm able to.



Newsforge, from the posts I've seen, not only has a readership that is technically proficient and familiar with GNU/Linux, but also has a readership (which must be growing) that is on the newbie side as well. We need the tips, help, hand holding, and more. Bringing attention to apps that are useful and replacements to Windows equivalents are always good. Bringing attention to features in those apps that we may not be aware of is even better. This is what will make the difference between someone that barely gets by, and a power user.



Keep 'em coming, Roblimo, Joe Barr, and others behind the scenes. And the same with the writers, Scott, Paul, and everyone else who has written articles such as this, or has submitted links to articles such as the Video article.



Once more, thanks!

#

Re:Another excellent article

Posted by: Anonymous Coward on March 24, 2004 04:34 AM
Well, they could start by replying to mails sent to the editors address offering to write articles<nobr> <wbr></nobr>;-)

I sent one there over a week ago, and no answer yet. Not even a "not interested".

Just in case an editor reads this, CHECK YOUR INBOX!

#

Roblimo's too busy

Posted by: Anonymous Coward on March 24, 2004 05:08 AM
I was just wondering about this. wtf is Roblimo? I haven't been paying too much attention to the bylines lately, but unless I'm mistaken, Roblimo's been awol.



I figure this cold wave of whether sent him hightailing it south. He's probably sipping a Margarita, while he hangs with her on a hotel balcony watching all the spring breakers go by.



If the cold weather doesn't let up, we may never get him back.



btw, he did publish some guidelines a while back, along with a good book to read for writing technique (I forget what it is). The payment isn't much, but few people ever made money writing for a living. As for answering your email, like I said, he's probably too busy with Margarita and sunshine right now.

#

Re:Roblimo's too busy

Posted by: Anonymous Coward on March 24, 2004 11:56 PM
It's ok. The article will get published on Linuxworld. They pay better anyway.

#

Linuxworld

Posted by: Anonymous Coward on March 25, 2004 12:28 AM


They may pay better, but they also astroturf.

#

HELP! pdf seach engine badly needed!

Posted by: Anonymous Coward on March 24, 2004 04:23 AM
Hi everyone,
I need a good search application to search for strings of text not in one open pdf document, but in a directory with almost 100 pdf documents. Is there an application which can help me do this (without opening all these documents one by one)?
Thanks!

#

Re:HELP! pdf seach engine badly needed!

Posted by: Anonymous Coward on March 24, 2004 05:03 AM
htdig will use acroread to allow the searching of pdf files.

#

Re:HELP! pdf seach engine badly needed!

Posted by: Anonymous Coward on March 24, 2004 07:41 AM
from the htdig FAQ:

"1.13. Why do you use a non-free product to index PDF files?

We don't. You can use the "acroread" program to index PDF files, but this is no longer recommended. Initially this program was the only reliable way to extract data from PDF files. However, the xpdf package is a reliable, free software package for indexing and viewing PDF files. See question 4.9 for details on using xpdf to index PDF files. We do not advocate using acroread any longer because it is a proprietary product. Additionally it is no longer reliable at extracting data."

BOYCOTT PROPRIETARY SOFTWARE - it is both immoral and self-defeating for the GNU/Linux community!

#

using ps2ps to clean up the .ps file

Posted by: Anonymous Coward on March 24, 2004 05:39 AM
Sometimes the problems in ps2pdf occur because of issues in the source<nobr> <wbr></nobr>.ps file. Those issues can often be corrected by running your<nobr> <wbr></nobr>.ps file through ps2ps. This can also help fix ps or pdf files that don't print properly, for whatever reason.

I've seen this most often with<nobr> <wbr></nobr>.ps files created my OpenOffice.org.

#

Re: using ps2ps to clean up the .ps file

Posted by: Anonymous [ip: 192.18.43.225] on August 21, 2007 06:28 PM
This solved my problem. Thanks.

Using mozilla 1.7 browser on Solaris, I printed a file to ps. Neither distill nor ps2pdf worked. After running ps2ps, ps2pdf successfully converted my files to pdf.

#

dude is on crack

Posted by: Anonymous Coward on March 24, 2004 07:39 AM
you don't need ps2pdf on mac os. everything on os x prints to pdf. every application, even carbon apps, can print to pdf. all you have to do is in print, hit preview, and viola, a pdf preview is created. then save the pdf. that's it. and by the way, ps2pdf is not a stand alone app. it is part of ghostscript which is a free cross platform suite of apps. very nice to.

as a side note, i showed off this feature on my linux laptop a couple of years ago in a college class. prof was very interested. i showed him how to set up a smaba/ftp server for his classes, and he could send all his files to it, and wrote a bash script to automate ps2pdf all the files. very cool.

but, on os x, everything is a pdf. it's built into the rendering engine.

#

and you are still in corporate handcuffs!

Posted by: Anonymous Coward on March 24, 2004 07:55 AM
OSX? Are your kidding us? You can safely assume that most of us understand the importance of free software and all the implications of using proprietary software. Why in the world would you tell us anyting about Mac?! What's OSX have to do with us anyway?
OSX is the BSD-based UNIX-like of those who FOR YEARS have told everyone how grand Mac OS1-9 is, how horrible the CLI is, how grand the bloated and totally opaque GUI layer of Mac is and who, suddenly, unexplicably, have "seen the light" and now looooove UNIX. And, needless to say, the UNIX version they advocate is, what else, the ultra-proprietary OSX. Well guys, you were full of shit then and you are full of shit now. As far as UNIX is concerned - you are only wannabes. As far as freedom is concerned - you are little more than a source of income for large software corporations (including M$ who owns part of Apple) who have as much concern for freedom as Joe Stalin did.

#

some misleading information

Posted by: Anonymous Coward on March 24, 2004 08:32 AM
This is a useful article in many respects but has some rather misleading or erroneous information too.

The Optimize parameter has no effect (in ps2pdf).

The UseFlateCompression parameter is set to true by default in every standard configuration and so is hardly worth mentioning unless one, for some reason, wants to turn it *off*.

The section on Embedding Fonts is completely wrong. Take a PDF that displays "fuzzy fonts" in your Acrobat reader on Linux and open it in gv or xpdf or a version 6 Acrobat Reader on Windows. No fuzzy fonts! So the fuzziness doesn't come from the "unique set of fonts" used in TeX or from "lower quality bitmapped fonts" substituted by ps2pdf; it comes from the Acrobat reader itself (versions less than 6). Nobody seems to know for sure why Adobe did this and I know that one shouldn't attribute to malice what can be explained by incompetence, but some have suggested that Adobe was simply trying to sell more of its type 1 fonts.

In any case, to work around this undesirable feature of the Acrobat Reader, one must avoid embedding type 3 fonts. For TeX/LaTeX users, this means configuring dvips to produce Postscript using type 1 fonts only; ps2pdf will then embed the type 1 fonts into the pdf and the fuzziness in the Acrobat Reader will be gone.

The real reasons to embed or not embed fonts in a pdf have to do with the size of the pdf versus its
faithfulness to the original. If a font is used in a document and not embedded, the pdf will be smaller but another font may have to be substituted by the reader. The differences between Times and Times New or between Helvetica and Arial are minor and can often be ignored. This is why the "standard 14" fonts are normally *not* embedded. But in high-quality pre-press situations, *all* fonts should be embedded, not to avoid fuzziness but to be faithful to the original.

#

ps2pdf

Posted by: Anonymous Coward on April 20, 2006 06:10 AM
You can try ps2pdf and other ghostscript powered tools at <a href="http://www.freeonlineapps.com/" title="freeonlineapps.com">http://www.freeonlineapps.com/</a freeonlineapps.com>.

#

negative article

Posted by: Administrator on April 07, 2004 11:04 PM
For better or for worse, Adobe's Portable Document Format (PDF) is a wildly popular way of exchanging information.

Pretty bad policy to disparage something you are writing an article about in the first paragraph.

To find out if ps2pdf is installed on your system, type which ps2pdf at the command line.

In GNU/Linux you would type "type ps2pdf".

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.

Or you can configure ghostscript so that if finds the required fonts.

This kind of error message also rears its head when you mistype the name of the PostScript file.

Or due to a misconfigured ghostscript. Try setting the <tt>GS_FONTPATH</tt> parameter.

#

Top of the pdf page is missing

Posted by: Administrator on October 08, 2006 03:52 AM
When I use ps2pdf, 18 mm of the top of my pdf page are missing.

Any tips ?

Not sure this is the good forum to deal with<nobr> <wbr></nobr>...

#

Re: Top of the pdf page is missing

Posted by: Anonymous [ip: 87.69.59.136] on August 17, 2007 04:21 PM
ps2pdf -sPAPERSIZE=a4 filename.ps

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya