[18904] | 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 |
|
---|
[21341] | 35 | % 26/02/15: If temp dir is not writable, use the output folder for temp
|
---|
| 36 | % files when appending (Javier Paredes); sanity check of inputs
|
---|
[24686] | 37 | % 24/01/18: Fixed error in case of existing output file (append mode)
|
---|
| 38 | % 24/01/18: Fixed issue #213: non-ASCII characters in folder names on Windows
|
---|
| 39 | % 06/12/18: Avoid an "invalid escape-char" warning upon error
|
---|
[21341] | 40 |
|
---|
[18904] | 41 | function append_pdfs(varargin)
|
---|
[21341] | 42 |
|
---|
[24686] | 43 | if nargin < 2, return; end % sanity check
|
---|
[21341] | 44 |
|
---|
[24686] | 45 | % Are we appending or creating a new file
|
---|
| 46 | append = exist(varargin{1}, 'file') == 2;
|
---|
| 47 | output = [tempname '.pdf'];
|
---|
| 48 | try
|
---|
| 49 | % Ensure that the temp dir is writable (Javier Paredes 26/2/15)
|
---|
| 50 | fid = fopen(output,'w');
|
---|
| 51 | fwrite(fid,1);
|
---|
| 52 | fclose(fid);
|
---|
| 53 | delete(output);
|
---|
| 54 | isTempDirOk = true;
|
---|
| 55 | catch
|
---|
| 56 | % Temp dir is not writable, so use the output folder
|
---|
| 57 | [dummy,fname,fext] = fileparts(output); %#ok<ASGLU>
|
---|
| 58 | fpath = fileparts(varargin{1});
|
---|
| 59 | output = fullfile(fpath,[fname fext]);
|
---|
| 60 | isTempDirOk = false;
|
---|
| 61 | end
|
---|
| 62 | if ~append
|
---|
| 63 | output = varargin{1};
|
---|
| 64 | varargin = varargin(2:end);
|
---|
| 65 | end
|
---|
| 66 |
|
---|
| 67 | % Create the command file
|
---|
| 68 | if isTempDirOk
|
---|
| 69 | cmdfile = [tempname '.txt'];
|
---|
| 70 | else
|
---|
| 71 | cmdfile = fullfile(fpath,[fname '.txt']);
|
---|
| 72 | end
|
---|
| 73 | prepareCmdFile(cmdfile, output, varargin{:});
|
---|
| 74 |
|
---|
| 75 | % Call ghostscript
|
---|
| 76 | [status, errMsg] = ghostscript(['@"' cmdfile '"']);
|
---|
| 77 |
|
---|
| 78 | % Check for ghostscript execution errors
|
---|
| 79 | if status && ~isempty(strfind(errMsg,'undefinedfile')) && ispc %#ok<STREMP>
|
---|
| 80 | % Fix issue #213: non-ASCII characters in folder names on Windows
|
---|
| 81 | for fileIdx = 2 : numel(varargin)
|
---|
| 82 | [fpath,fname,fext] = fileparts(varargin{fileIdx});
|
---|
| 83 | varargin{fileIdx} = fullfile(normalizePath(fpath),[fname fext]);
|
---|
| 84 | end
|
---|
| 85 | % Rerun ghostscript with the normalized folder names
|
---|
| 86 | prepareCmdFile(cmdfile, output, varargin{:});
|
---|
| 87 | [status, errMsg] = ghostscript(['@"' cmdfile '"']);
|
---|
| 88 | end
|
---|
| 89 |
|
---|
| 90 | % Delete the command file
|
---|
| 91 | delete(cmdfile);
|
---|
| 92 |
|
---|
| 93 | % Check for ghostscript execution errors
|
---|
| 94 | if status
|
---|
| 95 | errMsg = strrep(errMsg,'\','\\'); % Avoid an "invalid escape-char" warning
|
---|
| 96 | error('YMA:export_fig:append_pdf',errMsg);
|
---|
| 97 | end
|
---|
| 98 |
|
---|
| 99 | % Rename the file if needed
|
---|
| 100 | if append
|
---|
| 101 | movefile(output, varargin{1}, 'f');
|
---|
| 102 | end
|
---|
[21341] | 103 | end
|
---|
[24686] | 104 |
|
---|
| 105 | % Prepare a text file with ghostscript directives
|
---|
| 106 | function prepareCmdFile(cmdfile, output, varargin)
|
---|
| 107 | fh = fopen(cmdfile, 'w');
|
---|
| 108 | fprintf(fh, '-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="%s" -f', output);
|
---|
| 109 | fprintf(fh, ' "%s"', varargin{:});
|
---|
| 110 | fclose(fh);
|
---|
[18904] | 111 | end
|
---|
[24686] | 112 |
|
---|
| 113 | % Convert long/non-ASCII folder names into their short ASCII equivalents
|
---|
| 114 | function pathStr = normalizePath(pathStr)
|
---|
| 115 | [fpath,fname,fext] = fileparts(pathStr);
|
---|
| 116 | if isempty(fpath) || strcmpi(fpath,pathStr), return, end
|
---|
| 117 | dirOutput = evalc(['system(''dir /X /AD "' pathStr '*"'')']);
|
---|
| 118 | shortName = strtrim(regexprep(dirOutput,{'.*> *',[fname fext '.*']},''));
|
---|
| 119 | if isempty(shortName)
|
---|
| 120 | shortName = [fname fext];
|
---|
| 121 | end
|
---|
| 122 | fpath = normalizePath(fpath); %recursive until entire fpath is processed
|
---|
| 123 | pathStr = fullfile(fpath, shortName);
|
---|
[21341] | 124 | end
|
---|