TheRogueWolf
TheRogueWolf

Reputation: 353

Use ImageMagick to combine multiple PNGs into single PDF

I have a number of same-sized PNG files which I want to convert into a single PDF, with 3 PNG images to an A4 page (e.g. 30 images = 1 x 10 page PDF).

The images are all 1311 x 537 pixels and I'd like to stack them 3 to an A4 page. The filenames have no scheme but I don't mind the order they appear in the PDF.

Could anyone advise the best way to achieve this using ImageMagick (on Windows 10), please?

Upvotes: 6

Views: 9630

Answers (3)

mayeulk
mayeulk

Reputation: 148

To run Linux commands on Windows 10, you can use WSL or other solutions. Then you can do:

convert *.png temp.pdf ; pdfxup -x 1 -y 3 --portrait -ps a4 -o 3_by_page.pdf temp.pdf

-x 1 -y 3 means "1 column, 3 rows". In addition to ImageMagick, you need pdfxup from Debian package texlive-extra-utils.

Upvotes: -1

stefansson
stefansson

Reputation: 579

Go to the target directory where your images are settled.

convert *.png  Desired_Name.pdf
# All the images will combine into the declared *.pdf file
# ImageMagick.x86_64    6.9.12.61-1.el8 

Upvotes: 2

Mark Setchell
Mark Setchell

Reputation: 207445

At its most basic, without any Powershell or BATCH commands, you can stack 3 images one above the other, without spaces and resize to an A4 page like this:

convert image1.jpg image2.jpg image3.jpg -append -resize 2480x3508 page-01.png
convert image4.jpg image5.jpg image6.jpg -append -resize 2480x3508 page-02.png
...
...

Then combine all the pages into a PDF like this:

convert page-* result.pdf

If you have more time and patience, you can space the images out a little, or automate the process, but it may not be worth the effort.

If you want to space the images by, say 10, pixels, you can create transparent spacers in between like this:

convert -background none -size 10x10 image1.png xc:none image2.png xc:none image3.png -append -resize 2480x3508 page-01.png

In case any more Windows-y (TM) folk look at this, and feel like converting, you could do it like this on Linux/Unix:

Generate a file with all the filenames you want in the PDF, 3 to a line:

ls image*.jpg | xargs -n3 > files.txt

which gives:

i-1.png i-10.png i-11.png
i-12.png i-13.png i-14.png
i-15.png i-16.png i-17.png
i-18.png i-19.png i-2.png
i-20.png i-21.png i-22.png
i-23.png i-24.png i-25.png
i-26.png i-27.png i-28.png
i-29.png i-3.png i-30.png
i-4.png i-5.png i-6.png
i-7.png i-8.png i-9.png

I presume you would use DIR /B | something on Windows for that.

Then read that file, line by line and make a page of A4 sending it to stdout for a final convert to assemble into a PDF:

while read names ; do
   convert $names -append -resize 2470x3500 +repage miff:-
done < files.txt | convert miff:- result.pdf

I presume you would use FOR /F ... to do that on Windows, something like this mess:

FOR /F %%G IN (files.txt) DO (
   convert %%G -append -resize 2470x3500 +repage miff:-
) | convert miff:- result.pdf

Upvotes: 11

Related Questions