613

There are a lot of software in Windows to merge PDF files but how can we do the same in Ubuntu?

miguelmorales85
  • 523
  • 1
  • 4
  • 19

16 Answers16

606

pdftk

To merge two pdf files, file1.pdf and file2.pdf:

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

More info available hereWay Back Machine.

To install, run:

sudo snap install pdftk
Flimm
  • 44,031
Rojan
  • 6,794
320

PDF Arranger formerly known as PDF-Shuffler.

If you want a tool with a simple GUI, try pdfarranger. It allows for merging of PDFs as well as rearranging and deleting pages. For batch processing and/or more complicated tasks, pdftk is of course more powerful.

Screenshot of PDF-Shuffler

To install PDF Arranger in Ubuntu 20.04 and later open the terminal and type:

sudo apt install pdfarranger
karel
  • 122,292
  • 133
  • 301
  • 332
278

Ghostscript is a package (available by default in Ubuntu) that enables you to view or print PostScript and PDF files to other formats, or to convert those files to other formats.
To use Ghostscript to combine PDF files, type something like the following:

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dAutoRotatePages=/None -sOutputFile=finished.pdf  file1.pdf file2.pdf

Here is a brief explanation of the command:

gs         starts the Ghostscript program.
-dBATCH    once Ghostscript processes the PDF files, it should exit.
           If you don't include this option, Ghostscript will just keep running.
-dNOPAUSE  forces Ghostscript to process each page without pausing for user interaction.
-q         stops Ghostscript from displaying messages while it works
-sDEVICE=pdfwrite 
           tells Ghostscript to use its built-in PDF writer to process the files.
-sOutputFile=finished.pdf
           tells Ghostscript to save the combined PDF file with the specified name.
-dAutoRotatePages=/None
           Acrobat Distiller parameter AutoRotatePages controls the automatic orientation selection algorithm: For instance: -dAutoRotatePages=/None or /All or /PageByPage.

Your input files don't even need to be PDF files. You can also use PostScript or EPS files, or any mixture of the three.

There is a lot you can do with Ghostscript. You can read its documentation for more details.

Source

SebMa
  • 2,927
  • 5
  • 35
  • 47
ignite
  • 9,044
170

You also also use pdfunite to merge pdf documents :

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

To install pdfunite if it is not installed already, run:

sudo apt-get install poppler-utils
Flimm
  • 44,031
BЈовић
  • 4,662
38

PDF Chain Install PDF Chain

A very nice solution is PDFChain. It's GUI is a frontend of PDFTK where you can merge, split or even add some background to your PDF files.

Vincenzo
  • 2,685
19

An alternative approach is to use Latex as explained in this post (without root access assuming that you have pdflatex installed): https://tex.stackexchange.com/questions/8662/merge-two-pdf-files-output-by-latex

This is useful in case you do not have the mentioned tools nor root privileges, but you do have pdflatex.

I copy the tex code below to merge file1.pdf and file2.pdf. Create a file called output.tex and put:

\documentclass{article}
\usepackage{pdfpages}
\begin{document}
\includepdf[pages=-]{file1}
\includepdf[pages=-]{file2}
\end{document}

And to compile, simply use: pdflatex output.tex

The merged file will be named as output.pdf.

emre
  • 301
  • 2
  • 5
13

Give PDFMod a try, it’s from the GNOME project:

https://wiki.gnome.org/Apps/PdfMod

sudo apt install pdfmod
Flimm
  • 44,031
Detnuomnu
  • 2,017
11

Use pdfsam http://www.pdfsam.org/ it's very good for splitting and merging pdfs

sudo apt install pdfsam
Flimm
  • 44,031
R.Sathish Kumar
  • 111
  • 1
  • 3
10

I use pdfseparate to extract specific pages from big pdf file:

pdfseparate -f  156 -l 157 input.pdf  output_%d.pdf 
pdfseparate -f  1   -l 2   input.pdf  output_%d.pdf 

and aftewards I join them all via command:

pdfunite $(ls -v output_*.pdf | tr '\n' ' ') out$(date  +%Y-%m-%d_%H_%M_%S ).pdf

This joins:

output_1.pdf output_2.pdf output_156.pdf output_157.pdf  

into:

out2014-12-14_23_25_36.pdf

May be there is an easier way how to cope... :-)

Installation instructions:

sudo apt install poppler-utils
Flimm
  • 44,031
xerostomus
  • 1,060
6

You can also use jPDFTweak, pdfsam or pdfjam.

(That said, I use pdftk.)

frabjous
  • 6,601
5

You can use pdftk to merge and modify PDF documents in general. Alternatively there's an online service to do just that: http://www.pdfmerge.com/

joschi
  • 265
2

You can see use the free and open source pdftools (disclaimer: I am the author of it).

It is basically a Python interface to the Latex pdfpages package.

To merge pdf files one by one, you can run:

pdftools --input-file file1.pdf --input-file file2.pdf --output output.pdf

To merge together all the pdf files in a directory, you can run:

pdftools --input-dir ./dir_with_pdfs --output output.pdf
1

qpdf

This also worked for me:

sudo apt install qpdf
qpdf --empty --pages in1.pdf in2.pdf in3.pdf -- out.pdf

For some encryption related reason which I don't fully understand pdftk in1.pdf in2.pdf in3.pdf cat output out.pdf, pdftk was failing with:

Error: Invalid PDF: unknown.encryption.type.r

on certain PDFs I downloaded, even though they didn't require a password to open.

Related threads:

Source code: https://github.com/qpdf/qpdf

License: Apache.

Project description:

QPDF is a command-line tool and C++ library that performs content-preserving transformations on PDF files. It supports linearization encryption, and numerous other features. It can also be used for splitting and merging files, creating PDF files (but you have to supply all the content yourself), and inspecting files for study or analysis. QPDF does not render PDFs or perform text extraction, and it does not contain higher-level interfaces for working with page contents. It is a low-level tool for working with the structure of PDF files and can be a valuable tool for anyone who wants to do programmatic or command-line-based manipulation of PDF files.

Tested on Ubuntu 23.10, pdftk 3.3.3, qpdf 11.5.0.

1

Here is my approach:

  • I wanted it to be easily accessible so I created a right-click shortcut in Nautilus (see https://help.ubuntu.com/community/NautilusScriptsHowto)
  • I wanted it to be very quick so I used pdfunite
  • pdfunite only accepts the filepaths in the middle of the command so I had to scratch my head to manage the spaces in the filepaths. So I took the assumption that all filepaths will start with "/home/" and end with ".pdf"

Here is the result:

#!/bin/sh
CLEANED_FILE_PATHS=$(echo $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS | sed 's,.pdf /home/,.pdf\\n/home/,g')
echo $CLEANED_FILE_PATHS | bash -c 'IFS=$'"'"'\n'"'"' read -d "" -ra x;pdfunite "${x[@]}" merged.pdf'

Juste paste this script in

/home/your_username/.local/share/nautilus/scripts

and name it "merge_pdfs.sh" (for example). Then make it executable (right-click on merge_pdfs.sh -> Permissions tab -> tick "Allow executing file as a program"

So now to merge pdf files, you just have to select them -> right click -> scripts -> merge_pdfs.sh and it will create a "merged.pdf" file in the same directory

Hope it helps!

max
  • 445
  • 4
  • 9
0

I did not find pdfarranger in snap (Ubuntu 22.04).

Thus I installed sudo snap install pdfmerger.

This tool also has a simplistic GUI and worked perfectly for me.

hb0
  • 251
0

I had the same issue and this solves the problem

MERGE_ALL_PDF() {
    #preliminary move
    rm merged.pdf
    rm temp.pdf
# All pdf files are listed in FILES, it might be empty
FILES=("./"*.pdf)

# Check if there are any PDF files in the directory
if [ -z "$FILES" ]; then
    echo "No PDF files found!"
    exit 1
fi

# Initialize the merged file with the first PDF
firstFile=true

for f in "${FILES[@]}"
do
    if [ "$firstFile" = true ]; then
        # If this is the first file, initialize merged.pdf with it
        cp "$f" merged.pdf
        firstFile=false
        echo "Initialized merged.pdf with ${f}"
    else
        # Merge the current file with the existing merged.pdf
        pdftk merged.pdf "$f" cat output temp.pdf
        mv temp.pdf merged.pdf
        echo "Appended ${f} to merged.pdf"
    fi
done

echo "Merging completed! The result is in merged.pdf"

}

muru
  • 207,228
SO_32
  • 181