Skip to content

Commit acfd348

Browse files
committed
Improved appending of multiple EPS files into single PDF (issue altmany#233; thanks @shartjen)
1 parent 4130d63 commit acfd348

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

eps2pdf.m

+18-9
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function eps2pdf(source, dest, crop, append, gray, quality, gs_options)
4646
% Thank you to Scott for pointing out the subsampling of very small images,
4747
% which was fixed for lossless compression settings.
4848

49-
% 9/12/2011 Pass font path to ghostscript.
49+
% 09/12/11: Pass font path to ghostscript
5050
% 26/02/15: If temp dir is not writable, use the dest folder for temp
5151
% destination files (Javier Paredes)
5252
% 28/02/15: Enable users to specify optional ghostscript options (issue #36)
@@ -56,6 +56,7 @@ function eps2pdf(source, dest, crop, append, gray, quality, gs_options)
5656
% 04/10/15: Suggest a workaround for issue #41 (missing font path; thanks Mariia Fedotenkova)
5757
% 22/02/16: Bug fix from latest release of this file (workaround for issue #41)
5858
% 20/03/17: Added informational message in case of GS croak (issue #186)
59+
% 16/01/18: Improved appending of multiple EPS files into single PDF (issue #233; thanks @shartjen)
5960

6061
% Intialise the options string for ghostscript
6162
options = ['-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="' dest '"'];
@@ -99,7 +100,8 @@ function eps2pdf(source, dest, crop, append, gray, quality, gs_options)
99100
% Check if the output file exists
100101
if nargin > 3 && append && exist(dest, 'file') == 2
101102
% File exists - append current figure to the end
102-
tmp_nam = tempname;
103+
tmp_nam = [tempname '.pdf'];
104+
[fpath,fname,fext] = fileparts(tmp_nam);
103105
try
104106
% Ensure that the temp dir is writable (Javier Paredes 26/2/15)
105107
fid = fopen(tmp_nam,'w');
@@ -108,27 +110,34 @@ function eps2pdf(source, dest, crop, append, gray, quality, gs_options)
108110
delete(tmp_nam);
109111
catch
110112
% Temp dir is not writable, so use the dest folder
111-
[dummy,fname,fext] = fileparts(tmp_nam); %#ok<ASGLU>
112113
fpath = fileparts(dest);
113114
tmp_nam = fullfile(fpath,[fname fext]);
114115
end
115-
% Copy the file
116+
% Copy the existing (dest) pdf file to temporary folder
116117
copyfile(dest, tmp_nam);
117-
% Add the output file names
118-
options = [options ' -f "' tmp_nam '" "' source '"'];
118+
% Produce an interim pdf of the source eps, rather than adding the eps directly (issue #233)
119+
ghostscript([options ' -f "' source '"']);
120+
[~,fname] = fileparts(tempname);
121+
tmp_nam2 = fullfile(fpath,[fname fext]); % ensure using a writable folder (not necessarily tempdir)
122+
copyfile(dest, tmp_nam2);
123+
% Add the existing pdf and interim pdf as inputs to ghostscript
124+
%options = [options ' -f "' tmp_nam '" "' source '"']; % append the source eps to dest pdf
125+
options = [options ' -f "' tmp_nam '" "' tmp_nam2 '"']; % append the interim pdf to dest pdf
119126
try
120127
% Convert to pdf using ghostscript
121128
[status, message] = ghostscript(options);
122129
catch me
123-
% Delete the intermediate file
130+
% Delete the intermediate files and rethrow the error
124131
delete(tmp_nam);
132+
delete(tmp_nam2);
125133
rethrow(me);
126134
end
127-
% Delete the intermediate file
135+
% Delete the intermediate (temporary) files
128136
delete(tmp_nam);
137+
delete(tmp_nam2);
129138
else
130139
% File doesn't exist or should be over-written
131-
% Add the output file names
140+
% Add the source eps file as input to ghostscript
132141
options = [options ' -f "' source '"'];
133142
% Convert to pdf using ghostscript
134143
[status, message] = ghostscript(options);

0 commit comments

Comments
 (0)