Merge JPG Images into PDF on Linux
Converting a series of JPG images into a single PDF is useful for creating documents, presentations, or archiving scanned photos. Linux offers several tools for this task, each with different trade-offs between simplicity, quality preservation, and features.
Using img2pdf (Recommended)
For most cases, img2pdf is the best choice because it merges images without re-encoding or re-compressing them, preserving original quality:
img2pdf *.jpg -o output.pdf
To control the order of images, specify them explicitly:
img2pdf image1.jpg image2.jpg image3.jpg -o output.pdf
You can also set page size and margins:
img2pdf *.jpg -o output.pdf --imgsize 210x297mm --margin 10
The --imgsize flag accepts common sizes like A4, letter, or custom dimensions. If your images are already the right size, you can fit them to the page:
img2pdf *.jpg -o output.pdf --fit shrink
Install img2pdf on Debian/Ubuntu:
sudo apt install img2pdf
On RHEL/CentOS/Fedora:
sudo dnf install img2pdf
Using ImageMagick
ImageMagick’s convert command works but re-compresses images, reducing quality:
convert *.jpg output.pdf
This approach is slower and larger in file size compared to img2pdf. However, ImageMagick is useful if you need to manipulate images during conversion (resize, rotate, adjust colors):
convert *.jpg -resize 50% -quality 85 output.pdf
Using Ghostscript
For more control over PDF compression and settings, use Ghostscript:
convert *.jpg output.pdf && gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/ebook -o compressed.pdf output.pdf
Using PDFtk (for manipulation after creation)
If you need to merge PDFs that you’ve already created, or reorder pages:
pdftk page1.pdf page2.pdf output merged.pdf
Handling Specific Formats
For mixed image formats (JPG, PNG, TIFF):
img2pdf *.jpg *.png -o output.pdf
For images with rotation metadata:
img2pdf --auto-orient *.jpg -o output.pdf
Processing Large Image Sets
For directories with many images, sort them numerically:
img2pdf $(ls -v *.jpg) -o output.pdf
Or use find with numerical sorting:
find . -maxdepth 1 -name "*.jpg" -print0 | sort -z -V | xargs -0 img2pdf -o output.pdf
Adding OCR (Optional)
To make PDFs searchable, use OCRmyPDF after creation:
img2pdf *.jpg -o output.pdf
ocrmypdf output.pdf output-ocr.pdf
This requires ocrmypdf and Tesseract. Install on Debian/Ubuntu:
sudo apt install ocrmypdf tesseract-ocr
Performance Considerations
- img2pdf: Fastest, lossless, smallest output file
- ImageMagick: Slower, lossy compression, useful for pre-processing
- Ghostscript: Good for compression control after creation
For archival or quality-critical work, always use img2pdf. Reserve ImageMagick and Ghostscript for cases where you need image manipulation or aggressive compression.
Note that convert will read the uncompressed input files into memory before converting to pdf, so if you have a lot of files, you might run out of memory.
An alternative approach is first convert each file to pdf, and then combine the pdfs into one.