23
$\begingroup$

Note: Fixed in version 10. See Verbeia's answer.


I've upgraded from Mathematica 8 to 9 (which apparently leaves the old version installed and usable which I didn't expect). I see a significant difference in the size of the PDFs produced by 'Save Graphic As', or Export[].

For example, with this very simple plot:

Plot[
 Abs[Sinc[x]], {x, 0, 4 Pi}
 , AxesLabel -> {x, Abs[Sinc[x]]}
 , PlotRange -> Full
 ]
Export["foo8.pdf", %]

I see a pretty reasonable seeming size with Mathematica 8

-rwxrwxrwx  1 Peeter None    35493 Dec  7 12:18 foo8.pdf

But with Mathematica9 the files, even for this very simple plot are very much bigger:

-rwxrwxrwx 1 Peeter None 1550317 Dec  7 12:19 foo9.pdf

(44 times bigger).

Are there any Export[] options that allow the file size to be reduced? What accounts for the drastic increase in size when using the new Mathematica release (bug or feature)?

I can choose to export in other formats (like PNG), but when I embed a Mathematica generated png in a latex doc, it always looks fuzzy, so I liked the pdf save format (at least when the size was smaller).

$\endgroup$
6
  • 1
    $\begingroup$ The main reason why such bloat happens is that too many (unnecessary or standard) fonts are embedded in the PDF. I bet that's happening here too. One thing that always fixes this is to do this with your plot: Export["foo.pdf",First[ImportString[ExportString[%,"PDF"],"PDF"]]]; However, that's not a real solution. Alternatively, you'll most likely be able to fix the size by opening the PDF in another application and saving it back to PDF from there. $\endgroup$
    – Jens
    Dec 7, 2012 at 18:22
  • $\begingroup$ I get a file of 3.5 MB, even larger. After opening it with Acrobat and looking at the properties, it seems that Mathematica 9 embeds the Times font too, while Mathematica 8 does not (I vaguely remember someone complaining that these fonts should be embedded for some situations... even though Times is a standard that any PDF reader should be able to substitute even if it is not embedded) $\endgroup$
    – Szabolcs
    Dec 7, 2012 at 18:30
  • $\begingroup$ Here's a comment on this: forums.wolfram.com/mathgroup/archive/2008/Nov/msg00091.html Even there the reply was that the base 14 fonts do not need to be embedded. Perhaps this is a bug. Could you write support and ask them about it? 3.5 MB is certainly much larger than reasonable for such a plot. $\endgroup$
    – Szabolcs
    Dec 7, 2012 at 18:35
  • 2
    $\begingroup$ Usually, PDF writers should embed only a subset of a font definition if only a small number of characters in that font is required. $\endgroup$
    – cormullion
    Dec 7, 2012 at 19:31
  • $\begingroup$ I was experiencing the same issue with Mathematica 9 and apparently the issue is gone in version 9.0.1. I didn't change the default settings. Now the plot size is O(10) KB, while it was O(1) MB with version 9. $\endgroup$ Feb 17, 2014 at 10:21

4 Answers 4

16
$\begingroup$

As others noted, the increase in file size is due to font embedding. There is a set of 14 standard fonts that every PDF reader should know about and should be able to handle. The usually do not need to embedded in PDFs.

It seems that previous versions of Mathematica did not embed these fonts by default, but version 9 does. There are two relevant Front End options controlling front embedding:

  • EmbedExternalFonts

  • EmbedStandardPostScriptFonts

You can find them in Format -> Option Inspector after selecting Global Preferences.

If you set the latter to False, standard fonts won't be embedded (the earlier behaviour). If you set the first to False, no font will be embedded.

Note that the PDF/A standard requires all fonts (including base 14 fonts) to be embedded (thanks @Verbeia for the comment).

Note: the problem affected me as well, so I contacted support about it. They mentioned the options, but I guessed about their precise meanings, so my guess might of course be wrong. They said the change in v9 was intentional as not embedding all fonts has caused problems in the past.

