Sunday, February 20, 2011

Latex: Reduce PDF file size with latex + dvips + ps2pdf

Recently I have run into a funny problem with pdflatex. My one page document compiled in 111kb PDF file. How could it be that one page of plain text with no graphics or anything fancy generates such a huge file? Usually I don't care, we all have broadband and terabytes of the space on our hard drives these days. I didn't even noticed that until it was rejected by some weird submission system that has a strict limit on the file size. Trying different things, I found that the combination of latex + dvips + ps2pdf reduces the file size by almost 3 times:

pdflatex 111,214 bytes
latex + dvips + ps2pdf 40,544 bytes

As far as I know, pdflatex dumps all possible glyphs in the resulting pdf causing such a big difference, while the other option does not. Visually I found no significant difference between these two documents to bother with a bigger file.

Unfortunately TeXworks does not have an option for latex + dvips + ps2pdf by default, so I had to create it myself. It requires a few tricks (the instructions are for Windows, but the same idea works for Linux/Mac):

  • Create a latex-dvips-ps2pdf.bat somewhere on your drive with the following line
    @latex -synctex=1 "%1.tex" && dvips "%1.dvi" && ps2pdf "%1.ps"
  • In TeXworks open Preferences, Typesetting tab. Add "latex -> dvips -> ps2pdf" to Processing tools. Point to your latex-dvips-ps2pdf.bat file and provide $basename as your argument.
  • Try it and check your paper size.

If you use a different paper size than the one used in your latex system, you may run into problems. ps2pdf knows nothing about the paper size you've specified in you latex file. If you use MiKTeX, you can modify paper size in the MiKTeX settings program, say A4 or Letter (letterSize didn't work for me), otherwise you may need to include additional parameters for dvips and ps2pdf in your bat file.

No comments: