Robochameleon  v1.0
pdf2eps.m
1 %PDF2EPS Convert a pdf file to eps format using pdftops
2 %
3 % Examples:
4 % pdf2eps source dest
5 %
6 % This function converts a pdf file to eps format.
7 %
8 % This function requires that you have pdftops, from the Xpdf suite of
9 % functions, installed on your system. This can be downloaded from:
10 % http://www.foolabs.com/xpdf
11 %
12 %IN:
13 % source - filename of the source pdf file to convert. The filename is
14 % assumed to already have the extension ".pdf".
15 % dest - filename of the destination eps file. The filename is assumed to
16 % already have the extension ".eps".
17 
18 % Copyright (C) Oliver Woodford 2009-2010
19 
20 % Thanks to Aldebaro Klautau for reporting a bug when saving to
21 % non-existant directories.
22 
23 function pdf2eps(source, dest)
24 % Construct the options string for pdftops
25 options = ['-q -paper match -eps -level2 "' source '" "' dest '"'];
26 % Convert to eps using pdftops
27 [status, message] = pdftops(options);
28 % Check for error
29 if status
30  % Report error
31  if isempty(message)
32  error('Unable to generate eps. Check destination directory is writable.');
33  else
34  error(message);
35  end
36 end
37 % Fix the DSC error created by pdftops
38 fid = fopen(dest, 'r+');
39 if fid == -1
40  % Cannot open the file
41  return
42 end
43 fgetl(fid); % Get the first line
44 str = fgetl(fid); % Get the second line
45 if strcmp(str(1:min(13, end)), '% Produced by')
46  fseek(fid, -numel(str)-1, 'cof');
47  fwrite(fid, '%'); % Turn ' ' into '%'
48 end
49 fclose(fid);
50 return
51