How to convert a PDF into JPG with command line in

2020-07-06 02:59发布

问题:

What are fast and reliable ways for converting a PDF into a (single) JPEG using the command line on Linux?

回答1:

You can try ImageMagick's convert utility.

On Ubuntu, you can install it with this command:

$ sudo apt-get install imagemagick

Use convert like this:

$ convert input.pdf output.jpg


回答2:

For the life of me, over the last 5 years, I cannot get imagemagick to work consistently (if at all) for me, and I don't know why people continually recommend it again and again. I just googled how to convert a PDF to a JPEG today, found this answer, and tried convert, and it doesn't work at all for me:

$ convert in.pdf out.jpg
convert-im6.q16: not authorized `in.pdf' @ error/constitute.c/ReadImage/412.
convert-im6.q16: no images defined `out.jpg' @ error/convert.c/ConvertImageCommand/3258.

Then, I remembered there was another tool I use and wrote about, so I googled "linux convert pdf to jpg Gabriel Staples", clicked the first hit, and scrolled down to my answer. Here's what works perfectly for me:

  1. [Produces ~1MB-sized files per pg] Output in .jpg format at 300 DPI:

    mkdir -p images && pdftoppm -jpeg -r 300 mypdf.pdf images/pg
    
  2. [Produces ~2MB-sized files per pg] Output in .jpg format at highest quality (least compression) and still at 300 DPI:

    mkdir -p images && pdftoppm -jpeg -jpegopt quality=100 -r 300 mypdf.pdf images/pg
    
  3. If you need more resolution, you can try 600 DPI:

    mkdir -p images && pdftoppm -jpeg -r 600 mypdf.pdf images/pg
    
  4. ...or 1200 DPI:

    mkdir -p images && pdftoppm -jpeg -r 1200 mypdf.pdf images/pg
    

See the references below for more details and options.

References:

  1. Convert PDF to image with high resolution
  2. https://askubuntu.com/questions/150100/extracting-embedded-images-from-a-pdf/1187844#1187844


回答3:

Convert from imagemagick seems do a good job:

convert file.pdf test.jpg

and in case multiple files were generated:

convert test-0.jpg --append test-1.jpg ... --append one.jpg

to generate a single file, where all pages are concatenated.



回答4:

libvips can convert PDF -> JPEG quickly. It comes with most linux distributions, it's in homebrew on macos, and you can download a windows binary from the libvips site.

This will render the PDF to a JPG at the default DPI (72):

vips copy somefile.pdf somefile.jpg

You can use the dpi option to set some other rendering resolution, eg.:

vips copy somefile.pdf[dpi=600] somefile.jpg

You can pick out pages like this:

vips copy somefile.pdf[dpi=600,page=12] somefile.jpg

With this benchmark image, I see:

$ /usr/bin/time -f %e vips copy r8.pdf[page=3,dpi=300] x.jpg
0.77

And with ImageMagick it's:

$ /usr/bin/time -f %e convert -density 300 r8.pdf[3] x.jpg
3.04

So libvips is about 4x faster (on this test).