File Exchange

image thumbnail


version (4.55 KB) by Oliver Woodford
Append/concatenate multiple PDF files into a single file using Ghostscript.


Updated 08 Jul 2014

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:

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

This function is now included in the export_fig package:
The version here will not be maintained.

Comments and Ratings (40)

John B.

Very useful!! Thanks for your efforts.

Hi, I get the following error:
Error: /undefinedfilename in (figure1.pdf)
Operand stack:

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

Do you know how to solve it?

Thanks a lot!

Works great! Can't thank you enough.

Hello Oliver,
I get this warning: Path to ghostscript installation could not be saved. Enter it manually in ghostscript.txt.
I tried doing it manually but the text file is erased at every execution.

Can Agca

Like Christa (below), I am also getting a blank first page inserted into my combined pdf. Anyone have a solution to this problem?

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

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.


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?

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.



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/ no version information available (required by /lib64/
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?

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.

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?


So beautiful, thank you!


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.


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.

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{:});


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!


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!


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


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?


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!

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!

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.

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.

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

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


Update description as added to export_fig package

Added more syntax examples.

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

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

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

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: export_fig