@@ -46,7 +46,7 @@ function eps2pdf(source, dest, crop, append, gray, quality, gs_options)
46
46
% Thank you to Scott for pointing out the subsampling of very small images,
47
47
% which was fixed for lossless compression settings.
48
48
49
- % 9 /12/2011 Pass font path to ghostscript.
49
+ % 09 /12/11: Pass font path to ghostscript
50
50
% 26/02/15: If temp dir is not writable, use the dest folder for temp
51
51
% destination files (Javier Paredes)
52
52
% 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)
56
56
% 04/10/15: Suggest a workaround for issue #41 (missing font path; thanks Mariia Fedotenkova)
57
57
% 22/02/16: Bug fix from latest release of this file (workaround for issue #41)
58
58
% 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)
59
60
60
61
% Intialise the options string for ghostscript
61
62
options = [' -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="' dest ' "' ];
@@ -99,7 +100,8 @@ function eps2pdf(source, dest, crop, append, gray, quality, gs_options)
99
100
% Check if the output file exists
100
101
if nargin > 3 && append && exist(dest , ' file' ) == 2
101
102
% File exists - append current figure to the end
102
- tmp_nam = tempname ;
103
+ tmp_nam = [tempname ' .pdf' ];
104
+ [fpath ,fname ,fext ] = fileparts(tmp_nam );
103
105
try
104
106
% Ensure that the temp dir is writable (Javier Paredes 26/2/15)
105
107
fid = fopen(tmp_nam ,' w' );
@@ -108,27 +110,34 @@ function eps2pdf(source, dest, crop, append, gray, quality, gs_options)
108
110
delete(tmp_nam );
109
111
catch
110
112
% Temp dir is not writable, so use the dest folder
111
- [dummy ,fname ,fext ] = fileparts(tmp_nam ); % #ok<ASGLU>
112
113
fpath = fileparts(dest );
113
114
tmp_nam = fullfile(fpath ,[fname fext ]);
114
115
end
115
- % Copy the file
116
+ % Copy the existing (dest) pdf file to temporary folder
116
117
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
119
126
try
120
127
% Convert to pdf using ghostscript
121
128
[status , message ] = ghostscript(options );
122
129
catch me
123
- % Delete the intermediate file
130
+ % Delete the intermediate files and rethrow the error
124
131
delete(tmp_nam );
132
+ delete(tmp_nam2 );
125
133
rethrow(me );
126
134
end
127
- % Delete the intermediate file
135
+ % Delete the intermediate (temporary) files
128
136
delete(tmp_nam );
137
+ delete(tmp_nam2 );
129
138
else
130
139
% 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
132
141
options = [options ' -f "' source ' "' ];
133
142
% Convert to pdf using ghostscript
134
143
[status , message ] = ghostscript(options );
0 commit comments