Convert PDF to images in 1920×1080

conversionimagemagickpdfpng

i want to convert a PDF to PNG images using convert.
The images must fit the 1920×1080 ratio by having a ?x1080 ratio, and have the best quality.

Here are many options i can use with convert :
https://imagemagick.org/script/command-line-options.php#append


  • First i tried the following command line :

convert my.pdf -geometry 1920x1080 -size 1920x1080 -density 1920x1080 my_resized_pdf.png

The result of the command gives me an image with the good geometry (763×1080), but a low quality i don't want to get.

enter image description here

  • I use convert command line without the geometry parameter as following :

convert my.pdf -size 1920x1080 -density 1920x1080 my_resized_pdf.png

The quality of the result is exactly what i want but the resolution is not 1920×1080 ratio, but 842×595. Its does not exactly fit on height the 1920×1080 ratio.

enter image description here

Is it possible to get PNG images with a ?x1080 ratio and with a 100% quality from a PDF ?
Or is 842×595 the biggest ratio to get a 100% quality image ?
Should i set a DPI option to convert ?

Best Answer

This involves some trial & error and in the end, it's debatable which result you might consider to be the "best result". So allow me to just give some generic advice:

  • use the -flatten option to get rid of transparent background. The transparency makes it hard to judge actual quality of the result. If you need the transparency in the final image, you can remove -flatten once you're sure of the quality.

  • use something like -density 300 to get a high DPI result. The main issue with convert is that it uses a very low density by default (72 DPI). This parameter has to be specified before the input file.

  • downscaling a high DPI image might cause additional blur, so perhaps calculating the correct DPI value to achieve the desired resolution is the way to go:

    $ convert -density 100 file.pdf -flatten file100.png
    $ file file100.png
    file100.png: PNG image data, 827 x 1169, 8-bit colormap, non-interlaced
    $ echo $((1080*10000/1169))
    9238
    $ convert -density 92.38 file.pdf -flatten file9238.png
    $ file file9238.png
    file9238.png: PNG image data, 764 x 1080, 8-bit colormap, non-interlaced
    

I'm not sure if there is a way to have convert determine "ideal" DPI value by itself.

If you take this question to the ImageMagick IRC channel or forum, I'm sure you'd get some more advice. It helps if you provide the link to the PDF file you're working with. ;)

You can also improve quality in other ways, for example by trimming empty borders away. You're losing a lot of resolution if half of the page is white. There are even solutions that re-wrap PDF text to get the most out of available screenspace (e.g. k2pdfopt).

Finally, also try other programs. This is a matter of opinion, but I prefer using Inkscape or GhostScript directly. ImageMagick has characters "glued together", Inkscape has a more balanced result, and GhostScript allows you to render a blur-free pure pixel image (if that's something you like - use pngalpha for the blurry version, which is virtually identical to convert).

ImageMagick:

ImageMagick

Inkscape:

Inkscape

GhostScript:

gs -r92.38 -sDEVICE=png48 -sOutputFile=ghostscript.png file.pdf

GhostScript

Related Question