source: issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/append_pdfs.m@ 21757

Last change on this file since 21757 was 21757, checked in by Eric.Larour, 8 years ago

CHG: diverse

  • Property svn:executable set to *
File size: 2.7 KB
Line 
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% 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
37
38function append_pdfs(varargin)
39
40if nargin < 2, return; end % sanity check
41
42% Are we appending or creating a new file
43append = exist(varargin{1}, 'file') == 2;
44output = [tempname '.pdf'];
45try
46 % Ensure that the temp dir is writable (Javier Paredes 26/2/15)
47 fid = fopen(output,'w');
48 fwrite(fid,1);
49 fclose(fid);
50 delete(output);
51 isTempDirOk = true;
52catch
53 % Temp dir is not writable, so use the output folder
54 [dummy,fname,fext] = fileparts(output); %#ok<ASGLU>
55 fpath = fileparts(varargin{1});
56 output = fullfile(fpath,[fname fext]);
57 isTempDirOk = false;
58end
59if ~append
60 output = varargin{1};
61 varargin = varargin(2:end);
62end
63% Create the command file
64if isTempDirOk
65 cmdfile = [tempname '.txt'];
66else
67 cmdfile = fullfile(fpath,[fname '.txt']);
68end
69fh = fopen(cmdfile, 'w');
70fprintf(fh, '-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="%s" -f', output);
71fprintf(fh, ' "%s"', varargin{:});
72fclose(fh);
73% Call ghostscript
74ghostscript(['@"' cmdfile '"']);
75% Delete the command file
76delete(cmdfile);
77% Rename the file if needed
78if append
79 movefile(output, varargin{1});
80end
81end
Note: See TracBrowser for help on using the repository browser.