File Exchange

image thumbnail

append_pdfs

version 1.6 (4.55 KB) by

Append/concatenate multiple PDF files into a single file using Ghostscript.

86 Downloads

Updated

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

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 can be much faster than appending PDFs one at at time.
This function requires that you have ghostscript installed on your system. Ghostscript can be downloaded from: http://www.ghostscript.com

It also includes code from the export_fig package to interface with Ghostcript.

This function is now included in the export_fig package:
http://www.mathworks.com/matlabcentral/fileexchange/23629
The version here will not be maintained.

Comments and Ratings (31)

Long Cheng

Anise Ahmed

I get the following error:
>> append_pdfs(pdf_filename, list{:})
Error: /undefinedfilename in (river_flow.pdf)
Operand stack:

Execution stack:
   %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push
Dictionary stack:
   --dict:1202/1684(ro)(G)-- --dict:0/20(G)-- --dict:78/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
GPL Ghostscript 9.20: Unrecoverable error, exit code 1

Oliver Woodford

SCADA Miner: Please note that this version is not maintained. I suggest you use the version in export_fig, and if that doesn't work for you, raise an issue on GitHub.

SCADA Miner

Hi Oliver. Another excellent function. I have a problem though - I am creating multi page pdf reports with a logo (.png) in the top right hand corner. On the front page everyting is fine, but on all subsequent pages (all those which were actually appended to the first) the logo is distorted. I was previously using Bullzip pdf printer which was appending fine. I would prefer to keep the pdf printing internal to matlab because it is easier to control output filenames, locations etc. Any suggestions?

Shijin Thomas

Hi Oliver,
When i run the function it is showing an error
Error: /undefinedfilename in (Location S2 20 km Brake Data B3_0)
Operand stack:

Execution stack:
   %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push
Dictionary stack:
   --dict:1191/1684(ro)(G)-- --dict:0/20(G)-- --dict:77/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
GPL Ghostscript 9.15: Unrecoverable error, exit code 1
can you please help on it.

David

David (view profile)

Dehuan

Dehuan (view profile)

I modified this script to skip non-existant files instead of error and exit. I'm working on automated report generation and source PDFs are not guaranteed to be successfully generated so this feature is useful.

Elad Assia

Hi Oliver,
The function does not wotk for me...
I'm using a computer that is connected to a network and i Don't know whether the Matlab is installed on this computer or another one on the netwotk.
Every time I'm using the function, it creates a pdf file with the name I want but the file is empty. Every time I get this message: "gs: /usr/local/MATLAB/R2013a/bin/glnxa64/libtiff.so.5: no version information available (required by /lib64/libgs.so.9)
Error: /undefinedfilename in (out1)
Operand stack:

Execution stack:
   %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push
Dictionary stack:
   --dict:1175/1684(ro)(G)-- --dict:0/20(G)-- --dict:77/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
GPL Ghostscript 9.14: Unrecoverable error, exit code 1"

Do you know how to solve this problem?
Thanks!

Oliver Woodford

Andrea: You can't (unless you edit the code, of course). The solution is to delete the file beforehand if you want to overwrite it.

Andrea Rosso

This piece of software is awesome. Yet just a tiny question: how can I pass the -f option in order to overwrite already existing files?

Laura

Laura (view profile)

So beautiful, thank you!

Adam

Adam (view profile)

This is a great tool and works very nicely!

Does anyone have any examples where they generate Bookmarks as well? It seems like a relatively easy process. Below is a website that looks to be very useful, so I may try it myself.

http://blog.tremily.us/posts/PDF_bookmarks_with_Ghostscript/

Corey

Corey (view profile)

Hi Oliver,
Thank you for your response.
I am indeed using the functional form of the command. Apparently this is a limitation with using GhostScript in general. Was just curious if there was a sneaky workaround for paths that get too long.
http://ghostscript.com/doc/current/Language.htm

Oliver Woodford

Christa: To save to a particular folder, just specify the full or relative path in the output filename. I haven't seen the blank first page, so can't help there.

Corey: I haven't seen the 128 character limit problem, so can't really help. However, are you using the functional form of the command? E.g.:
output = 'test.pdf';
inputs = {'long_name1.pdf', 'long_name2.pdf'};
append_pdfs(output, inputs{:});

Corey

Corey (view profile)

This is great. I was wondering if you knew a good way to get around the 128 character limit on filename sizes? When you are appending to too many subfolders, the path can get too long, and cause the function to fail. Of course, one could reduce the length of the path, but if there is a sneaky way to get around this 128 character limit, it would be appreciated!

Christa

Thanks for sharing this! It is very helpful. I have a couple questions though. I'd like to save the combined pdf into a specific folder, not just the general matlab folder that I'm working in. Also, it is outputting a blank white sheet as the first page of my pdf. Any idea how to correct either of these issues? Thanks in advance!

Courtney

Bitmap/vector problem appears to have resolved itself. Disregard my question. Thanks!

Courtney

Hi Oliver,
Thanks for your suite of image exporting functions. I'm having an issue with complex (large, ~15MB) vector-rendered pdfs being transformed to bitmaps when combined, and getting pixelated. Any way around this?

Claudia

Ben Noffke

Thank you very much! You were right. I created a cell array and now it works.

I apologize for the rate.
This was my first post on mathworks. When I clicked the "submit" button nothing happend so I thought I have to rate. Without thinking (and without reading) I rated 4 stars. Than I realised that I just had to reload the page to see my post.
Sorry for that.

Thank you for this tool!

Oliver Woodford

Christoph: This an issue with your use of MATLAB syntax, rather than anything else. Make a cell array, string1, of the input file names, then call
   append_pdfs('output.pdf', string1{:});
It's not clear why gave the function 4 stars. Any suggestions?

It works when I write output, input1, input2 inside '' with .pdf extensions <== true.
+ I can't make it work if I stock all inputs during a loop in a string (the string includes the apostrophes and commas)
e.g. string1='input1','input2'
append_pdfs('output',string1) doesn't work.
Anyone an idea how to make it work?
Thx a lot!

Hakan Çakan

Oliver,
Thank you very much. I solved it but in the help section it is written that:

% Example:
% append_pdfs(output, input1, input2, ...)

It works when I write output, input1, input2 inside '' with .pdf extensions. It would be beter if you update help file accordingly.

Thanks again.

Oliver Woodford

Michael: Fix submitted. Thanks for reporting the problem.

Michael Teo

I was trying to appends 200+pdfs to 1 pdf. It resulted in "The command line is too long." message.

Pls advise, thanks.

Oliver Woodford

Thanks, David. It'll be in the next version.

Regarding ghostscript.m, in Linux /usr/bin/gs is the more likely path for gs.

Updates

1.6

Update description as added to export_fig package

1.5

Added more syntax examples.

1.3

Improve quality of bitmaps. Thanks to Martin Wittmann for pointing out the issue.

1.2

Fixed bug affecting long commands. Thanks to Michael Teo for reporting it.

1.1

Bug fix to ghostscript.m mostly affecting Linux (thanks to Paul Durack for reporting it).

MATLAB Release
MATLAB 7.11 (R2010b)
Acknowledgements

Inspired by: export_fig

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video