Skip to content

Instantly share code, notes, and snippets.

@brenopolanski
Last active May 2, 2024 06:56
Show Gist options
  • Save brenopolanski/2ae13095ed7e865b60f5 to your computer and use it in GitHub Desktop.
Save brenopolanski/2ae13095ed7e865b60f5 to your computer and use it in GitHub Desktop.
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.

@ageek
Copy link

ageek commented Jan 11, 2021

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

@mrwm
Copy link

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
Copy link

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
Copy link

Thanks

@IllustratedMan-code
Copy link

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} }

@guilhemferr
Copy link

Added a check. Found it handy to have in .zshrc.
Usage will be eg. pdfmerge mergerd.pdf in1.pdf in2.pdf

function pdfmerge() {
  if [[ $# -lt 2 ]]; then
    echo "usage: $0 <out.pdf> <in1.pdf> <in2.pdf> ... <inn.pdf>" >&2
    return 1
  fi
  gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=$@ ; 
}

@rustinlewis
Copy link

Any downside to using -o?

Auto adds the -dBATCH and -dNOPAUSE ... Found in the docs here

gs -o combine.pdf -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress 1.pdf 2.pdf

@frankie303
Copy link

thank you!

@TomLaco
Copy link

TomLaco commented Jul 19, 2022

This could not be easier ! Thank you very much !

@tanimislam
Copy link

thanks!

@Steve778845
Copy link

Thanks!

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