Categories
Command Line Scripting Tutorial

Batch Convert Images to PDF from Command Line

A friend asked me for a way to batch-convert images into PDFs. Instead of using online converters, here's a command-line solution using ImageMagick that works on both Mac and Linux. It converts all images in a folder to individual PDFs and optionally merges them into one file.

Prerequisites

macOS

Install ImageMagick via Homebrew:

brew install imagemagick

If you don't have Homebrew, install it first from brew.sh.

Linux (Ubuntu/Debian)

Install ImageMagick and Poppler utilities:

sudo apt-get install imagemagick
sudo apt-get install poppler-utils

The Script

This script converts all PNG images in the current directory to individual PDFs, then merges them into a single file:

for image in `ls *.png`; do convert $image `basename $image .png`.pdf; done
pdfunite *.pdf out.pdf

How It Works

Line 1: Convert images to PDFs

for image in `ls *.png`; do convert $image `basename $image .png`.pdf; done
  • Lists all PNG files in the current directory
  • For each image, uses ImageMagick's convert command to create a PDF
  • The PDF has the same name as the image (e.g., photo.png becomes photo.pdf)

Line 2: Merge PDFs (optional)

pdfunite *.pdf out.pdf
  • Uses pdfunite from poppler-utils to merge all PDFs into out.pdf
  • Skip this line if you want individual PDFs instead of one merged file

Usage Examples

Convert all PNG images:

cd /path/to/your/images
for image in `ls *.png`; do convert $image `basename $image .png`.pdf; done
pdfunite *.pdf out.pdf

Convert JPG images instead:

for image in `ls *.jpg`; do convert $image `basename $image .jpg`.pdf; done
pdfunite *.pdf out.pdf

Convert without merging:

for image in `ls *.png`; do convert $image `basename $image .png`.pdf; done
# Now you have individual PDFs for each image

Additional Options

Control image quality and size:

convert -quality 90 -density 150 image.png output.pdf
  • -quality 90: JPEG quality (0-100, higher = better quality)
  • -density 150: DPI resolution (default is 72)

Convert multiple formats at once:

for image in *.{png,jpg,jpeg}; do convert "$image" "${image%.*}.pdf"; done

This handles PNG, JPG, and JPEG files in one pass.

Notes

File naming: The script preserves image filenames. scan001.png becomes scan001.pdf.

Order matters: If you're merging PDFs, they're combined in alphabetical order. Name your files accordingly (e.g., 01.png, 02.png, 03.png).

Large batches: For hundreds of images, this might take a few minutes. ImageMagick processes each image sequentially.

Cleanup: After merging, you may want to delete individual PDFs:

rm !(out).pdf  # Keeps only out.pdf

Alternative tools: If you prefer GUI tools, consider:

  • Mac: Preview.app can combine images into PDFs
  • Linux: GIMP, LibreOffice Draw
  • Cross-platform: Online converters like jpg2pdf.org

View the original code on Gist.

By Shishir Sharma

Shishir Sharma is a Software Engineering Leader, husband, and father based in Ottawa, Canada. A hacker and biker at heart, and has built a career as a visionary mentor and relentless problem solver.

With a leadership pedigree that includes LinkedIn, Shopify, and Zoom, Shishir excels at scaling high-impact teams and systems. He possesses a native-level mastery of JavaScript, Ruby, Python, PHP, and C/C++, moving seamlessly between modern web stacks and low-level architecture.

A dedicated member of the tech community, he serves as a moderator at LUG-Jaipur. When he’s not leading engineering teams or exploring new technologies, you’ll find him on the open road on his bike, catching an action movie, or immersed in high-stakes FPS games.

7 replies on “Batch Convert Images to PDF from Command Line”

I do not know whether it’s just me or if perhaps everyone else encountering issues with your site.
It appears as though some of the written text on your content are running off the screen.
Can someone else please provide feedback and let me know if this is
happening to them as well? This might be a issue with my browser because I’ve had this happen before.
Thank you

Right here is the perfect site for anybody who hopes to find out about this topic.
You understand so much its almost hard to argue with you (not that I actually would want to…HaHa).
You certainly put a fresh spin on a topic which has been written about for a long time.
Excellent stuff, just wonderful!

Leave a Reply to Alice Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.