STEP 0 : Install

Install program pdftk and imagemagick :

sudo apt-get install pdftk imagemagick

STEP 1 : Convert

Input is :
/image/topic-01/01-01.jpg
/image/topic-02/02-01.jpg
/image/topic-02/02-02.jpg
/image/topic-03/03-01.jpg

Output is :
- topic-01.pdf (1 pages)
- topic-02.pdf (2 pages)
- topic-03.pdf (1 pages)

Basic convert :

find /image -type d | while read d; do convert "${d}"/.jpg ./"${d##/}.pdf"; done

Option resize convert(Reduced quality) :

find /image -type d | while read d; do convert -density 150 -quality 50 "${d}"/.jpg ./"${d##/}.pdf"; done

STEP 2 : Combine

Input is :
*pdf

Output is :
onefile.pdf

Combine pdf files :

pdftk *pdf cat output onefile.pdf

Dani AI

Generated

Good follow-ups to and — a few practical alternatives and gotchas that make a batch workflow more reliable and often produce smaller, lossless PDFs.

Image-quality and ordering
ImageMagick’s convert is handy but will usually recompress images (bigger or lower‑quality PDFs) and can ignore EXIF rotation/order unless you normalize first. If you want truly lossless embedding of existing JPEGs (no re-encode), use img2pdf. Make sure files are named so they sort in the intended page order (01-01.jpg, 01-02.jpg, …) before batching.

Per-directory conversion (lossless)
A small robust loop that keeps original JPEG data and creates one PDF per folder:

for d in /image/*/; do
  img2pdf -o "$(basename "$d").pdf" "$d"/*.jpg
done

Use the find/print0 pattern if directory names may contain spaces.

Merging and size control
For a simple merge, pdfunite (as noted by ) is lightweight. For more control (compression/compatibility), Ghostscript is the go-to:

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=onefile.pdf *.pdf

To aggressively reduce output size, add -dPDFSETTINGS=/ebook (or /screen, /printer) — test to pick the quality/size tradeoff you want.

Troubleshooting tips

  • Normalize EXIF rotation (so pages aren’t sideways) before converting.
  • Test on one folder first to confirm order and quality.
  • If convert fails on PDF output, recent ImageMagick/security policies or missing Ghostscript delegates may be the reason; img2pdf + Ghostscript are good fallbacks.

These approaches keep image quality predictable, avoid unnecessary recompression, and give simple tools for per-folder PDF creation and final merging.

You can also use /usr/bin/pdfunite to merge several pdf into one (in ubuntu, install package poppler-utils for this)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.