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.

nobody2100
  • 161
  • 1
  • 7

1 Answers1

9

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.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • wow... this is embarrassing lol Thanks for the quick reply and, even more, for this simple solution ;) – nobody2100 Feb 17 '13 at 17:29
  • 1
    If you're interested in the learning experience, [whatswrongwithmyscript.com](http://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. – that other guy Feb 17 '13 at 17:44
  • 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 ;D – nobody2100 Mar 02 '13 at 10:45