convert a directory of images into a single PDF
Asked Answered
S

1

5

I have a directory of images:

path/to/directory/
   image01.jpg
   image02.jpg
   ...

and would like to convert it into a single PDF file:

path/to/directory.pdf

This is what I managed to code so far:

#!/bin/bash

echo Directory $1
out=$(echo $1 | sed 's|/$|.pdf|')
echo Output $out

mkdir tmp

for i in $(ls $1)
do
    # MAC hates sed with "I" (ignore case) - thanks SO for the perl solution!
    # I want to match "jpg, JPG, Jpg, ..."
    echo $1$i $(echo "tmp/$i" | perl -C -e 'use utf8;' -pe 's/jpg$/pdf/i')
    convert $1$i $(echo "tmp/$i" | perl -C -e 'use utf8;' -pe 's/jpg$/pdf/i')
done

pdftk tmp/*.pdf cat output $out
rm -rf tmp

So the idea was to convert each image into a pdf file with imagemagick, and use pdftk to merge it into a single file. Thanks to the naming of the files I don't have to bother about the ordering.

Since I'm a newbie to this I'm sure there are many refinements one can do:

  • only iterate over image-files in the directory (in case there is some Readme.txt,...)
  • including the extensions png, jpeg, ...
  • using the trailing "/" is not elegant I admint
  • etc.

Currently my main problem is, however, that there are cases where my directories and image files contain spaces in their names. The for-loop then iterates over sub-strings of the filename and I imagine that the line with convert will also fail. I have tried out some things but haven't succeeded so far and hope someone will be able to help me here. If anyone has ideas to address the issues I listed above as well I would be very glad to hear them too.

Stamps answered 17/2, 2013 at 17:6 Comment(0)
B
10

convert can do this in one go:

convert *.[jJ][pP][gG] output.pdf

Or to answer several of your other questions and replace your script:

#!/bin/bash
shopt -s nullglob nocaseglob
convert "$1"/*.{png,jpg,jpeg} "${1%/}.pdf"

will iterate over all the given extensions in the first argument, regardless of capitalization, and write to yourdir.pdf. It will not break on spaces.

Beyer answered 17/2, 2013 at 17:13 Comment(3)
wow... this is embarrassing lol Thanks for the quick reply and, even more, for this simple solution ;)Stamps
If you're interested in the learning experience, whatswrongwithmyscript.com will point out a dozen things to do in your original script to help handle spaces. I'd be happy to give it another look afterwards.Beyer
thanks for the link! it sure helps to spot the common mistakes i did in my original script. also thx for the update on the script! it's now in my local bin as a "dir2pdf" command ;DStamps

© 2022 - 2024 — McMap. All rights reserved.