Converting multiple images to one PDF on Linux

There are a number of ways you can go about the process of converting image files into PDFs, most simply by opening a given image and printing it. You can select “Print to file” instead of a printer device, PDF format is the default for this type of output. But say you have a folder full of images and you want to make one big PDF of them. Here’s how you can do that:

1
2
3
4
5
6
7
cd FOLDER_WITH_IMAGES
FILES=$(ls *jpg)
mkdir temp && cd temp
for file in $FILES; do BASE=$(echo $file | sed 's/.jpg//g'); convert ../$BASE.jpg $BASE.pdf; done &&
pdftk *pdf cat output ../FINAL_NAME.pdf &&
cd ..
rm -rf temp

This loops through all the .jpg images in the directory and converts them to a PDF file of the same name. Once that is done they are all combined into FINAL_NAME.pdf by pdftk, a handy PDF utility. The temp dir business is there to make the temp PDF file removal easier.

Share and Enjoy:
  • email
  • LinkedIn
  • Slashdot
  • StumbleUpon
  • Technorati
  • Netvibes

Post to Twitter Post to Delicious Post to Digg Post to Reddit

Possibly Related (no promises):

  1. Viewing all users on a Linux system

Related posts brought to you by Yet Another Related Posts Plugin.

This entry was posted in CLI and tagged , , . Bookmark the permalink.

2 Responses to Converting multiple images to one PDF on Linux

  1. Montebelo says:

    It worked very very well!
    I had problems with the “convert” (from imagemagick) because it makes my computer run out of memory when I want to convert a lot of image files into one pdf. This seems to solve the problem. I just wich I could understand all the those BASE=$ commands. lol

  2. @Montebelo I’m glad it worked for you! Actually that command probably looks trickier than it is. The for loop iterates over each item in the FILES variable (i.e. all the image files we want to convert). For each item BASE is set to the image filename with the extension (“.jpg”) stripped. This is so we can title our output the same as the original. In the convert step, we use BASE to specify what image to convert and also what to call the resultant PDF. Hope that helps!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>