LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Blogs > sag47
User Name
Password

Notices


Rate this Entry

Batch convert images to PDF

Posted 10-10-2010 at 05:48 AM by sag47
Updated 08-11-2011 at 01:10 AM by sag47

Recently (within the past year) I have helped a user with a problem converting hundreds of images into individual PDF files. I was pleasantly surprised to find imagemagick to effortlessly do this.

Command line tool imagemagick does what you need if they're images.
Installation:
Code:
sudo apt-get install imagemagick
Usage: (coverts all images into one pdf)
Code:
convert *.jpg foo.pdf
Usage: (converts each image into an individual pdf)
This is if you want .jpg to be replaced with .pdf. This command will work with any image. Replace .jpg with whatever image extension you want.
Code:
IFS=$'\n';for x in `ls -w 1 *.jpg`;do convert $x `echo $x | sed 's/\.jpg$/.pdf/'`;done
Here is the previous command but a little simpler. This is if you want the file extension to be .jpg.pdf.
Code:
IFS=$'\n';for x in `ls -w 1 *.jpg`;do convert $x $x.pdf;done
Here's your bulk solution for converting all image files to pdf individually...
Code:
IFS=$'\n';for x in `ls -w 1 *.jpg *.jpeg *.png *.gif *.tif *.tiff`;do convert $x $x.pdf;done
As you can see with a little bit of terminal ninja scripting you can get the job done relatively effortlessly. (search shell script howto in google)
Posted in Uncategorized
Views 6885 Comments 1
« Prev     Main     Next »
Total Comments 1

Comments

  1. Old Comment
    My examples listed above are a pile of crap. Here's a better way of doing the above.

    For JPG images,
    Code:
    for x in *.jpg;do convert "${x}" "${x%.jpg}.pdf";done
    The above example is better because it takes advantage of name globbing in bash for the *.jpg and when renaming the image to a PDF file it removes the .jpg extension first using bash parameter expansion.
    Posted 08-03-2013 at 09:50 AM by sag47 sag47 is offline
 

  



All times are GMT -5. The time now is 12:32 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration