409

I have requirement of converting PDF pages to images. There is a background image with some text in my file, and when I save it as an image only the background image gets saved.

Is there any software available for the same so that complete page can be converted to an image?

12 Answers 12

526

You can use pdftoppm to convert a PDF to a PNG:

pdftoppm input.pdf outputname -png

This will output each page in the PDF using the format outputname-01.png, with 01 being the index of the page.

Converting a single page of the PDF

pdftoppm input.pdf outputname -png -f {page} -singlefile

Change {page} to the page number. It's indexed at 1, so -f 1 would be the first page.

Specifying the converted image's resolution

The default resolution for this command is 150 DPI. Increasing it will result in both a larger file size and more detail.

To increase the resolution of the converted PDF, add the options -rx {resolution} and -ry {resolution}. For example:

pdftoppm input.pdf outputname -png -rx 300 -ry 300
| improve this answer | |
  • 28
    Thank you so much. Much better quality than with imagemagick or graphicsmagick! – dAnjou Jan 9 '13 at 0:18
  • 10
    pdftoppm is much faster than convert – zuo Nov 6 '13 at 4:52
  • 6
    This is really much better than imagemagick. Imagemagick actually changed the colors in an unexpected way in my case! – NoBackingDown Sep 17 '15 at 7:16
  • 23
    this is good!, but it's a bit easier to write -r 300 instead of specifying the x and y resolutions independently when you want to set them to the same value. – mlc Oct 20 '15 at 14:52
  • 4
    I had much more success with pdftoppm than with imagemagick. – Michael Hays Apr 14 '18 at 7:30
323
  1. Install imagemagick.

  2. Using a terminal where the PDF is located:

    • For the full document:

      convert -density 150 input.pdf -quality 90 output.png
      
    • For a single page:

      convert -density 150 input.pdf[666] -quality 90 output.png
      

Whereby:

  • PNG, JPG or (virtually) any other image format can be chosen.

  • -density xxx will set the DPI to xxx (common are 150 and 300).

  • -quality xxx will set the compression to xxx for PNG, JPG and MIFF file formates (100 means no compression).

  • [666] will convert only the 667th page to PNG (zero-based numbering so [0] is the 1st page).

  • All other options (such as trimming, grayscale, etc.) can be viewed on the website of Image Magic.

| improve this answer | |
  • 2
    The answer as is does work but the resolution is very poor. Therefore not currently an answer that is useful. Maybe if convert has some parameters that can be specified this could change. – Elijah Lynn Jan 16 '15 at 19:57
  • 50
    This answer is much higher quality askubuntu.com/a/50180/11929 – Elijah Lynn Jan 16 '15 at 20:06
  • 6
    You can change the density by adding the -density 300 parameter – OHLÁLÁ Apr 1 '15 at 12:18
  • 4
    So can anybody confirm that specifying density makes it "as good" as the other answers here, or not? Also as a note to followers, ImageMagick calls out to "ghostscript" to actually convert from pdf to png ex: gs -q NOPROMPT ...-sDEVICE=pngalpha -r150x150 -sOutputFile=/var/tmp/Yf%d -f/var/tmp/L -f/var/tmp/Fic1 and if you get convert: no images defined output.png it means you don't have ghostscript installed... – rogerdpack Mar 3 '17 at 17:29
  • 6
    Parsing PDF in imagemagick has been disabled - bugs.archlinux.org/task/59778 - it can be enabled manually by editing /etc/ImageMagick-7/policy.xml file and removing PDF from <policy domain="coder" rights="none" pattern="{PS,PS2,PS3,EPS,PDF,XPS}" /> – Jezor Dec 10 '18 at 18:58
28

IIRC GIMP is capable of using PDFs, i.e. converting them into images. So if you want to edit the images right away - GIMP is your friend.

| improve this answer | |
  • GIMP can indeed open PDFs, each page as one layer. Choosing "Export As" seems to save only the current layer, but you can easily delete the layer after exporting and run "Export As" again. – Dan Dascalescu Aug 12 '19 at 8:34
  • As of the current respective versions packaged in Ubuntu 19.04, I found GIMP produces a much higher quality conversion than imagemagick. – durette Dec 1 '19 at 18:38
15

The currently accepted answer does the job but results in an output which is larger in size and suffers from quality loss.

The method in the answer given here results in an output which is comparable in size to the input and doesn't suffer from quality loss.

TLDR - Use pdfimages : pdfimages -j input.pdf output

Quoting the linked answer:

