Convert Images to PDF Fast with img2pdf: A Step-by-Step Guideimg2pdf is a lightweight, lossless command-line tool for converting raster images (JPEG, PNG, TIFF, WebP, etc.) into PDF files without re-encoding image data. It preserves image quality, supports multiple images per PDF, and offers options for page size, metadata, layout, and compression when appropriate. This guide shows how to install img2pdf, use common command patterns, automate batch conversions, tweak layout and page settings, and troubleshoot problems.
Why choose img2pdf?
- Lossless conversion: img2pdf wraps image data into a PDF container without re-encoding (for formats that allow embedding), so there’s no quality loss.
- Fast and lightweight: Minimal dependencies and quick execution, especially useful for scripts and servers.
- Flexible layout and sizing: Specify page size, orientation, margins, and image positioning.
- Multiplatform: Works on Windows, macOS, Linux, and in Python environments.
Installation
img2pdf is a Python package available on PyPI. Install it using pip:
-
For most systems:
pip install img2pdf
-
If you need system-wide install or on Linux:
python3 -m pip install --user img2pdf
-
From source (for latest development version):
git clone https://github.com/josch/img2pdf.git cd img2pdf pip install .
If you prefer package managers:
- On Debian/Ubuntu:
sudo apt install img2pdf
- On macOS with Homebrew:
brew install img2pdf
Confirm installation:
img2pdf --version
Basic usage
Convert a single image to PDF:
img2pdf input.jpg -o output.pdf
Convert multiple images into a single PDF (order matters — use natural sorting or explicit listing):
img2pdf img1.jpg img2.png img3.tiff -o album.pdf
Use shell globbing (be careful with order):
img2pdf *.jpg -o all_photos.pdf
Page size, orientation, and scaling
Set a specific page size (for example, A4):
img2pdf -o output.pdf --pagesize A4 input.jpg
Use custom size in points (1 point = ⁄72 inch), millimeters, or inches:
img2pdf -o output.pdf --pagesize 210mmx297mm input.jpg
Fit image to page while preserving aspect ratio (default behavior). To center images and add white margins:
img2pdf -o output.pdf --fit centering --border 10mm input.jpg
To rotate pages:
img2pdf -o output.pdf --rotate 90 input.jpg
Image arrangement and multiple images per page
Place several images on one PDF page using layout options:
img2pdf -o contact_sheet.pdf --layout 2x3 img1.jpg img2.jpg img3.jpg img4.jpg img5.jpg img6.jpg
This arranges images in 2 columns by 3 rows.
Control spacing and padding with –border and –xres/–yres options if you need precise control over image sizes.
Preserve or set PDF metadata
Add title, author, subject, and keywords:
img2pdf -o out.pdf --title "Vacation 2025" --author "Alice" --subject "Photos" --keywords "vacation,beach"
Working with transparency and color profiles
- PNGs with transparency are supported; img2pdf places transparent areas over the PDF page background (white by default).
- Color profiles embedded in images are preserved when possible. For strict color management, convert images beforehand with tools like ImageMagick or dedicated color-management utilities.
Compressing PDFs
img2pdf itself focuses on lossless embedding. If you need smaller files, you can:
- Resize images before conversion (e.g., with ImageMagick):
magick input.jpg -resize 2000x2000 resized.jpg img2pdf resized.jpg -o out.pdf
- Recompress the final PDF with ghostscript (lossy):
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dBATCH -sOutputFile=out_compressed.pdf out.pdf
Using img2pdf from Python
img2pdf can be used as a library:
import img2pdf with open("out.pdf","wb") as f: f.write(img2pdf.convert(["img1.jpg","img2.png"], dpi=300, layout=(2,2)))
Examples for per-image layout, DPI control, and page dimensions are in the img2pdf documentation.
Batch processing and automation
-
Convert all images in subfolders and preserve order:
find . -type f ( -iname "*.jpg" -o -iname "*.png" ) | sort | xargs img2pdf -o combined.pdf
-
Loop in shell to create one PDF per folder:
for d in */ ; do img2pdf "$d"/*.jpg -o "${d%/}.pdf" done
-
Use Python to walk directories and call img2pdf.convert for finer control (filenames with Unicode handled properly).
Troubleshooting
- “Unsupported image format”: ensure PIL (Pillow) supports the image; install Pillow: pip install Pillow.
- Wrong order with globbing: sort filenames explicitly (ls -v or GNU sort -V).
- Large memory use: process images in smaller batches or downscale before conversion.
- Corrupt output PDF: verify input images and try creating a single-image PDF; update img2pdf and dependencies.
Examples and common command recipes
Create A4 PDFs from all PNGs at 300 DPI:
img2pdf *.png -o images.pdf --pagesize A4 --dpi 300
Make a printable contact sheet (2 columns x 5 rows) with borders:
img2pdf -o sheet.pdf --layout 2x5 --border 5mm *.jpg
Convert TIFF multipage to PDF (preserve pages):
img2pdf multipage.tiff -o multipage.pdf
Further reading and resources
- img2pdf command-line help: img2pdf –help
- Official repository and documentation for advanced options and examples.
Leave a Reply