From LaTeX to PDF

How to generate PDF documents from LaTeX code

Author: Francesco Poli
Contact: invernomuto@paranoici.org
Version: 0.5
Copyright: Expat license
Notice:

Copyright (c) 2005-2014 Francesco Poli

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About this document
Web form HyperText Markup Language
Source form reStructuredText
Web stylesheet Cascading StyleSheets
Build directives Makefile

Contents

You have LaTeX, you want PDF

LaTeX manuals and tutorials often teach you how to compile LaTeX code into DVI (DeVice Independent) and PS (PostScript) formats and possibly mention the PDF (Portable Document Format) output as secondary option. However, in most cases though, PDF is a more convenient output form, as it supports hyperlinks, is typically faster to display on screen and equally suitable for printing.

This document tries to summarize the main methods to produce a PDF file starting from LaTeX code.

I will assume you use TeX Live under a Unix or Unix-like system. Some of the following reasoning may hold even for different (La)TeX distributions and/or different OS families: I simply do not know...

To be more precise, this document is basically focused on tools available in Debian GNU/Linux: feel free to point out any differences that apply to other distros or free operating systems, if you know better.

The classical method: use ps2pdf

Compile as if you were interested in the DVI output:

$ latex ${FILE}.tex

where ${FILE} is obviously the (extension-less) filename of your document. It may be necessary to re-run the command a second time, to get cross-references right or to properly build tables of content (or similar stuff...). Maybe you will add a bibtex run and a third latex run, or some other you-know-what...

Anyway, in the end, you have a DVI file and you may convert it to PostScript with:

$ dvips ${FILE}.dvi -o ${FILE}.ps

Finally you get a PDF file by issuing the following command [1]:

$ ps2pdf ${FILE}.ps
[1]ps2pdf is part of GhostScript (Debian package: ghostscript)

Shorthands

The above-mentioned commands may be shortened by omitting several details:

$ latex ${FILE}
...
$ dvips ${FILE} -o
$ ps2pdf ${FILE}.ps

Old options

In the past, the classical method used to produce an output encumbered with Type 3 fonts, that are not well supported when they end up in a PDF file. This caused the PDF file to look badly on most PDF viewers.

Actually, on old systems (such as Debian woody), the default behavior of dvips and ps2pdf used to produce bad PDF files, unless the following options were used:

$ latex ${FILE}
...
$ dvips -Ppdf -G0 -Z ${FILE} -o
$ ps2pdf -sPAPERSIZE=a4 -dMaxSubsetPct=100 -dCompatibilityLevel=1.2 \
      -dSubsetFonts=true -dEmbedAllFonts=true \
      -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode \
      ${FILE}.ps

Fortunately newer distros (Debian sarge and later releases) fixed the behavior (so that Type 1 fonts are used by default) and these options are no longer needed (unless you seek backward-compatibility with very old systems).

Note that you can check the fonts used in a PDF file by issuing the following command [2]:

$ pdffonts ${FILE}.pdf
[2]pdffonts is part of the Poppler library suite (Debian package: poppler-utils)

The simplified method: use dvipdf

Who needs the PostScript file? If you are only interested in the PDF final output, you will probably discard any PS file you generated on your way. So why bothering to generate them?

Skip the PostScript step [3]:

$ latex ${FILE}
...
$ dvipdf ${FILE}
[3]dvipdf is part of GhostScript (Debian package: ghostscript)

Another simplified method: use dvipdfm

Another possibility is using dvipdfm [4] rather than dvipdf:

$ latex ${FILE}
...
$ dvipdfm ${FILE}
[4]dvipdfm is part of TeX Live (Debian package: texlive-binaries)

The direct method: use pdflatex

If you don't need a PostScript output, maybe you don't need a DVI one either... You can use pdflatex [5] and compile LaTeX code directly into PDF format:

$ pdflatex ${FILE}
...

Note that subsequent compiler runs (when they are needed at all) must as well be based on pdflatex.

[5]pdflatex is part of TeX Live (Debian package: texlive-latex-base)

Prepare your LaTeX code for pdflatex

Warning! Compiling a LaTeX document with pdflatex may work out of the box in simple cases, but some care is due in the general case. Remember:

  • if you load the graphicx package with the dvips option, then use the pdftex option instead, or, even better without any such option
  • if you use the \includegraphics command, do not include EPS (Encapsulated PostScript) files: supported formats are PDF, PNG (Portable Network Graphics), JPEG (Joint Photographic Experts Group), and others...
  • if you use the \includegraphics command, omitting the file extension is recommended: if you do so, a file with the appropriate extension is automatically searched for and selected (and much fewer changes are needed, should you decide to switch back to latex or again to pdflatex)

Conclusions

As can be seen, there are more than one method to generate PDF files starting from LaTeX code. Which is the best strategy? The answer is: it depends on your needs. Hence the suggestion is: choose the method that best fits your needs on a case by case basis.