One day I needed to make a big pdf file from many graphic files. There was no simply solution for this and I wrote this little shell script.
There are several programs, that can convert any file to pdf (for example OpenOffice). But if you have 100 separate files and you want to make a one book from them or print them, it's no so pleasant to do this manually, adding file by file to OpenOffice and then saving as a .pdf or printing. That's why I'm showing you my solution for this.
I'm using bash, so as every shell script we begin with
BASH:
Then we will do good filenames (this means filenames in lowercase with no whitespaces)
BASH:
#convert to lowercase
for i in *;
do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`;
done
#substitute whitespaces with underscores
for i in *;
do mv "$i" `echo $i | tr ' ' '_'`;
done
Note that this will work only in the current directory and will affect all the files in it. If you want to convert only the files of the certain type, substitute the * with your file extensions mask (for example *.jpg in my case). If you have images in format other than jpeg, you should convert them to jpeg first, for example with ImageMagick:
BASH:
for i in *.png;
do convert $i `basename $i .png`.jpg;
done
Of course you can extend this with your own filetypes or convertors, or add more automatic. For example you might want to resize your images to reduce the disk usage:
BASH:
for i in *.png;
do convert -resize 800 $i `basename $i .png`_resized.jpg;
done
This converts images to jpg, additionally resizes their width to the 800 pixels and maintains the height automatically.
But what must be done in any case - all the files must be converted to .ps. Use jpeg2ps:
BASH:
for i in *.jpg;
do jpeg2ps $i > `basename $i .jpg`.ps;
done
For further actions we have at least two ways. If your end format is pdf, you can directly use GhostScript. If you want to have a book in ps format, you can use psmerge or GhostScript. So I've choosed the GhostScript because it's more multi-purpose:
BASH:
gs -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=joined.pdf *.ps -c quit
or use this for ps conversion
BASH:
gs -dNOPAUSE -sDEVICE=pswrite -sOutputFile=joined.ps *.ps -c quit
or read the
man page of gs to convert in other formats.
If you have used psmerge or want additionally make a pdf book from ps, use this:
BASH:
Thus now you have a large book instead of many files and can easy print or read it.