Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a directory of images into a single PDF

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.

like image 945
nobody2100 Avatar asked Feb 17 '23 12:02

nobody2100


1 Answers

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.

like image 116
that other guy Avatar answered Feb 28 '23 01:02

that other guy