Ubuntu – convert several png to one pdf

convertpdfpng

I have 280 pgn files that I want to merge and convert into one pdf.

PNG files names are in the sequence p000.png, p001.png etc and total space is about 30mb.

I did:

convert p00*.png to myfile.pdf 

and it worked fine. Luckly enough in the right order.

But when I do:

conver p*.png to myfile.pdf

It consumes all my 4Gb of memory and half my swap. Then it crashes with the error

convert: memory allocation failed `myfile.pdf' @ error/pdf.c/WritePDFImage/1595.

Any idea of how to make a workaround to this problem?

I think that a good enough solution would be to use convert one file at a time to pdf, with the same name. Then use pdfunite or pdftk to merge them. But I don't know how to use convert in this way.

Best Answer

Imagemagick seems to be very violent with memory usage. A suggestion is to use -limit to limit the memory size convert will use, it should use a disk cache for anything else it needs. Read an explanation here:

http://www.imagemagick.org/pipermail/magick-users/2002-March/001285.html

And here's how to use the -limit option : http://www.imagemagick.org/script/command-line-options.php#limit

If not, you can do it like you describe, converting each image individually and then merging them with some other tool. Something like

for name in p*.png; do convert $name `basename $name .png`.pdf; done

should do it.

Basically it iterates over all the png files, runs convert on them, the only magic is the basename $name .png part, which will be replaced by the filename minus the .png part, so that when attaching .pdf extension to it, the filename makes sense.

Try it for yourself so you get a feel of what basename does:

basename something.png .png
echo `basename something.png .png`.pdf