source: issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/eps2pdf.m@ 21243

Last change on this file since 21243 was 19993, checked in by seroussi, 9 years ago

BUG: reverting back to old export_fig (too many problems)

File size: 5.0 KB
Line 
1%EPS2PDF Convert an eps file to pdf format using ghostscript
2%
3% Examples:
4% eps2pdf source dest
5% eps2pdf(source, dest, crop)
6% eps2pdf(source, dest, crop, append)
7% eps2pdf(source, dest, crop, append, gray)
8% eps2pdf(source, dest, crop, append, gray, quality)
9%
10% This function converts an eps file to pdf format. The output can be
11% optionally cropped and also converted to grayscale. If the output pdf
12% file already exists then the eps file can optionally be appended as a new
13% page on the end of the eps file. The level of bitmap compression can also
14% optionally be set.
15%
16% This function requires that you have ghostscript installed on your
17% system. Ghostscript can be downloaded from: http://www.ghostscript.com
18%
19%IN:
20% source - filename of the source eps file to convert. The filename is
21% assumed to already have the extension ".eps".
22% dest - filename of the destination pdf file. The filename is assumed to
23% already have the extension ".pdf".
24% crop - boolean indicating whether to crop the borders off the pdf.
25% Default: true.
26% append - boolean indicating whether the eps should be appended to the
27% end of the pdf as a new page (if the pdf exists already).
28% Default: false.
29% gray - boolean indicating whether the output pdf should be grayscale or
30% not. Default: false.
31% quality - scalar indicating the level of image bitmap quality to
32% output. A larger value gives a higher quality. quality > 100
33% gives lossless output. Default: ghostscript prepress default.
34
35% Copyright (C) Oliver Woodford 2009-2011
36
37% Suggestion of appending pdf files provided by Matt C at:
38% http://www.mathworks.com/matlabcentral/fileexchange/23629
39
40% Thank you to Fabio Viola for pointing out compression artifacts, leading
41% to the quality setting.
42% Thank you to Scott for pointing out the subsampling of very small images,
43% which was fixed for lossless compression settings.
44
45% 9/12/2011 Pass font path to ghostscript.
46
47function eps2pdf(source, dest, crop, append, gray, quality)
48% Intialise the options string for ghostscript
49options = ['-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="' dest '"'];
50% Set crop option
51if nargin < 3 || crop
52 options = [options ' -dEPSCrop'];
53end
54% Set the font path
55fp = font_path();
56if ~isempty(fp)
57 options = [options ' -sFONTPATH="' fp '"'];
58end
59% Set the grayscale option
60if nargin > 4 && gray
61 options = [options ' -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray'];
62end
63% Set the bitmap quality
64if nargin > 5 && ~isempty(quality)
65 options = [options ' -dAutoFilterColorImages=false -dAutoFilterGrayImages=false'];
66 if quality > 100
67 options = [options ' -dColorImageFilter=/FlateEncode -dGrayImageFilter=/FlateEncode -c ".setpdfwrite << /ColorImageDownsampleThreshold 10 /GrayImageDownsampleThreshold 10 >> setdistillerparams"'];
68 else
69 options = [options ' -dColorImageFilter=/DCTEncode -dGrayImageFilter=/DCTEncode'];
70 v = 1 + (quality < 80);
71 quality = 1 - quality / 100;
72 s = sprintf('<< /QFactor %.2f /Blend 1 /HSample [%d 1 1 %d] /VSample [%d 1 1 %d] >>', quality, v, v, v, v);
73 options = sprintf('%s -c ".setpdfwrite << /ColorImageDict %s /GrayImageDict %s >> setdistillerparams"', options, s, s);
74 end
75end
76% Check if the output file exists
77if nargin > 3 && append && exist(dest, 'file') == 2
78 % File exists - append current figure to the end
79 tmp_nam = tempname;
80 % Copy the file
81 copyfile(dest, tmp_nam);
82 % Add the output file names
83 options = [options ' -f "' tmp_nam '" "' source '"'];
84 try
85 % Convert to pdf using ghostscript
86 [status, message] = ghostscript(options);
87 catch me
88 % Delete the intermediate file
89 delete(tmp_nam);
90 rethrow(me);
91 end
92 % Delete the intermediate file
93 delete(tmp_nam);
94else
95 % File doesn't exist or should be over-written
96 % Add the output file names
97 options = [options ' -f "' source '"'];
98 % Convert to pdf using ghostscript
99 [status, message] = ghostscript(options);
100end
101% Check for error
102if status
103 % Report error
104 if isempty(message)
105 error('Unable to generate pdf. Check destination directory is writable.');
106 else
107 error(message);
108 end
109end
110end
111
112% Function to return (and create, where necessary) the font path
113function fp = font_path()
114fp = user_string('gs_font_path');
115if ~isempty(fp)
116 return
117end
118% Create the path
119% Start with the default path
120fp = getenv('GS_FONTPATH');
121% Add on the typical directories for a given OS
122if ispc
123 if ~isempty(fp)
124 fp = [fp ';'];
125 end
126 fp = [fp getenv('WINDIR') filesep 'Fonts'];
127else
128 if ~isempty(fp)
129 fp = [fp ':'];
130 end
131 fp = [fp '/usr/share/fonts:/usr/local/share/fonts:/usr/share/fonts/X11:/usr/local/share/fonts/X11:/usr/share/fonts/truetype:/usr/local/share/fonts/truetype'];
132end
133user_string('gs_font_path', fp);
134end
Note: See TracBrowser for help on using the repository browser.