It's not clear what you mean by "quality loss". That could mean a lot of different things. Could you post some samples to illustrate? Perhaps cut the same section out of the poor quality and good quality versions (as a PNG to avoid further quality loss).

Perhaps you need to use -density to do the conversion at a higher dpi:

convert -density 300 file.pdf page_%04d.jpg

(You can prepend -units PixelsPerInch or -units PixelsPerCentimeter if necessary. My copy defaults to ppi.)

Update: As you pointed out, gscan2pdf (the way you're using it) is just a wrapper for pdfimages (from poppler). pdfimages does not do the same thing that convert does when given a PDF as input.

convert takes the PDF, renders it at some resolution, and uses the resulting bitmap as the source image.

pdfimages looks through the PDF for embedded bitmap images and exports each one to a file. It simply ignores any text or vector drawing commands in the PDF.

As a result, if what you have is a PDF that's just a wrapper around a series of bitmaps, pdfimages will do a much better job of extracting them, because it gets you the raw data at its original size. You probably also want to use the -j option to pdfimages, because a PDF can contain raw JPEG data. By default, pdfimages converts everything to PNM format, and converting JPEG > PPM > JPEG is a lossy process.

So, try

pdfimages -j file.pdf page

You may or may not need to follow that with a convert to .jpg step (depending on what bitmap format the PDF was using).

I tried this command on a PDF that I had made myself from a sequence of JPEG images. The extracted JPEGs were byte-for-byte identical to the source images. You can't get higher quality than that.

| improve this answer | |
  • 2
    This is the incorrect solution for the OPs question if the PDF is a print-ready PDF created by something like Illustrator or Acrobat, since pdfimages extracts only the images from the PDF but does not flatten each entire page and export the full pages to images. – GuyPaddock May 14 at 15:22
  • @GuyPaddock Thanks for pointing it out. – Anmol Singh Jaggi May 15 at 8:00
10

If your pdfs are scanned, the images are already stored as part of pdf. you will simply need to extract them with pdfimages:

pdfimages my-file.pdf prefix 
| improve this answer | |
  • 3
    This is the perfect solution for scanned pdfs, as with this you can, with one command, extract the original jpgs, and without further recompressions. – Jose Gómez Jan 31 '16 at 22:49
  • This is the incorrect solution for the OPs question if the PDF is a print-ready PDF created by something like Illustrator or Acrobat, since pdfimages extracts only the images from the PDF but does not flatten each entire page and export the full pages to images. – GuyPaddock May 14 at 15:22
3

If you only want to convert a specific page of a PDF to a PNG, you can pipe pdftk to convert (described above) like this:

pdftk document.pdf cat 12 output - | convert - document-page-12.png
| improve this answer | |
3

To get a single page from gm convert, add [N] (with N the page number starting at 0) to the PDF name, ie gm convert foo.pdf[11] out.png to get the 12th page from the PDF.

For pdftoppm use -f N -singlefile, where N is the page number starting at 1, ie pdftoppm -f 12 -singlefile foo.pdf out for the same result. It appears to always add ".png" to the output filename and there is no way to stop this.

| improve this answer | |
2

You can use convert and specify a higher density using -density option.

eg. convert -d 300 foo.pdf bar.png

| improve this answer | |
  • can you explain more about what is density and what It can do? – rɑːdʒɑ Jul 24 '14 at 4:03
  • 1
    @AgentCool It specifies the horizontal and vertical image density (in ppi). – Arjun Jul 27 '14 at 10:02
2

pdftocairo file.pdf -png (was posted by Anthony Ebert as a comment at How to convert PDF to image?)

| improve this answer | |
1

Master PDF Editor (ver 2.2) has this option built in. Open the PDF file and then go to File > Export to > Images. It presents a dialog where you can define different options for the output. Extremely useful. Hope this info helps.

| improve this answer | |
  • Is that in the free or paid version? In my version, the option is greyed out? Does that mean I need to pay? Is there a paid version? – Joshua Robison Sep 7 '17 at 2:25
0

You can do this with ghostscript:

gs -dSAFER -dBATCH -dNOPAUSE -r300 -sDEVICE=png16m -dFirstPage=1 -dLastPage=1 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sOutputFile=output.png input.pdf

See https://www.ghostscript.com/doc/9.52/Devices.htm for details

| improve this answer | |
0

PDF Mod also allows exporting images of all or individual pages of PDF files.

  • Open PDF file in PDF Mod
  • Select page(s)-
  • Edit > Export image(s)
| improve this answer | |

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