Skip to content
Advertisement

Script to merge all image files from a folder inside a pdf

I frequently have to merge a lot of images inside a pdf. Naturally, I’d like to automate this a bit.

Here are the things I need:

  • lossless merge into a pdf
  • merge of most common image formats
  • natural sorting of numbers (1.jpg, 2.jpg, and 10.jpg would be merged in this order, not as 1.jpg 10.jpg 2.jpg)
  • can deal with white-spaces in file names

I’ve come across a solution that works exactly like I’d need it to, but which fails for files containing a white-space in their name.

#!/bin/bash
convert `ls -v *.jpg *.jpeg *.JPG *.JPEG *.png *.PNG *.gif *.GIF *.tiff *.TIFF *.webp *.WEBP *.bmp *.BMP` compilation_images.pdf

Advertisement

Answer

Trying to parse ls is wrong, as you’ve found out. Assuming you’re using GNU versions of utilities as the linux tag usually implies, try something like:

#!/usr/bin/env bash

# Turn on case-insensitive globs and make ones that
# don't match anything expand to nothing
shopt -s nocaseglob nullglob

readarray -d '' -t images < <(printf "%s" *.jpg *.jpeg *.png *.gif *.tiff *.webp *.bmp | sort -Vz)
convert "${images[@]}" compilation_images.pdf

which uses the common trick of separating filenames with nul characters in order to safely handle whitespace when populating a bash array with sorted filenames.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement