| 1 | %APPEND_PDFS Appends/concatenates multiple PDF files
|
|---|
| 2 | %
|
|---|
| 3 | % Example:
|
|---|
| 4 | % append_pdfs(output, input1, input2, ...)
|
|---|
| 5 | % append_pdfs(output, input_list{:})
|
|---|
| 6 | % append_pdfs test.pdf temp1.pdf temp2.pdf
|
|---|
| 7 | %
|
|---|
| 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.
|
|---|
| 10 | %
|
|---|
| 11 | % This function requires that you have ghostscript installed on your
|
|---|
| 12 | % system. Ghostscript can be downloaded from: http://www.ghostscript.com
|
|---|
| 13 | %
|
|---|
| 14 | % IN:
|
|---|
| 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.
|
|---|
| 21 |
|
|---|
| 22 | % Copyright: Oliver Woodford, 2011
|
|---|
| 23 |
|
|---|
| 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.
|
|---|
| 26 |
|
|---|
| 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.
|
|---|
| 29 |
|
|---|
| 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
|
|---|
| 33 | % setting
|
|---|
| 34 |
|
|---|
| 35 | function append_pdfs(varargin)
|
|---|
| 36 | % Are we appending or creating a new file
|
|---|
| 37 | append = exist(varargin{1}, 'file') == 2;
|
|---|
| 38 | if append
|
|---|
| 39 | output = [tempname '.pdf'];
|
|---|
| 40 | else
|
|---|
| 41 | output = varargin{1};
|
|---|
| 42 | varargin = varargin(2:end);
|
|---|
| 43 | 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{:});
|
|---|
| 49 | fclose(fh);
|
|---|
| 50 | % Call ghostscript
|
|---|
| 51 | ghostscript(['@"' cmdfile '"']);
|
|---|
| 52 | % Delete the command file
|
|---|
| 53 | delete(cmdfile);
|
|---|
| 54 | % Rename the file if needed
|
|---|
| 55 | if append
|
|---|
| 56 | movefile(output, varargin{1});
|
|---|
| 57 | end
|
|---|
| 58 | end
|
|---|