How to merge multiple PDF files into one PDF in Linux?

Considering that pdfunite is part of poppler it has a higher chance to be installed, usage is also simpler than pdftk:

pdfunite in-1.pdf in-2.pdf in-n.pdf out.pdf

Just make sure you remember to provide out.pdf, or else it will overwrite the last file in your command

How to merge multiple PDF files into one PDF in Linux?

Try the good ghostscript:

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=merged.pdf mine1.pdf mine2.pdf

or even this way for an improved version for low resolution PDFs (thanks to Adriano for pointing this out):

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=merged.pdf mine1.pdf mine2.pdf

In both cases the ouput resolution is much higher and better than this way using convert:

convert -density 300x300 -quality 100 mine1.pdf mine2.pdf merged.pdf

In this way you wouldn’t need to install anything else, just work with what you already have installed in your system (at least both come by default in my box).

UPDATE: first of all thanks for all your nice comments!! just a tip that may work for you guys, after googleing, I found a superb trick to shrink the size of PDFs, I reduced with it one PDF of 300 MB to just 15 MB with an acceptable resolution! and all of this with the good ghostscript, here it is:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -dDetectDuplicateImages -dCompressFonts=true -r150 -sOutputFile=output.pdf input.pdf


Answer #3:

I’m sorry, I managed to find the answer myself using google and a bit of luck : )

For those interested;

I installed the pdftk (pdf toolkit) on our debian server, and using the following command I achieved desired output:

pdftk file1.pdf file2.pdf cat output output.pdf


gs -q -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=output.pdf file1.pdf file2.pdf file3.pdf ...

This in turn can be piped directly into pdf2ps.

Merge/convert multiple PDF files into one PDF:

This is the easiest solution if you have multiple files and do not want to type in the names one by one:

qpdf --empty --pages *.pdf -- out.pdf

Answer #5:

pdfunite is fine to merge entire PDFs. If you want, for example, pages 2-7 from file1.pdf and pages 1,3,4 from file2.pdf, you have to use pdfseparate to split the files into separate PDFs for each page to give to pdfunite.

At that point you probably want a program with more options. qpdf is the best utility I’ve found for manipulating PDFs. pdftk is bigger and slower and Red Hat/Fedora don’t package it because of its dependency on gcj. Other PDF utilities have Mono or Python dependencies. I found qpdf produced a much smaller output file than using pdfseparate and pdfunite to assemble pages into a 30-page output PDF, 970kB vs. 1,6450 kB. Because it offers many more options, qpdf‘s command line is not as simple; the original request to merge file1 and file2 can be performed with

qpdf --empty --pages file1.pdf file2.pdf -- merged.pdf

Answer #6:

You can use the convert command directly,


convert sub1.pdf sub2.pdf sub3.pdf merged.pdf

Answer #7:

I am biased being one of the developers of PyMuPDF (a Python binding of MuPDF).

You can easily do what you want with it (and much more). Skeleton code works like this:

import fitz         # the binding PyMuPDF
fout =  # new PDF for joined output
flist = ["1.pdf", "2.pdf", ...]  # list of filenames to be joined

for f in flist:
    fin =  # open an input file
    fout.insertPDF(fin) # append f

That’s about it. Several options are available for selecting only pages ranges, maintaining a joint table of contents, reversing page sequence or changing page rotation, etc., etc.

We are on PyPi.

Answer #8:

Although it’s not a command line solution, it may help macos users:

  1. Select your PDF files
  2. Right-click on your highlighted files
  3. Select Quick actions > Create PDF

Answer #9:

I second the pdfunite recommendation. I was however getting Argument list too long errors as I was attempting to merge > 2k PDF files.

I turned to Python for this and two external packages: PyPDF2 (to handle all things PDF related) and natsort (to do a “natural” sort of the directory’s file names). In case this can help someone:

from PyPDF2 import PdfFileMerger
import natsort
import os

DIR = "dir-with-pdfs/"
OUTPUT = "output.pdf"

file_list = filter(lambda f: f.endswith('.pdf'), os.listdir(DIR))
file_list = natsort.natsorted(file_list)

# 'strict' used because of
merger = PdfFileMerger(strict=False)

for f_name in file_list:
  f = open(os.path.join(DIR, f_name), "rb")

output = open(OUTPUT, "wb")

Answer #10:

Here is a Bash script which checks for merging errors.

I had the problem that a few PDF merges produced some error messages. As it is quite a lot trial and error to find the corrupt PDFs, I wrote a script for it.

The following Bash script merges all available PDFs in a folder one by one and gives a success status after each merge. Just copy it in the folder with the PDFs and execute from there.


rm -f "${PDFOUT}"

for f in *.pdf
  printf "processing %-50s" "$f  ..." >&2
  if [ -f "$PDFOUT" ]; then
    #  -dPDFSETTINGS=/prepress
    status=$(gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="${PDFOUT}.new" "${PDFOUT}" "$f" 2> /dev/null)
    if [ "$status" ]
      echo "gs ERROR: $status" >&2
      echo "successful" >&2
    mv "${PDFOUT}.new" "${PDFOUT}"
    cp "$f" "${PDFOUT}"
    echo "successful" >&2

example output:

processing inp1.pdf  ...                                     successful
processing inp2.pdf  ...                                     successful

Hope you learned something from this post.

Follow Programming Articles for more!

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →