How to Merge PDF on Linux

Bits Lovers
Written by Bits Lovers on
How to Merge PDF on Linux

Merging PDFs on Linux is one of those things that sounds harder than it is. Whether you’re combining signed contracts, stacking reports, or just cleaning up a folder full of scanned pages, there are solid tools built right into your terminal. And if you prefer a GUI, those exist too.

A quick search for “merge PDF” turns up plenty of desktop apps and online services. The well-known ones include:

The catch is that most of those are paid and don’t offer a command-line interface. That’s fine if you need a full editor, but if you just want to combine some files quickly, the CLI tools below get the job done faster and for free.

Here’s what I’ll cover:

  • Command-line tools for merging PDFs
    • pdfunite (Poppler)
    • PDFtk
    • Ghostscript
    • ImageMagick
    • qpdf
  • A graphical option (PDFsam)
  • A Python script approach
  • A quick shell alias to speed things up

Before you start

  1. Open a terminal.
  2. Move all the PDFs you want to merge into the same directory. It saves you from typing full paths repeatedly.
  3. Check what PDF tools you already have installed. Type pdf in your terminal and press Tab twice to see what shows up. If something appears, check the man page (e.g., man pdftk) to see what it does.

If your PDF files are inside a zip or tar.gz archive, extract them to the same folder first.

pdfunite: The simplest option

If you’re on Ubuntu, Mint, or most Debian-based distros, pdfunite is probably already available. It comes bundled with the poppler-utils package.

pdfunite file1.pdf file2.pdf mergedfile.pdf

The output file always goes last. That’s it.

Install it if you don’t have it:

On Fedora / RHEL / CentOS:

sudo dnf install poppler-utils

On Debian / Ubuntu:

sudo apt install poppler-utils

On Arch:

sudo pacman -S poppler

pdfunite is my go-to. It’s fast, it’s simple, and it handles the vast majority of merge tasks without any fuss. The only thing it doesn’t do is selective page merging – for that, see the other tools below.

PDFtk: Merge PDF Files

PDFtk is a classic. The original C++ version is no longer maintained, but pdftk-java (the Java port) is actively maintained and available in most distro repositories.

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

Notice the cat output between the input files and the output filename. That’s the syntax PDFtk requires.

Install it on Debian / Ubuntu:

sudo apt install pdftk-java

On Fedora:

sudo dnf install pdftk

On Arch (from AUR):

yay -S pdftk-bin

PDFtk can do a lot more than merging – rotating pages, filling forms, stamping watermarks. Worth knowing about if you work with PDFs regularly.

Ghostscript: Concatenate PDF Files

Ghostscript is a powerhouse for PDF and PostScript manipulation. The syntax is a bit verbose, but it works reliably:

gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=merged.pdf -dBATCH fileA.pdf fileB.pdf

Install it on Fedora / RHEL:

sudo dnf install ghostscript

On Debian / Ubuntu:

sudo apt install ghostscript

The -dNOPAUSE flag stops Ghostscript from pausing between pages, and -dBATCH tells it to exit after processing. Run gs -h to see the full list of output devices.

qpdf: A modern alternative

qpdf is worth knowing about. It’s actively maintained, handles large files well, and has clean syntax for merging:

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

Install it:

On Debian / Ubuntu:

sudo apt install qpdf

On Fedora / RHEL:

sudo dnf install qpdf

On Arch:

sudo pacman -S qpdf

qpdf is particularly good at preserving PDF structure, handling encrypted files, and linearizing PDFs for web viewing. It’s one of those tools that doesn’t get enough attention.

ImageMagick: Merge PDF Files

ImageMagick can merge PDFs too, but I’d consider it a last resort for this specific task. It works by rasterizing pages and re-encoding them, which means the output quality can degrade and file sizes can balloon.

That said, here’s how it works.

Install ImageMagick on Fedora / RHEL:

sudo dnf install ImageMagick

On Debian / Ubuntu:

sudo apt install imagemagick

If you’re running ImageMagick 7 (which most current distros ship), the command is magick, not convert:

magick file1.pdf file2.pdf mergedfile.pdf

On older systems still running ImageMagick 6, the legacy command still works:

convert file1.pdf file2.pdf mergedfile.pdf

To merge specific pages, use bracket notation (pages are 0-indexed):

magick file1.pdf[0-2] fileB.pdf mergedresult.pdf

You can also set the DPI for better quality:

magick -density 300 fileA.pdf fileB.pdf merged.pdf

And yes, you can mix images with PDFs:

magick fileA.pdf imageA.jpg merged.pdf

Fixing the “security policy” error

If you get an error like:

convert: attempt to perform an operation not allowed by the security policy 'PDF'

ImageMagick blocks PDF operations by default due to security concerns. You need to edit the policy file.

Open it with your editor:

sudo nano /etc/ImageMagick-6/policy.xml

On newer systems with ImageMagick 7, the file is at:

sudo nano /etc/ImageMagick-7/policy.xml

Find this line:

<policy domain="coder" rights="none" pattern="PDF" />

Change rights="none" to rights="read|write":

<policy domain="coder" rights="read|write" pattern="PDF" />

