Robochameleon  v1.0
eps2pdf.m
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 
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 '"'];
50 % Set crop option
51 if nargin < 3 || crop
52  options = [options ' -dEPSCrop'];
53 end
54 % Set the font path
55 fp = font_path();
56 if ~isempty(fp)
57  options = [options ' -sFONTPATH="' fp '"'];
58 end
59 % Set the grayscale option
60 if nargin > 4 && gray
61  options = [options ' -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray'];
62 end
63 % Set the bitmap quality
64 if 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
75 end
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
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);
94 else
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);
100 end
101 % Check for error
102 if 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
109 end
110 return
111 
112 % Function to return (and create, where necessary) the font path
113 function fp = font_path()
114 fp = user_string('gs_font_path');
115 if ~isempty(fp)
116  return
117 end
118 % Create the path
119 % Start with the default path
120 fp = getenv('GS_FONTPATH');
121 % Add on the typical directories for a given OS
122 if ispc
123  if ~isempty(fp)
124  fp = [fp ';'];
125  end
126  fp = [fp getenv('WINDIR') filesep 'Fonts'];
127 else
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'];
132 end
133 user_string('gs_font_path', fp);
134 return