You are on page 1of 4

Converting multiple images into one PDF on Linux | tail -f... http://dancingpenguinsoight.com/2010/02/converting-m...

tail -f findings.out

Converting multiple images into one PDF on Linux


Posted on February 21, 2010 by Samuel Huckins

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? Heres 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. Heres 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!

Tweet

Tetszik

10

Share

StumbleUpon

No related posts.
This entry was posted in CLI and tagged images, Linux, PDF. Bookmark the permalink.

1/4

2013-09-11 12:00

Converting multiple images into one PDF on Linux | tail -f... http://dancingpenguinsoight.com/2010/02/converting-m...

20 Responses to Converting multiple images into one PDF on Linux


Montebelo says:
July 20, 2010 at 11:47 am

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

Samuel Huckins says:


July 21, 2010 at 5:50 am

@Montebelo Im 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!

andreas says:
February 5, 2011 at 8:37 pm

what about the single line: convert *.jpg figures.pdf; or am I missing something here? take care

Samuel Huckins says:


February 6, 2011 at 12:37 pm

@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 dont at all need as your example shows. Ill rework the post shortly, thanks very much for the correction!

A. Syukri says:
August 17, 2011 at 7:11 pm

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?

Samuel Huckins says:


August 18, 2011 at 7:07 am

@A. Syukri: Thanks for reporting that! Ive added the older version into the post. Note: It doesnt handle case-variability in file extensions right now, so will try to add that soon.

2/4

2013-09-11 12:00

Converting multiple images into one PDF on Linux | tail -f... http://dancingpenguinsoight.com/2010/02/converting-m...

Enriqueto says:
December 3, 2011 at 4:27 pm

Amazing!

Abhishek D says:
February 5, 2012 at 12:05 am

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

Quique says:
February 14, 2012 at 1:55 pm

Thanks, Abhishek D. The result was much better using converts option -quality 100 .

johu says:
May 19, 2012 at 2:40 am

Linux is great! Its just the convert command I looked for. Thanks

Pingback: Converter mltiplas imagens em um arquivo PDF | maregito

Gillesg06 says:
September 28, 2012 at 7:05 am

Here is again, a perfect reason Im into Linux now. Thanks

Bill says:
September 30, 2012 at 2:38 pm

Thanks! The one liner: convert *.jpg figures.pdf; didnt work. Too many files Created the script and it worked perfectly!

pieneman1 says:
December 2, 2012 at 11:36 am

I tried the convert line. Trying to convert 100 jpgs into 1 pdf. It works but it puts the jpgs 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

Sopherl says:
January 17, 2013 at 3:42 pm

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?

3/4

2013-09-11 12:00

Converting multiple images into one PDF on Linux | tail -f... http://dancingpenguinsoight.com/2010/02/converting-m...

matt says:
January 28, 2013 at 5:36 am

@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

Anandharaj says:
February 15, 2013 at 1:58 am

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

Alen says:
March 9, 2013 at 4:40 pm

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

Alan says:
May 10, 2013 at 3:59 am

Hi, im a newbie and trying to put multiple jpgs 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

Francesc says:
May 15, 2013 at 3:56 am

@Alan, you need to substitute FOLDER_WITH_IMAGES with the path of your folder, not drag it to the terminal. If you dont 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&

tail -f findings.out, by Samuel Huckins | Theme: variant of twentyten. | Site license


Proudly powered by WordPress.

4/4

2013-09-11 12:00

You might also like