5

I have a bunch of png's ripped from github, that look kind of like this:

enter image description here

All have a width of 912, but the heights vary.

I'm trying to convert them all to a nice readable A4 pdf, using:

convert -page A4 *.png -format pdf file.pdf

I can get them all into the pdf, but can't get the results quite right:

  • It puts them on the bottom instead of the top of the page.
  • Also, most of them fit on one page, but for the ones with lots of pixels going down, I'd like to split and crop it so it spans multiple pages while keeping a consistent size

How do I get them on the top of the page (centered, or left-justified with a bit of margin), and bonus points for splitting them?

2 Answers 2

5

ImageMagick adds pictures to a PDF at the bottom left corner. It is possible to set an offset to push it up to the top of the page. Here is your code modified to work with your example png:

convert -page A4+0+593 example.png -format pdf file.pdf

However, the vertical offset (593px here) needs to be in relation to the height of each png. You can find the height of the file (using identify xyz.png) and subtract it from 842 (the height of the A4 PDF). That will give you the necessary vertical offset for each file.

The complication for you is that your pngs are 912px wide and the A4 pdf is only 595px wide, so your png is being proportionally reduced in order to fit. You can use ImageMagick to reduce the image proportionately to 595px wide (using convert example.png -resize 595x595 reducedExample.png). Then find the new height of the reduced png, subtract that from 842, and use that number as the offset.

This helps you solve your second problem. Once you've established the height of your reduced png, you'll know if it is too tall for a page. Anything taller than 842px (or less if you include margins) will need to be chopped to fit the page using the -chop operator (see ImageMagick Usage manual for details).

2
  • 1
    Hmm, I'd like to avoid shrinking the images, want to keep them readable - can you instead set the DPI of the pdf? Dec 31, 2013 at 20:54
  • 1
    Yes, you can reset the DPI using the -density option. See Usage
    – T-Dor
    Dec 31, 2013 at 21:04
1

Instead of calculating you could use the -gravity parameter. I would suggest "-gravity north" to stick the images to the top margin or "-gravity center" to center them. Results vary depending on if the image fits properly into the page. If they are all about the same size it should work.

So if you are in the folder where all your png:s are i would do the following: convert -gravity north -page A4 *.png -format pdf output.pdf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.