Export PDF: font not found

Tom de Geus Source

I am trying to export my figure as a PDF. I run into the problem that when I want to edit it (in this case by Graphic on macOS) that the font appears not to be found by the editor. My question is, how do I solve this? Can I 'install' the fonts used by matplotlib?


import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.plot([0,1], [0,1])


This PDF looks fine in Preview:

enter image description here

But in the editor, it gives gibberish:

enter image description here

Failing solution


matplotlib.rc("pdf", fonttype=42)

(see this answer).

Partly working solution

What works is to install all matplotlib's fonts. I have followed this answer to find all matplotlib's ttfs, and installed them. This works and solves the problem.

But... This does solve the issues when LaTeX is enabled, by

import matplotlib

matplotlib.rcParams['text.usetex'] = True

How do I install the fonts that matplotlib uses now?

A solution here is to export as SVG. However, for some reason this takes ages on my system (see this bug).



answered 2 months ago nahusznaj #1

What pdf editor are you using? I don't know how to check whether an attempt to solve this is working... but I always use this code to save images in pdf or png:

plt.savefig('test.pdf', edgecolor='none', bbox_inches="tight")

answered 2 months ago Tom de Geus #2

I have found a workaround. This is to convert all fonts to outlines before editing. One way to do this is with GhostScript with the option -dNoOutputFonts, as described in this answer:

gs -o file-with-outlines.pdf -dNoOutputFonts -sDEVICE=pdfwrite file.pdf

comments powered by Disqus