It is a myth that pdf->ps->pdf
will always give you a smaller file size. For e.g., consider
(in mathematica)
fileName = "test.pdf";
p = DensityPlot[Sin[x] Sin[y], {x, -4, 4}, {y, -3, 3}];
Export[fileName, p];
(in the shell)
pdf2ps test.pdf && ps2pdf test.ps test1.pdf
test.pdf
is 446kB, whereas test1.pdf
is a whopping 11.5MB on my machine!
What happens normally, is that when you export a PDF file using Mathematica, it stores the text (axes tick marks, labels, etc), fonts and images, all of which add to the bulk. If you have vector graphics with lots of tick marks/labels, these can be quite significant. Doing pdf->ps->pdf
will strip the file of font data and text data, at the same time compressing the image, which is why you see a decrease in file size. Although not observable at small magnifications, you will notice the difference when you zoom in considerably. For e.g., replacing p
with
p = Plot[Sin[t], {t, 0, 2 Pi}];
produces a 37kB file for test.pdf
and an 8kB file for test1.pdf
on my machine. The quality of the 8kb file is poorer than the original (zooming to 600%+ shows this). You can see the plain text information that mathematica stores in the PDF file by doing
Import[fileName,"Plaintext"]
However, for more complicated PDF files, converting to ps
and then back to pdf
is not necessarily a good option and can result in the file size blowing up.
You can try playing with the options for PDF in Export
, such as "AllowRasterization"
and ImageResolution
, or even rasterize the image before saving, but I guess you already knew that.
Here's a back-alley quick and dirty way of reducing file size from within mathematica
Export[fileName, First@Import@Export[fileName, p]]
The file size is now only 12kB! It's only a third of the original, only slightly larger than that obtained from pdf->ps->pdf
, and is of a much better quality than the 8kB one (although not exactly the same as the original. The differences can be seen at magnifications of 1200% and above).
What happens is that upon importing and re-exporting, only the stored image is saved. The plain text part of it is thrown away, and in this small example, that happened to be significant. For the DensityPlot
example, the reduction was only to 425kB (still, went down rather than blow up to 11MB).