$\endgroup$
7
  • 3
    $\begingroup$ It's worth noting that the PDF/A standard requires even those standard fonts to be embedded. But it's possible they aren't being properly subsetted? $\endgroup$
    – Verbeia
    Dec 14, 2012 at 7:20
  • 4
    $\begingroup$ @Verbeia Mathematica doesn't (yet) support subsetting of fonts. I'm well aware of how useful this would be. $\endgroup$
    – John Fultz
    Feb 3, 2013 at 23:10
  • 2
    $\begingroup$ @Szabolcs For me, it doesn't seem to be doing so in 9.0.1, although I can reproduce the 9.0.0 behavior. My example, on Mac, export of Plot[Sin[x], {x, 0, 2Pi}, PlotLabel->Sin[x], BaseStyle -> {PrivateFontOptions -> {"OperatorSubstitution" -> False}}] was 3.5M in 9.0.0 and 6.3K in 9.0.1. I think this was just a bug, but I was unable to quickly find a bug report referencing this. $\endgroup$
    – John Fultz
    Feb 3, 2013 at 23:24
  • 1
    $\begingroup$ @JohnFultz Thanks for pointing this out, I'll update the answer later. Just to clear up the confusion and explain why I asked you about this: Support told me that "This change is intentional since there were a number of font issues occurring in PDFs in Mathematica 8." Also I did not notice that the behaviour changed in 9.0.1, as I did not reset my settings when installing 9.0.1. I thought turning off EmbedStandardPostScriptFonts may cause problems. $\endgroup$
    – Szabolcs
    Feb 4, 2013 at 0:00
  • 2
    $\begingroup$ I know this is not a solution within Mathematica, but for those not willing/able to upgrade to Mathematica 10 yet you can use the simple Linux tool ps2pdf, which accepts PDFs as inputs as well: ps2pdf -dEPSCrop figure.pdf figure-nofonts.pdf. This will remove unnecessary embedded fonts, greatly reducing filesize (the dEPSCrop saves the original bounding box). $\endgroup$
    – Guillochon
    Jul 28, 2014 at 18:27
7
$\begingroup$

This has been fixed in version 10.

Fonts are now correctly subsetted, even an OpenType font like Calluna, as shown here.

enter image description here

The graphic was produced with the following code, and the resulting file size is just 11kb.

test = Plot[{Sin[x], Cos[x]}, {x, 0, 5}, 
 BaseStyle -> {FontFamily -> "Calluna Sans", FontSize -> 16}]
Export["testcalluna.pdf", test]

The PDF export functionality was completely revamped in version 10, using the Cairo library.

enter image description here

$\endgroup$
3
  • $\begingroup$ I think the PDF exporter has been rewritten. Many PDF export bugs are fixed. $\endgroup$
    – Szabolcs
    Jul 11, 2014 at 22:57
  • $\begingroup$ @Szabolcs - I know for a fact it has. See my edit. This might have had something to do with user feedback. $\endgroup$
    – Verbeia
    Jul 11, 2014 at 22:59
  • 2
    $\begingroup$ This one, however, has not been fixed. Beware of saving graphics interactively. Use Export instead. $\endgroup$
    – Szabolcs
    Jul 11, 2014 at 23:02
1
$\begingroup$

When exported to EPS, your plot produces a much smaller file - about 85Kb. When converted to PDF (eg via Preview.app or by running /usr/bin/pstopdf) it gets even smaller - 19Kb. Some kind of batch/automatic conversion set-up might be worth developing if the sizes become significant.

$\endgroup$
3
  • 1
    $\begingroup$ But you lose some things when exporting to EPS, for example transparency. $\endgroup$
    – Szabolcs
    Dec 7, 2012 at 20:55
  • $\begingroup$ @Szabolcs Tangentially related: what does it look like when re-importing the EPS in version 9? Are the fonts messed up? They are on version 8. Anyway, I couldn't figure out a way from the documentation how to suppress including the fonts in the PDF - and that's probably what the question really requires. $\endgroup$
    – Jens
    Dec 7, 2012 at 21:08
  • $\begingroup$ @Jens The result is exactly the same in 9 as in 8. $\endgroup$
    – Szabolcs
    Dec 7, 2012 at 23:07
0
$\begingroup$

Select the figure, and use your AdobePDF to print it. The quality remains while the size is significantly reduced.

$\endgroup$
1
  • $\begingroup$ That's not a task that could easily be automated. $\endgroup$ Sep 10, 2013 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.