Home > data-vis > supporting_functions > export_fig > append_pdfs.m

append_pdfs

PURPOSE ^

APPEND_PDFS Appends/concatenates multiple PDF files

SYNOPSIS ^

function append_pdfs(varargin)

DESCRIPTION ^

APPEND_PDFS Appends/concatenates multiple PDF files

 Example:
   append_pdfs(output, input1, input2, ...)
   append_pdfs(output, input_list{:})
   append_pdfs test.pdf temp1.pdf temp2.pdf

 This function appends multiple PDF files to an existing PDF file, or
 concatenates them into a PDF file if the output file doesn't yet exist.

 This function requires that you have ghostscript installed on your
 system. Ghostscript can be downloaded from: http://www.ghostscript.com

 IN:
    output - string of output file name (including the extension, .pdf).
             If it exists it is appended to; if not, it is created.
    input1 - string of an input file name (including the extension, .pdf).
             All input files are appended in order.
    input_list - cell array list of input file name strings. All input
                 files are appended in order.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 %APPEND_PDFS Appends/concatenates multiple PDF files
0002 %
0003 % Example:
0004 %   append_pdfs(output, input1, input2, ...)
0005 %   append_pdfs(output, input_list{:})
0006 %   append_pdfs test.pdf temp1.pdf temp2.pdf
0007 %
0008 % This function appends multiple PDF files to an existing PDF file, or
0009 % concatenates them into a PDF file if the output file doesn't yet exist.
0010 %
0011 % This function requires that you have ghostscript installed on your
0012 % system. Ghostscript can be downloaded from: http://www.ghostscript.com
0013 %
0014 % IN:
0015 %    output - string of output file name (including the extension, .pdf).
0016 %             If it exists it is appended to; if not, it is created.
0017 %    input1 - string of an input file name (including the extension, .pdf).
0018 %             All input files are appended in order.
0019 %    input_list - cell array list of input file name strings. All input
0020 %                 files are appended in order.
0021 
0022 % Copyright: Oliver Woodford, 2011
0023 
0024 % Thanks to Reinhard Knoll for pointing out that appending multiple pdfs in
0025 % one go is much faster than appending them one at a time.
0026 
0027 % Thanks to Michael Teo for reporting the issue of a too long command line.
0028 % Issue resolved on 5/5/2011, by passing gs a command file.
0029 
0030 % Thanks to Martin Wittmann for pointing out the quality issue when
0031 % appending multiple bitmaps.
0032 % Issue resolved (to best of my ability) 1/6/2011, using the prepress
0033 % setting
0034 
0035 % 26/02/15: If temp dir is not writable, use the output folder for temp
0036 %           files when appending (Javier Paredes); sanity check of inputs
0037 
0038 function append_pdfs(varargin)
0039 
0040 if nargin < 2,  return;  end  % sanity check
0041 
0042 % Are we appending or creating a new file
0043 append = exist(varargin{1}, 'file') == 2;
0044 output = [tempname '.pdf'];
0045 try
0046     % Ensure that the temp dir is writable (Javier Paredes 26/2/15)
0047     fid = fopen(output,'w');
0048     fwrite(fid,1);
0049     fclose(fid);
0050     delete(output);
0051     isTempDirOk = true;
0052 catch
0053     % Temp dir is not writable, so use the output folder
0054     [dummy,fname,fext] = fileparts(output); %#ok<ASGLU>
0055     fpath = fileparts(varargin{1});
0056     output = fullfile(fpath,[fname fext]);
0057     isTempDirOk = false;
0058 end
0059 if ~append
0060     output = varargin{1};
0061     varargin = varargin(2:end);
0062 end
0063 % Create the command file
0064 if isTempDirOk
0065     cmdfile = [tempname '.txt'];
0066 else
0067     cmdfile = fullfile(fpath,[fname '.txt']);
0068 end
0069 fh = fopen(cmdfile, 'w');
0070 fprintf(fh, '-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="%s" -f', output);
0071 fprintf(fh, ' "%s"', varargin{:});
0072 fclose(fh);
0073 % Call ghostscript
0074 ghostscript(['@"' cmdfile '"']);
0075 % Delete the command file
0076 delete(cmdfile);
0077 % Rename the file if needed
0078 if append
0079     movefile(output, varargin{1});
0080 end
0081 end

Generated on Wed 24-May-2017 00:00:56 by m2html © 2005