Save a figure to PDF or EPS with non-standard fonts

51 visualizzazioni (ultimi 30 giorni)
If I generate a figure using some fancy fonts, for example like this:
plot(rand(3));
set(gca, 'FontName', 'Georgia');
title('My graph', 'FontName', 'Impact');
then when I export the figure to PDF or EPS using print, e.g.:
print test.pdf -dpdf
the fonts are changed to Courier. This is because only a small number of fonts are supported when printing using the Postscript or Ghostscript drivers (e.g. when exporting to PDF or EPS files using the painters renderer).
How can I get round this limitation? Ideally I'd like a programmatic solution (i.e. one which doesn't require any user interaction).

Risposta accettata

Daniel Shub
Daniel Shub il 9 Dic 2011
On a Windows machine with a virtual pdf printer (in my case the one that comes with Adobe Acrobat Pro) I can print to a pdf file and the fonts get embedded.
I tried this a few years ago (before there was export_fig). The biggest problem I had, and the one that made me give up, was I couldn't bypass the print dialog box. I always had to provide confirmation. I think there may have also been small changes in the figure and in some cases sections of the figure were replaced with bitmaps.
EDIT:
I can get a little closer. On both Windows and Arch Linux I have a virtual PDF printer installed and named PDF Printer. On Windows it the Adobe Acrobat Pro (or maybe Adobe Distiller printer) on Linux it is the CUPS-PDF printer. On both systems
print '-PPDF Printer' myfilename.pdf
gives the desired result without any user interaction. I think it should be pretty OS independent and consistent across PDF printer type. Integrating it into export_fig may be harder. You will either need the user to supply the name of the PDF printer or a good guess as to what it is called. Guessing the printer name will probably be harder than guessing where Ghost Script is installed.
  4 Commenti
Oliver Woodford
Oliver Woodford il 10 Dic 2011
Interesting update. Thanks. One problem I see with incorporating this approach into export_fig is that you probably lose control over the compression level of bitmaps. There could be others.
Jan
Jan il 10 Dic 2011
You can select the printer using the -Pprinter flag of the PRINT command. There are PDF printers available, which do not need a dialog.

Accedi per commentare.

Più risposte (4)

Juan Guerrero
Juan Guerrero il 25 Apr 2017
Hi, I came up with a solution explained here: http://isa.uniovi.es/~guerrero/FontExample/FontExampleFiles.zip
There are pretty amazing solutions like export_fig but when you only want to obtain a pdf with correct fonts this is simpler. Another advantage is that you are aware of what you are doing, which is an advantage when the Matlab version changes.
Best regards.
  1 Commento
Michael G. Baker
Michael G. Baker il 13 Set 2018
Modificato: Michael G. Baker il 27 Nov 2018
This is a good work-around for something that Matlab should have fixed long ago.
On Mac El Capitan, I needed to add the "-dNOSAFER" flag to the GhostScript call to prevent GS from crashing due to file permissions (even though I had read/write for the specified file).

Accedi per commentare.


Owen Gebler
Owen Gebler il 6 Apr 2018
My solution to this problem is to use the open-source image editor Inkscape to convert an SVG plot produced by MATLAB to a PDF file. This can be automated from within a MATLAB script by calling Inkscape via a command prompt with the following command:
command = strcat('"C:\Program Files\Inkscape\inkscape.exe" ', {' '}, '"', fileToConvert,...
'.svg" --export-pdf="', fileToConvert, '.pdf" --export-pdf-version=1.5 --export-area-drawing');
command = string(command);
[ status, msg ] = system(command); % Run command prompt - return variable to suppress output text
Obviously this is Windows specific, and one should ensure Inkscape has been installed locally, and the path to it is accurate in the command.

Daniel Shub
Daniel Shub il 10 Dic 2011
When creating eps and pdf files I include the following check:
validFontNames = {'AvantGarde'; 'Bookman'; 'Courier'; 'Helvetica'; ...
'Helvetica-Narrow'; 'NewCenturySchoolBook'; 'Palatino'; ...
'Symbol'; 'Times'; 'ZapfChancery'; 'ZapfDingbats'};
htxt = findobj(hfig, '-depth', inf, '-property', 'FontName');
for itxt = 1:length(htxt)
errorMsg = ['The font "', get(htxt(itxt), 'FontName'), '" ',...
'is not supported for pdf output.'];
if ~any(strcmpi(get(htxt(itxt), 'FontName'), validFontNames))
warning(errorMsgId, errorMsg, '');
end
end
While not a solution, at least it warns me about the font substitution ...
  2 Commenti
Alan
Alan il 28 Set 2012
But findobj doesn't find objects whose HandleVisibility is off like xlabel and ylabel.
Daniel Shub
Daniel Shub il 28 Set 2012
You are right. I make the handles of all the children visible at the beginning of the function and return them to the original state at the end.

Accedi per commentare.


Oliver Woodford
Oliver Woodford il 12 Dic 2011
The file exchange submission export_fig now supports font correcting in EPS and PDF files for up to 11 non-standard fonts in a figure. If using croppping, there may be issues to do with corrected fonts being cropped slightly, where they are larger than the substituted font.
  1 Commento
Daniel Shub
Daniel Shub il 12 Dic 2011
If you show the code snippet you use to replace the fonts, I will vote for you. Then you can see if clean, but limited to 11 fonts, solution is more desirable than my hack with the PDF printer ...

Accedi per commentare.

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by