1 %EPS2PDF Convert an eps file to pdf format
using ghostscript
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)
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
16 % This
function requires that you have ghostscript installed on your
17 % system. Ghostscript can be downloaded from: http:
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.
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).
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.
35 % Copyright (C) Oliver Woodford 2009-2011
37 % Suggestion of appending pdf files provided by Matt C at:
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.
45 % 9/12/2011 Pass font path to ghostscript.
47 function eps2pdf(source, dest, crop, append, gray, quality)
48 % Intialise the options
string for ghostscript
49 options = ['-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="' dest '"'];
52 options = [options ' -dEPSCrop'];
57 options = [options ' -sFONTPATH="' fp '"'];
59 % Set the grayscale option
61 options = [options ' -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray'];
63 % Set the bitmap quality
64 if nargin > 5 && ~isempty(quality)
65 options = [options ' -dAutoFilterColorImages=false -dAutoFilterGrayImages=false'];
67 options = [options ' -dColorImageFilter=/FlateEncode -dGrayImageFilter=/FlateEncode -c ".setpdfwrite << /ColorImageDownsampleThreshold 10 /GrayImageDownsampleThreshold 10 >> setdistillerparams"'];
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);
76 % Check if the output file exists
77 if nargin > 3 && append && exist(dest, 'file') == 2
78 % File exists - append current figure to the end
81 copyfile(dest, tmp_nam);
82 % Add the output file names
83 options = [options ' -f "' tmp_nam '" "' source '"'];
85 % Convert to pdf using ghostscript
86 [status, message] = ghostscript(options);
88 % Delete the intermediate file
92 % Delete the intermediate file
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);
105 error('Unable to generate pdf. Check destination directory is writable.');
112 % Function to return (and create, where necessary) the font path
113 function fp = font_path()
114 fp = user_string('gs_font_path');
119 % Start with the default path
120 fp = getenv('GS_FONTPATH');
121 % Add on the typical directories for a given OS
126 fp = [fp getenv('WINDIR') filesep 'Fonts'];
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'];
133 user_string('gs_font_path', fp);