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.

Be Sociable, Share!

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

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

20 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…

  11. Pingback: Converter múltiplas imagens em um arquivo PDF | maregito

  12. Gillesg06 says:

    Here is again, a perfect reason I’m into Linux now. Thanks

  13. Bill says:

    Thanks! The one liner: “convert *.jpg figures.pdf;” didn’t work.
    Too many files… Created the script and it worked perfectly!

  14. pieneman1 says:

    I tried the convert line. Trying to convert 100 jpg’s into 1 pdf. It works but it puts the jpg’s in a random order. They are called 1.jpg 2.jpg …. 99.jpg. But when I look in the pdf it starts with 1.jpg and then the order is wrong. Anyone knows a SIMPLE sollution :)

  15. Sopherl says:

    This works in principle fine, but does anyone know why the convert command produces blank pages out of my png images? Is this only supposed to work with jpegs?

  16. matt says:

    @pieneman1, problem probably is withing naming You used. Try to rename: 1.jpg->01.jpg, 2.jpg->02.jpg and so on.
    then redo the convert command

  17. Anandharaj says:

    Thank you so much for this useful information.
    It works fine

  18. Alen says:

    Try to use online tools. I have found recently: convert-my-image.com>.
    May be will be interesting for somebody

  19. Alan says:

    Hi, im a newbie and trying to put multiple jpg’s onto one pdf document. im having difficulty with understanding the first steps above. What does this mean:

    1 cd FOLDER_WITH_IMAGES
    2 convert *.jpg images.pdf

    Does it mean i firstly type ‘cd’ into the terminal then drag and drop the folder i want to put onto one pdf (the folder with the images)? Then i press enter and go onto step 2 which involves typing “convert *.jpg images.pdf” into the terminal?
    Thanks

  20. Francesc says:

    @Alan, you need to substitute FOLDER_WITH_IMAGES with the path of your folder, not drag it to the terminal.
    If you don’t know what the path is you might want pressing ctrl+L in order to get its location, copy that with ctrl+c and paste it into the terminal with ctrl+shift+V
    Alternatively you can install a useful util for nautilus (assuming you run gnome or unity) that will allow you to open the terminal in any folder with a right click. In a terminal type:
    sudo apt-get install nautilus-open-terminal
    logout and login again or restart nautilus with the following commands (all your nautilus windows will be closed down):
    killall nautilus
    nautilus&

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>