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

조회 수: 49 (최근 30일)
Oliver Woodford
Oliver Woodford 2011년 12월 9일
편집: Michael G. Baker 2018년 11월 27일
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).

채택된 답변

Daniel Shub
Daniel Shub 2011년 12월 9일
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
Oliver Woodford
Oliver Woodford 2011년 12월 10일
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 2011년 12월 10일
You can select the printer using the -Pprinter flag of the PRINT command. There are PDF printers available, which do not need a dialog.

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Juan Guerrero
Juan Guerrero 2017년 4월 25일
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
Michael G. Baker
Michael G. Baker 2018년 9월 13일
편집: Michael G. Baker 2018년 11월 27일
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).

댓글을 달려면 로그인하십시오.


Owen Gebler
Owen Gebler 2018년 4월 6일
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 2011년 12월 10일
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
Alan
Alan 2012년 9월 28일
But findobj doesn't find objects whose HandleVisibility is off like xlabel and ylabel.
Daniel Shub
Daniel Shub 2012년 9월 28일
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.

댓글을 달려면 로그인하십시오.


Oliver Woodford
Oliver Woodford 2011년 12월 12일
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
Daniel Shub
Daniel Shub 2011년 12월 12일
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 ...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by