How to embed fonts when exporting Mathematica graphics as PDF

Upon exporting a Mathematica graphics expression as a PDF, Mathematica does not seem to embed any nonstandard fonts used. When you open the resulting PDF document with a PDF viewer, all the text items are rendered with standard fonts instead.

Is there a way to make Mathematica embed nonstandard fonts?

Example with Mathematica 8.0.4 under Mac OS X 10.7:

g = Graphics[{ Circle[],
Text["Sample",
BaseStyle -> {FontFamily -> "Papyrus", FontWeight -> "Bold",
FontSize -> 24}]}, ImageSize -> Tiny]
f = Export["~/Desktop/Graphics.PDF", g, "PDF"]


The resulting PDF does not use the correct font.

-
Can you give a code example, and instructions for checking that the fonts are embedded? I tried Export["testpdf.pdf", Graphics@Text[Style["asd", FontFamily -> "Candara", FontSize -> 50]]] and Adobe Reader reports that the font is embedded. I used CTRL-D in Adobe Reader, and clicked the Fonts tab. It says, "Candara (Embedded)". –  Szabolcs Jan 24 '12 at 20:26
See updated question. –  sakra Jan 24 '12 at 21:07
Sakra, what font version of Papyrus and what format is it? You can check this in Fontbook. I think it matters. –  Verbeia Jan 26 '12 at 19:01
Font Book.app says Version 6.1d10e2. –  sakra Jan 26 '12 at 19:31

What Verbeia said in her answer is not entirely correct — Mathematica indeed does embed the font, regardless of whether a particular font weight/slant exists or not. The real culprits are the PDF viewers on Macs, which do not use the base font if the specified weight is not available. It took some digging around to get to the reason though. The clues that led to my reasoning are as follows:

1. Mathematica knows what font it used when you re-import
This was the first clue. Executing Import["~/Desktop/Graphics.PDF"] will correctly display the graphics in Papyrus font, albeit without the bold option (which, as Verbeia noted, doesn't exist in all fonts). So the information had to be in the file somewhere.

2. The binary file shows Papyrus embedded in it
Opening the pdf in vim under hex mode, you find the font info embedded in it. What you see in the readable text on the right is the full copyright info, which they'd have to include only if it were embedded.

3. It opens correctly on linux systems
Papyrus is not a font that comes installed by default with linux systems. The above file opens with the correct font (but not weight) on RHEL 6 and Ubuntu 11.10, which can only mean that the font is embedded in the pdf. Below is a screenshot in RHEL 6, with the same file opened in Adobe reader (left) and evince (right).

Below is a screenshot of the same file opened in Adobe reader (left) and Preview.app (right). You can see that Adobe sure does know which font to use.

So you can see here that the problem is most likely with the PDF viewers on macs (other than Adobe reader). My guess is that they don't stay true to Adobe's specifications, and instead rely on OSX's Quartz framework for rendering the file, which is why you don't see the right font.

Either that, or they don't like the fact that Mathematica uses PDF v1.4 to export its files. v1.4 is pretty old (2001), and it is worth mentioning (since you're embedding non-standard fonts) that PDF v1.4 does not support embedding "Open Type" fonts. That was introduced only in v1.6, and if you're working with such fonts, you might want to keep this in mind.

-
Thanks for digging that out. I only use Preview.app for PDF viewing under Mac OS X. –  sakra Jan 25 '12 at 18:33
@R.M Aren't you saying, though, that Adobe Reader doesn't so much 'use the correct font' as 'guess correctly what Mathematica got up to when it created the file', and then cover for it? Technically Mathematica specified a font that didn't exist, and just drew a black line around the font to make it look bold. But Preview refused to make a font up this way, and defaulted to something it did have. If you ask for Papyrus Italic, Mathematica can do that too... Perhaps Preview should loosen up and play a little bit more! –  cormullion Jan 25 '12 at 20:17
@R.M this answer is incorrect and misleading. The presence of the font name in the PDF file does not mean that the font itself is embedded in the file, which was the OP's question. Adobe Reader and Mathematica display the re-imported graphic correctly because you have the font installed. If you displayed the graphic on a computer that did not have the font installed, the bold version would not display but the normal weight one would. (Though strangely, now that I check this on my Mac it does embed, even though similar fonts on Windows don't. The format of the font might matter as well.) –  Verbeia Jan 26 '12 at 13:10
@Verbeia I don't follow your comment... you start out saying it isn't embedded, but ultimately agree with me in saying that it is indeed embedded. Which one is it? It is incorrect to say that the font is not embedded just because it doesn't have a bold option. As I said in my answer (but didn't make it explicit) I tested it on ubuntu 11.10 and it opens with the correct font. Papyrus is a font that is shipped only with Windows and OS X, not linux distros, and this can only mean that the font was embedded. –  rm -rf Jan 26 '12 at 16:51
Ok I have removed the downvote. But I think that both answers contain part of the truth. There clearly are fonts that don't embed properly, even if they exist, or this question would not have come up. And even though Papyrus Bold works in some PDF viewers, it is fair to say that font weights that don't exist can't be relied on. It is not clear to me that Preview's behaviour is incorrect. There seems to be a subtle relationship between font format and how it exports, which needs further investigation. –  Verbeia Jan 26 '12 at 22:13

Mathematica will normally embed the fonts correctly in a PDF. But this only works if the font exists. Papyrus does not have a bold weight. Try with FontWeight->"Bold" deleted and see if this works.

I don't have Papyrus on my Windows machine, but I checked that Kristen ITC does not embed if it is bold, and does if the FontWeight option is removed.

-
By the way @sakra, thank you for this question. I didn't actually know the answer to this, and your question made me find it out. Very useful. –  Verbeia Jan 24 '12 at 22:06
+1 that is interesting that it doesn't embed if the font weight for that font isn't available. –  Mike Honeychurch Jan 24 '12 at 22:49
In Mathematica 9, (rm -rf)'s comment that fonts with the extension .otf (aka OpenType) can't be embedded still holds. In addition, I've also noticed that not all fonts with the extension .ttf can be embedded.