Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Merge multiple PDFs using Ghostscript

A simple Ghostscript command to merge two PDFs in a single file is shown below:

gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=combine.pdf -dBATCH 1.pdf 2.pdf

Install Ghostscript:

Type the command sudo apt-get install ghostscript to download and install the ghostscript package and all of the packages it depends on.

@oldi

This comment has been minimized.

Copy link

@oldi oldi commented Apr 1, 2019

Many thanks! You saved my day :) 👍

@carpediemmlf

This comment has been minimized.

Copy link

@carpediemmlf carpediemmlf commented Aug 15, 2019

many thx

@Lydnasty

This comment has been minimized.

Copy link

@Lydnasty Lydnasty commented Mar 11, 2020

Thank you

@kepatopoc

This comment has been minimized.

Copy link

@kepatopoc kepatopoc commented May 17, 2020

thx!

@Abhishek-Deshmukh

This comment has been minimized.

Copy link

@Abhishek-Deshmukh Abhishek-Deshmukh commented Nov 22, 2020

Thanks!

@oliverlambson

This comment has been minimized.

Copy link

@oliverlambson oliverlambson commented Jan 4, 2021

I added this to a function in my .zshrc (or .bash_profile for *nix) for easier use.

pdfcombine ()
{
   gs -q -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=${1%.*}___${2%.*}.pdf -dBATCH $1 $2
}

Then you can just run

pdfcombine file1.pdf file2.pdf

Which will output the combined pdfs to a file called file1___file2.pdf

Inspired by this gist which is using ghostscript to compress a pdf.

@ageek

This comment has been minimized.

Copy link

@ageek ageek commented Jan 11, 2021

Works perfectly on Cygwin @Windows 10 64 bit . Thanks !

@mrwm

This comment has been minimized.

Copy link

@mrwm mrwm commented Mar 2, 2021

Is there a way to merge them together to make a double spread pdf? something like this, but with ghostscript?

@alexisdal

This comment has been minimized.

Copy link

@alexisdal alexisdal commented May 27, 2021

didn't work for me. silly errors in the output that i did not understand.

but pdftk free did work for me => https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
beautifully simple and fast command line. might help someone else

pdftk.exe input_*.pdf output combined.pdf

@madurapa

This comment has been minimized.

Copy link

@madurapa madurapa commented Sep 17, 2021

Thanks

@IllustratedMan-code

This comment has been minimized.

Copy link

@IllustratedMan-code IllustratedMan-code commented Nov 9, 2021

I slightly modified @oliverlambson 's function to accept n arguments, where the first argument is the name of the resulting pdf.
pdfcombine () { gs -q -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=${1%.*}.pdf -dBATCH ${@:2} }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment