Home > database >  How to convert png to pdf with A4 dimensions using imagemagick?
How to convert png to pdf with A4 dimensions using imagemagick?

Time:01-23

I'm trying to use imagemagick cli in combination with powershell to convert a png image into pdf. I would like for the resulting pdf to be A4 landscape.

This is what I have come up with so far:

magick overview.png -page a4 -rotate -90 overview.pdf

Unfortunately my resulting pdf is not A4, being approximately 4x5 inches.

I'm not sure what I'm doing wrong here, since based on the docs a4 is a valid value for the -page parameter.

How can I convert the png to a pdf with A4 dimensions using the -page parameter?

CodePudding user response:

-page does not resize the image.

In ImageMagick, since about 7.1.0.13 or later, you can do

magick input -set option:dims "%[papersize:A4]" -resize "%[dims]" output

Or simpler,


magick input -resize "%[papersize:A4]" output

in order to resize to a given page size by name, such as A4.

(The name "dims" is not required. You can call it anything you want)

CodePudding user response:

This was no walk in the park, but it turns out that I had to account for the density, width and length of the original image. The density is basically the resolution, and had to be converted to adjust to the correct dimensions. In Powershell:

$width = 2400
$length = $width*21/29.7
$density = $width/29.7# pixel/cm
$resize = "$($width)x$($length)\!"
magick overview.png -density $density -resize $resize overview.pdf
  •  Tags:  
  • Related