Save and close. The merge command should work now.

Reference: AskUbuntu

Reduce PDF File Size

If your merged PDF is too large (common when it contains scanned images), Ghostscript is the best tool for compressing it:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dQUIET -dBATCH -sOutputFile=compressed.pdf input.pdf

The -dPDFSETTINGS flag controls the compression level:

Setting Quality DPI Use case
/prepress High 300 dpi Print-ready output
/ebook Medium 150 dpi Good balance of size and quality
/screen Low 72 dpi Smallest file, fine for on-screen reading

If you don’t specify a setting, /prepress is the default.

How to merge PDF files using PDFsam

PDFsam Basic is a free, open-source GUI tool for splitting, merging, rotating, and rearranging PDFs. It runs on Linux, Windows, and macOS. It’s a good option if you prefer a visual interface.

PDFsam Basic 5.x requires Java 17 or newer. Most current distros ship a compatible JDK.

On Debian / Ubuntu, install the dependencies:

sudo apt install default-jre libopenjfx-jni libopenjfx-java openjfx

Then download and install PDFsam from the releases page:

wget https://github.com/torakiki/pdfsam/releases/download/v5.2.6/pdfsam_5.2.6-1_amd64.deb
sudo dpkg -i pdfsam_5.2.6-1_amd64.deb

(Check the releases page for the latest version number – it may have changed since this was written.)

Launch it:

pdfsam

How to merge PDF files on Linux

How to merge PDF files using PDFsam

How to merge PDF files using PDFsam

How to merge PDF files using PDFsam

PDFsam is powered by the Sejda SDK, which is actively maintained and battle-tested. If you’re building a Java application that needs PDF manipulation, that’s the library to look at.

How to Merge PDF files in Ubuntu

Everything above works on Ubuntu. If you want a graphical tool specifically, PDF Arranger is a lightweight option available in the Ubuntu App Store.

What PDF Arranger can do:

  • Merge multiple PDF documents
  • Zoom in and out
  • Remove pages from a PDF
  • Crop PDF pages
  • Reorder pages with drag and drop
  • Export selected pages
  • Rotate pages

How to Merge PDF files in Ubuntu

How to Merge PDF files in Ubuntu

Install it:

On Ubuntu / Debian:

sudo apt install pdfarranger

On Fedora 30+:

sudo dnf install pdfarranger

Combine PDF Files on Arch Linux

The pdfjam package (part of TeX Live) provides the pdfjoin command on Arch:

Merge all PDFs in the current directory, sorted alphabetically:

pdfjoin *.pdf

Merge all JPG images into a PDF:

pdfjoin *.jpg

Specify an explicit order:

pdfjoin 1.pdf 2.pdf 3.pdf

Note: pdfjam depends on a TeX distribution, so it pulls in a fair number of packages. If you don’t already have TeX installed, pdfunite or qpdf are lighter options.

Automate: A Quick Alias for Merging PDFs

Merging PDFs on Linux doesn’t require complex commands. But typing the same thing repeatedly gets old. I considered writing a shell script, but realized a simple alias is all I need.

Add this to your ~/.bashrc (or ~/.zshrc if you use zsh):

alias mf='pdftk *.pdf cat output'

Then reload your shell config:

source ~/.bashrc

Now you can merge all PDFs in the current directory with:

mf merged.pdf

The merged.pdf argument is the output filename. pdftk will combine every .pdf file in the current folder into that one file.

This works for my workflow. If you want the alias to use pdfunite instead (simpler syntax), just adjust it:

alias mfu='pdfunite *.pdf'

Then run mfu merged.pdf.

Python Script: Merge PDF Files

If you need to merge PDFs programmatically, Python is a good option. The library to use these days is pypdf (not PyPDF2 – that one is deprecated and no longer maintained).

Install it:

pip install pypdf

The code is straightforward. Here’s a minimal example:

from pypdf import PdfWriter, PdfReader

writer = PdfWriter()

for pdf_file in ["file1.pdf", "file2.pdf"]:
    reader = PdfReader(pdf_file)
    for page in reader.pages:
        writer.add_page(page)

with open("merged.pdf", "wb") as f:
    writer.write(f)

I also have a ready-to-use script on GitHub that takes filenames as command-line arguments:

python bitslovers-pdf-merge.py file01.pdf file02.pdf

Online Options

If you don’t want to install anything and the PDFs don’t contain sensitive information, these free online tools work for small documents:

I’d avoid uploading confidential documents to any online service. For anything sensitive, stick with the local tools above.

Wrapping up

When I first started using Linux, managing PDFs from the command line felt like a chore. Now there are plenty of reliable tools and most of them take a single command.

My recommendation: start with pdfunite for simple merges. If you need more control (selective pages, forms, encryption), reach for pdftk or qpdf. And if you just want a GUI, PDF Arranger or PDFsam will do the job.

Got a tool I missed or a better workflow? Leave a comment – I’d like to hear how you handle PDFs on Linux.

Bits Lovers

Bits Lovers

Professional writer and blogger. Focus on Cloud Computing.

Comments

comments powered by Disqus