All posts

Merge PDF via CLI

Concatenate several PDFs into one file with Ghostscript.

Open online converter

Packages to install

ghostscript is enough. File order at the end of the command becomes page order in the result.

ghostscript
apt install
sudo apt-get update
sudo apt-get install -y ghostscript

How to run the command

gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sOutputFile=merged.pdf a.pdf b.pdf. Add as many input PDFs as you need after OutputFile.

gs
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sOutputFile=merged.pdf a.pdf b.pdf

Dockerfile example

Build a minimal Ubuntu image with the required packages and run the conversion inside the container.

Dockerfile
FROM ubuntu:24.04

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        ghostscript && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /work
docker build && docker run
docker build -t merge-pdf .
docker run --rm -v "$PWD:/work" -w /work merge-pdf \
  gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sOutputFile=merged.pdf a.pdf b.pdf

Example commands

Run these locally after installing the packages, or inside the container from the Dockerfile.

Example 1
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sOutputFile=merged.pdf a.pdf b.pdf
Example 2
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sOutputFile=merged.pdf part1.pdf part2.pdf part3.pdf