Converting a PDF to PNG

前端 未结 12 1803
一个人的身影
一个人的身影 2020-11-27 09:58

I\'m trying to convert a PDF to a PNG image (at least the cover of one). I\'m successfully extracting the first page of the PDF with pdftk. I\'m using imagemagick to do the

相关标签:
12条回答
  • 2020-11-27 10:13

    My solution is much simpler and more direct. At least it works that way on my PC (with the following specs):

    me@home: my.folder$ uname -a
    Linux home 3.2.0-54-generic-pae #82-Ubuntu SMP Tue Sep 10 20:29:22 UTC 2013 i686 i686 i386 GNU/Linux
    

    with

    me@home: my.folder$ convert --version
    Version: ImageMagick 6.6.9-7 2012-08-17 Q16 http://www.imagemagick.org
    Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
    Features: OpenMP
    

    So, here's what I run on my file.pdf:

    me@home: my.folder$ convert -density 300 -quality 100 file.pdf file.png
    
    0 讨论(0)
  • 2020-11-27 10:16

    For a PDF that ImageMagick was giving inaccurate colors I found that GraphicsMagick did a better job:

    $ gm convert -quality 100 -thumbnail x300 -flatten journal.pdf\[0\] cover.jpg
    
    0 讨论(0)
  • 2020-11-27 10:20

    Couldn't get the accepted answer to work. Then found out that actually the solution is much simpler anyway as Ghostscript not just natively supports PNG but even multiple different "encodings":

    • png256
    • png16
    • pnggray
    • pngmono
    • ...

    The shell command that works for me is:

    gs -dNOPAUSE -q -sDEVICE=pnggray -r500 -dBATCH -dFirstPage=2 -dLastPage=2 -sOutputFile=test.png test.pdf
    

    It will save page 2 of test.pdf to test.png using the pnggray encoding and 500 DPI.

    0 讨论(0)
  • 2020-11-27 10:20

    Here is a german discussion about a problem like this for SVG files where it is solved by using

    convert -background transparent
    

    Perhaps this works for you, too.

    0 讨论(0)
  • 2020-11-27 10:21

    One can also use the command line utilities included in poppler-utils package:

    sudo apt-get install poppler-utils
    pdftoppm --help
    pdftocairo --help
    

    Example:

    pdftocairo -png mypage.pdf mypage.png
    
    0 讨论(0)
  • 2020-11-27 10:24

    To convert pdf to image files use following commands:

    For PNG gs -sDEVICE=png16m -dTextAlphaBits=4 -r300 -o a.png a.pdf

    For JPG gs -sDEVICE=jpeg -dTextAlphaBits=4 -r300 -o a.jpg a.pdf

    If you have multiple pages add to name %03d gs -o a%03d.jpg a.pdf

    What each option means:

    • sDEVICE={jpeg,pngalpha,png16m...} - filetype
    • -o - output file (%stdout to stdout)
    • -dTextAlphaBits=4 - font antialiasing.
    • -r300 - 300 dpi
    0 讨论(0)
提交回复
热议问题