Merge PDF files using Ghostscript

© copyright 02.Jan.2009 by Paul Bradley filed under PDF


[Ad] need a break from coding?

Hoseasons Villas

I am in the process of developing a large document management system, where each document within the system is published to a PDF file for viewing and printing. One of the requirements of the system is that certain sub categories of documents need all the individual PDF files to be merged together into a logical sequence to create a master "overview" PDF file.

A quick search on Google lists a whole array of tools that can do this, but I needed something that could be run from the command line, so that my program can call this external utility to combine the PDF files in the order that I stipulate. I had used Ghostscript on a previous project to convert Postscript files to PDF files, but hadn't realised how easy was to merge PDF files using Ghostscript.

An example of merging 3 PDF files.

The example below will combine the files 1.pdf, 2.pdf and 3.pdf into a new PDF file called merged.pdf


gswin32 -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUT=merged.pdf
	-dBATCH 1.pdf 2.pdf 3.pdf

n.b. the code should be typed on one line, split here for presentation.

 


If you have found this article helpful or useful please consider linking to it, emailing it to friends, or share it with others using social sites like del.icio.us, Stumble Upon or Twitter.

Paul Bradley

About the Author
Paul Bradley is a VB.NET software developer living and working in Cumbria. He provides PHP & MySQL bespoke development services via his software development company, Carlisle Software Limited.
He has over 20 years programming experience.

Categories & Topics

Home · Apache · JavaScript · Perl · PDF · PHP · MySQL · MSSQL · TAR · Ubuntu Linux · Video · Visual Basic

Browse the complete article history, and if you like what you see; consider subscribing to the rss feed.