1 %APPEND_PDFS Appends/concatenates multiple PDF files
4 % append_pdfs(output, input1, input2, ...)
5 % append_pdfs(output, input_list{:})
6 % append_pdfs test.pdf temp1.pdf temp2.pdf
8 % This
function appends multiple PDF files to an existing PDF file, or
9 % concatenates them into a PDF file
if the output file doesn
't yet exist. 11 % This function requires that you have ghostscript installed on your 12 % system. Ghostscript can be downloaded from: http://www.ghostscript.com 15 % output - string of output file name (including the extension, .pdf). 16 % If it exists it is appended to; if not, it is created. 17 % input1 - string of an input file name (including the extension, .pdf). 18 % All input files are appended in order. 19 % input_list - cell array list of input file name strings. All input 20 % files are appended in order. 22 % Copyright: Oliver Woodford, 2011 24 % Thanks to Reinhard Knoll for pointing out that appending multiple pdfs in 25 % one go is much faster than appending them one at a time. 27 % Thanks to Michael Teo for reporting the issue of a too long command line. 28 % Issue resolved on 5/5/2011, by passing gs a command file. 30 % Thanks to Martin Wittmann for pointing out the quality issue when 31 % appending multiple bitmaps. 32 % Issue resolved (to best of my ability) 1/6/2011, using the prepress 35 function append_pdfs(varargin) 36 % Are we appending or creating a new file 37 append = exist(varargin{1}, 'file
') == 2; 39 output = [tempname '.pdf
']; 42 varargin = varargin(2:end); 44 % Create the command file 45 cmdfile = [tempname '.txt
']; 46 fh = fopen(cmdfile, 'w
'); 47 fprintf(fh, '-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=
"%s" -f
', output); 48 fprintf(fh, ' "%s"', varargin{:}); 51 ghostscript(['@"' cmdfile '"']); 52 % Delete the command file 54 % Rename the file if needed 56 movefile(output, varargin{1});