Converting multiple images into one PDF on Linux

There are a number of ways you can go about 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, then select PDF format and give it a name. But what to do if you have a folder full of images and want to make one big PDF of them? Here’s how you can do that:

1
2
cd FOLDER_WITH_IMAGES
convert *.jpg images.pdf

This creates a single PDF, images.pdf, containing all JPG files in the directory. It uses the very handy convert utility. Thanks to reader andreas for pointing out this much simpler approach!

[Edit, 2011-08-18] Thanks to reader A. Syukri for pointing out that the above approach can crash when there are too many input files. Here’s the longer approach that should be more stable:

1
2
3
4
5
6
7
8
9
10
cd FOLDER_WITH_IMAGES
FILES=$( find . -type f -name "*jpg" | cut -d/ -f 2)
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.

If you need to do this frequently you can create a bash function and pass in the folder containing the JPGs. You might want to also consider passing in the extension(s) desired if that varies for you.

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

No related posts.

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

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

10 Responses to Converting multiple images into 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!

  3. andreas says:

    what about the single line:

    convert *.jpg figures.pdf;

    or am I missing something here?

    take care

  4. @andreas Your invocation works perfectly well. I cannot fathom right now why I made it so complicated! The only reason I needed a temp folder was to deal with the intermediate PDFs, which I don’t at all need as your example shows. I’ll rework the post shortly, thanks very much for the correction!

  5. A. Syukri says:

    I cannot fathom right now why I made it so complicated!

    Because the one-liner will crash when handling too many files as input. Can you please show again the original commands you used that @montebelo said was working?

  6. @A. Syukri: Thanks for reporting that! I’ve added the older version into the post.

    Note: It doesn’t handle case-variability in file extensions right now, so will try to add that soon.

  7. Enriqueto says:

    Amazing!

  8. Abhishek D says:

    The quality of pdf generated by this method is too poor.Though many people might have not noticed it.You can check this thread here
    http://askubuntu.com/questions/95136/converting-images-to-pdf-without-quality-loss/95189#comment108998_95189

  9. Quique says:

    Thanks, Abhishek D.
    The result was much better using convert’s option -quality 100 .

  10. johu says:

    Linux is great! It’s just the convert command I looked for. Thanks…

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>