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
convertcommand to create a PDF - The PDF has the same name as the image (e.g.,
photo.pngbecomesphoto.pdf)
Line 2: Merge PDFs (optional)
pdfunite *.pdf out.pdf
- Uses
pdfunitefrom poppler-utils to merge all PDFs intoout.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.
7 replies on “Batch Convert Images to PDF from Command Line”
Does this thing stitches images into one PDF? If No, is there a way to stitch the converted files into one? Maybe this is a lame question.
No it does not. But you can use kpdf to merge multiple pdf into one.
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
The featured image is looking awesome, makes the article even more interesting!
Thank Vishu, 🙂
I am using Paper by 53 with their Pencil.
I enjoy the efforts you have put in this, thank you for all the great content.
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!