andres marrugo

Convert Image to PDF With Automator and Imagemagick

I often use Imagemagick for image manipulation1. However, for simple conversion of an image to another format or similar things I have to go to the terminal and type something like the following

convert somefile.png somefile.pdf

Which is quite simple, and much better than opening preview -> export2->select pdf-> and hit save. But typing that for more than one image is also tedious. So, I decided I’d better create a service that would essentially do the following.

It would take the selected finder items, mainly images in png, jpeg, or whatever, and convert all of them to pdf. Simple. The automator service is shown in the following image. It does exactly what I’ve described.

to-pdf-automator-script
Automator service.

The shell script is quite simple. Because the convert command from Imagemagick is not a command in bash, I have to call it with the whole path. The rest of that one-liner is a sed call to substitute whatever extension the input file has (with the regex \..*) to .pdf, and that’s it. Here’s the script.

convert-to-pdf
1
2
3
4
5
for f in "$@"
do
  /opt/local/bin/convert "$f" `echo "$f"|sed "s/\..*/.pdf/"`
done

  1. As a matter of fact, now that I’ve finally upgraded to Mountain Lion, Imagemagick stopped working. Had to reinstall with macports.

  2. “Save as” in the old days, prior to OS X 10.7.

Comments