Merging PDFs on Linux is a command-line task that leverages tools like Ghostscript or pdftk for quick file combination. If you are wondering how to merge pdf on linux, you have come to the right place. This guide will show you several reliable methods, from simple terminal commands to graphical tools, so you can combine your PDF files without hassle.
Linux offers powerful utilities for PDF manipulation. You don’t need expensive software. The terminal is your best friend here. Let’s get started with the most common approaches.
How To Merge Pdf On Linux
This section covers the core methods to combine PDF files on a Linux system. We will focus on command-line tools first, as they are fast and scriptable. Then we will explore GUI options for those who prefer a visual interface.
Using Ghostscript For PDF Merging
Ghostscript is a swiss-army knife for PDF processing. It is often pre-installed on many Linux distributions. If not, you can install it easily.
Installing Ghostscript
- On Debian/Ubuntu:
sudo apt install ghostscript - On Fedora:
sudo dnf install ghostscript - On Arch:
sudo pacman -S ghostscript
Basic Merge Command
Once installed, use this command to merge multiple PDFs:
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=merged.pdf file1.pdf file2.pdf file3.pdf
Replace file1.pdf file2.pdf with your actual file names. The output will be merged.pdf. This method works well for most files.
Merging All PDFs In A Folder
To merge every PDF in the current directory, use a wildcard:
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=all_merged.pdf *.pdf
This command merges them in alphabetical order. Be careful with the order if it matters to you.
Using Pdftk For Simple Merging
Pdftk (PDF Toolkit) is another popular choice. It is simpler than Ghostscript for basic merges. However, it may not be pre-installed.
Installing Pdftk
- On Debian/Ubuntu:
sudo apt install pdftk - On Fedora:
sudo dnf install pdftk - On Arch:
sudo pacman -S pdftk
Merge Command With Pdftk
The syntax is very straightforward:
pdftk file1.pdf file2.pdf file3.pdf cat output merged.pdf
You can also use wildcards:
pdftk *.pdf cat output combined.pdf
Pdftk is great for quick jobs. It handels encryption and form data well too.
Using Pdfunite (Poppler Utils)
Pdfunite is part of the poppler-utils package. It is lightweight and perfect for simple merges.
Installing Pdfunite
- On Debian/Ubuntu:
sudo apt install poppler-utils - On Fedora:
sudo dnf install poppler-utils
Merge Command
Use this command:
pdfunite file1.pdf file2.pdf output.pdf
It is the simplest option. No complex flags needed. Just list input files and the output name.
Graphical Tools For PDF Merging
If you prefer a graphical interface, Linux has you covered. These tools are user-friendly and intuitive.
Using GIMP
GIMP can open PDFs as layers. You can then export them as a single PDF. However, this method is slow for large files. It works best for small documents.
- Open GIMP and go to
File > Open. Select your first PDF. - When prompted, choose to open pages as layers.
- Repeat for other PDFs, adding them as new layers.
- Go to
File > Export Asand choose PDF format. - Adjust settings and export.
Using LibreOffice Draw
LibreOffice Draw is a free alternative. It can import and merge PDFs.
- Open LibreOffice Draw.
- Go to
File > Openand select your first PDF. - Then go to
Insert > Page > From Fileand choose the next PDF. - Repeat for all files.
- Save as PDF via
File > Export As > Export as PDF.
Using PDF Arranger
PDF Arranger is a dedicated tool for PDF manipulation. It allows drag-and-drop reordering and merging.
- Install via package manager:
sudo apt install pdfarranger - Open the app, then drag PDF files into the window.
- Arrange pages as needed.
- Click
File > Exportto save the merged PDF.
Advanced Merging With QPDF
QPDF is a powerful tool for PDF transformations. It can merge files while preserving structure.
Installing QPDF
- On Debian/Ubuntu:
sudo apt install qpdf - On Fedora:
sudo dnf install qpdf
Merge Command
Use the --empty flag to start from scratch:
qpdf --empty --pages file1.pdf file2.pdf -- output.pdf
You can also specify page ranges:
qpdf --empty --pages file1.pdf 1-3 file2.pdf 4-6 -- output.pdf
This gives you fine control over which pages to include.
Automating Merges With Shell Scripts
If you merge PDFs often, create a script. Save time and avoid typos.
Simple Bash Script
Create a file called merge_pdfs.sh:
#!/bin/bash
pdftk "$@" cat output merged_$(date +%Y%m%d).pdf
Make it executable: chmod +x merge_pdfs.sh
Run it like: ./merge_pdfs.sh file1.pdf file2.pdf
Script With Ghostscript
#!/bin/bash
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=merged_$(date +%Y%m%d).pdf "$@"
This creates a timestamped output file.
Handling Large PDF Files
Large PDFs can be problematic. Ghostscript may consume a lot of memory. Here are some tips.
- Use
-dPDFSETTINGS=/ebookto reduce file size. - Split large files before merging if needed.
- Use QPDF for better memory management.
Merging Password-Protected PDFs
Some PDFs have passwords. Pdftk can handle this.
pdftk A=protected.pdf B=other.pdf cat output merged.pdf input_pw A=password
Replace password with the actual password. Ghostscript may require decryption first.
Preserving Bookmarks And Metadata
When merging, bookmarks may be lost. Ghostscript can preserve them with the right flags.
gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=merged.pdf file1.pdf file2.pdf
This setting tries to keep metadata intact. For exact control, consider using a dedicated tool like pdfbookmark.
Troubleshooting Common Issues
Here are problems you might face and solutions.
Ghostscript Errors
- Error:
/undefinedfilename– Check file names and paths. - Error:
InvalidFont– Install missing fonts or use-dPDFSETTINGS=/screen.
Pdftk Not Found
If pdftk is not available, try pdftk-java or use Ghostscript instead.
Output File Corrupted
Ensure all input PDFs are valid. Use qpdf --check file.pdf to verify.
Comparing Tools: Which One To Choose?
Each tool has strengths. Here is a quick comparison.
- Ghostscript: Most powerful, handles complex files, but slower.
- Pdftk: Simple syntax, good for forms, but not always installed.
- Pdfunite: Fast and minimal, best for quick merges.
- QPDF: Great for page selection and large files.
- GUI Tools: Best for beginners or visual users.
Step-By-Step Example: Merging Three PDFs
Let’s walk through a real example. You have report1.pdf, report2.pdf, and appendix.pdf.
- Open a terminal in the folder containing these files.
- Run:
pdfunite report1.pdf report2.pdf appendix.pdf final_report.pdf - Check the output:
ls -lh final_report.pdf - Open it with a PDF viewer to confirm.
That’s it. You have successfully merged them.
Merging PDFs With Different Orientations
Sometimes pages have mixed orientations. Ghostscript can rotate them automatically.
gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dAutoRotatePages=/All -sOutputFile=merged.pdf file1.pdf file2.pdf
This rotates pages to match the majority orientation.
Batch Merging Multiple Sets
If you have many sets of PDFs to merge, use a loop.
for i in set1 set2 set3; do pdfunite ${i}_*.pdf ${i}_combined.pdf; done
This merges files in each set separately.
Using Docker For PDF Merging
If you don’t want to install tools, use Docker.
docker run --rm -v $(pwd):/work -w /work mnuessler/pdftk *.pdf cat output merged.pdf
This runs pdftk in a container. Ensure Docker is installed.
Merging PDFs From The Internet
You can download and merge in one step.
curl -O http://example.com/file1.pdf -O http://example.com/file2.pdf && pdfunite file1.pdf file2.pdf combined.pdf
This saves time for remote files.
Common Mistakes To Avoid
- Forgetting file extensions – Always include
.pdf. - Mixing up order – List files in the order you want them.
- Not checking output – Always verify the merged file.
- Using wrong paths – Use absolute paths if needed.
Performance Tips
- Use
pdfunitefor small merges (under 100 pages). - Use
qpdffor large files to save memory. - Compress output with
gs -dPDFSETTINGS=/printerfor smaller size. - Run commands in
screenortmuxfor long operations.
Alternative: Using Python Scripts
Python with PyPDF2 or pdfrw can merge PDFs programmatically.
from PyPDF2 import PdfMerger
merger = PdfMerger()
merger.append('file1.pdf')
merger.append('file2.pdf')
merger.write('merged.pdf')
merger.close()
Install PyPDF2 with pip install PyPDF2. This is useful for complex workflows.
Security Considerations
When merging PDFs from untrusted sources, be cautious. PDFs can contain malicious code. Use a sandboxed environment if needed.
Summary Of Commands
Here is a quick reference table.
- Ghostscript:
gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=out.pdf in1.pdf in2.pdf - Pdftk:
pdftk in1.pdf in2.pdf cat output out.pdf - Pdfunite:
pdfunite in1.pdf in2.pdf out.pdf - QPDF:
qpdf --empty --pages in1.pdf in2.pdf -- out.pdf
Frequently Asked Questions
Here are common questions about merging PDFs on Linux.
Can I merge PDFs without installing software?
Yes, you can use online services, but they may compromise privacy. Command-line tools are safer and faster.
How do I merge PDFs in a specific order?
List the files in the desired order in the command. For example: pdftk first.pdf second.pdf third.pdf cat output out.pdf.
What if my PDFs have different page sizes?
Ghostscript can handle mixed sizes. Use -dPDFSETTINGS=/prepress for best results. The output will preserve each page’s original size.
Is there a way to merge PDFs using a GUI on Linux?
Yes, tools like PDF Arranger, LibreOffice Draw, and GIMP offer graphical merging. They are great for occasional use.
Can I merge password-protected PDFs?
Yes, with Pdftk you can provide the password. For example: pdftk A=secured.pdf input_pw A=password cat output out.pdf.
Now you have multiple ways to handle PDF merging on Linux. Choose the method that fits your workflow best. Whether you prefer the speed of the terminal or the simplicity of a GUI, Linux has a solution for you. Practice with small files first, then move to larger projects. Happy merging!