Convert PDF to PNG using ImageMagick

138,467

Solution 1

when you set the density to 96, doesn't it look good?

when i tried it i saw that saving as jpg resulted with better quality, but larger file size

Solution 2

Reducing the image size before output results in something that looks sharper, in my case:

convert -density 300 a.pdf -resize 25% a.png

Solution 3

To get high quality, one should do "supersampling" in Imagemagick. Convert at a high density, but then resize down as needed (nominal enough to compensate for the high density).

convert -density 288 input.pdf -resize 25% output.png

288=72*4 (72 dpi is default density, so 4x)
25%=1/4

So the 1/4 compensates for the 4x.

Solution 4

convert -density 192 input.pdf -quality 100 -alpha remove output.png

for pdf text document is good enough. -density 192 double 96dpi, higher just make bigger image and file size -quality 100 somehow this give slightly smaller file size -alpha remove to remove png transparent background

Share:
138,467

Related videos on Youtube

StackOverflowNewbie
Author by

StackOverflowNewbie

Updated on December 28, 2021

Comments

  • StackOverflowNewbie
    StackOverflowNewbie over 2 years

    using ImageMagick, what command should i use to convert a PDF to PNG? I need highest quality, smallest file size. this is what I have so far (very slow by the way):

    convert -density 300 -depth 8 -quality 85 a.pdf a.png
    

    Looking at what Gmail does when a user "view" a PDF, the quality is awesome and the file size very minimal. The DPI is just 96 (I have to set a density of 300 to get anything decent). Anyone know how GMail does it? Thanks.

    • ch271828n
      ch271828n about 8 years
      Using density is also the solution of the converted image too blur.
    • bers
      bers over 7 years
      I need highest quality, smallest file size. At the same time? Impossible. Welcome to the real world!
    • A. Go
      A. Go almost 3 years
      I guess he meant maximum quality with minimum file size possible or as sharp as original quality without adding unnecessary artifacts that increase file size without making image better. In that case, try convert -density 192 input.pdf -quality 100 -alpha remove output.png somehow -quality 100 may lower the file size. 192 double 96dpi is good enough, and -alpha remove to remove transparent png background.
  • Avi Pinto
    Avi Pinto about 14 years
    just tried convert -density 96 -quality 85 a.pdf a.png and the results are very good
  • anderstood
    anderstood over 9 years
    This methods worked for me while the accepted answer did not. In particular, the resulting thin lines are now OK while they were hugely aliased before.
  • Squirrel in training
    Squirrel in training almost 7 years
    Doesn't work for me it claims it doesn't find the file.
  • Squirrel in training
    Squirrel in training almost 7 years
  • Squirrel in training
    Squirrel in training almost 7 years
    And ran the command in the commandline. #shiftenteringishard
  • Andreas
    Andreas over 5 years
    What is the full command line for this? When I try to run this on a Windows machine it's running the Windows "convert" command.
  • Andreas
    Andreas over 5 years
    On Windows you need to run magick convert -density ... because "convert" is a Windows system utility.
  • Matt Manuel
    Matt Manuel almost 4 years
    on Windows preface the "convert" command with "magick" to get it to work. magick convert -density 300 -depth 8 -quality 85 a.pdf a.png
  • Tom
    Tom about 3 years
    @Andreas Or you can just use full path, eg. C:\MyFiles\convert -density ...
  • A. Go
    A. Go almost 3 years
    Disagree, density 96 is not good enough for pdf text doucment. And jpg may result worse result for pdf text document; jpg make larger file size with artifacts and worse quality for text. png however, result better in smaller file size and text quality; Use -alpha remove to remove png transparent background.
  • fmw42
    fmw42 almost 3 years
    72 dpi is default for PDF not necessarily screen. See stackoverflow.com/questions/36905337/…. Feel free to use any density and fraction you want.
  • Geppettvs D'Constanzo
    Geppettvs D'Constanzo over 2 years
    This works great, nevertheless I am not using the -resize 25% as it makes the text blurry and some images suffer some kind of compression, pixelated.