40

I want to convert multi page pdfs into single page images efficiently.

I already know how to do this one page at a time with imagemagick. For example,

convert x.pdf[2] x3.jpg

will give me the 3rd page of the pdf as an image. So if I figure out how many pages are in the pdf using identify then I can loop through and convert all pages in the pdf to images. This method can however take a while. For example a 15 page pdf could take anywhere between 15-30 seconds.

According to answers that I have seen elsewhere (also on the imagemagick forums) the following imagemagick command should split a pdf into multiple images.

convert x.pdf x-%0d.jpg

but all this ends up doing is creating the first page named x-0.jpg

As an alternative I have tried using pdftk with the burst capability. The problem I faced there is that burst does not work in all cases. It does for some pdf's and does not for some others.

Any suggestions on how to improve things would help.

My OS is Mac OSX Lion but I do need this working on CentOS 6 as well.

fmw42
  • 46,825
  • 10
  • 62
  • 80
Moiz Raja
  • 5,612
  • 6
  • 40
  • 52
  • This answer works perfectly when I directly use ghostscript - http://stackoverflow.com/questions/11002982/converting-multi-page-pdfs-to-several-jpgs-using-imagemagick-and-or-ghostscript?rq=1 so why would imagemagick not work? – Moiz Raja Jun 10 '13 at 13:58
  • ImageMagick convert works as expected on CentOS. At this time it seems to be an issue on the Mac. – Moiz Raja Jun 11 '13 at 04:12
  • this seem to output only the first page of the pdf, I'm on mac – 2cupsOfTech Sep 23 '14 at 13:11
  • 1
    The command works fine for me on my Mac (assuming you really have a multipage PDF) and you include the number of zeros you want, such as %02d or just %d without any zeros. The other issue is likely that you either do not have Ghostscript installed in the ImageMagick delegates or you need to upgrade Ghostscript as a delegate to ImageMagick. What is your ImageMagick version `convert -version` and does it list gs or gslib? What is the version of Ghostscript `gs --version`. – fmw42 Jul 16 '18 at 17:15

4 Answers4

56

You're missing the quantity of digits. Use:

convert x.pdf x-%04d.jpg

Where 4 means 4 digits will be show on the page count.

MrLore
  • 3,759
  • 2
  • 28
  • 36
beak42
  • 676
  • 5
  • 5
  • 7
    When using `-flatten`, the parameter is expanded, but only one image is generated, N pages at 1/Nth opacity - useless for PDF, which are paginated for a reason, but maybe it makes sense in other contexts. `-flatten` seemed necessary because it was filling with gray by default (why not black or white? Clearly a 'compromise'.) and would not accept `-transparency {black,...,white}` as suggested by `zsh` completion, and for whatever reason this used a different background color when flattening. Anyway, your method is correct, just use `-transparent-color` if this is an issue. – John P Mar 31 '18 at 22:46
7

If you use Graphicsmagick on Debian or ImageMagick on macOS you probably have to add ADJOIN to your command. So it should look like

convert x.pdf +adjoin x-%04d.jpg
Ric
  • 79
  • 1
  • 3
7

When I tried to convert my multi-page pdf, the resulting image files had a gray background despite the pdf having a white background. (@John P commented on it on the accepted answer, but I couldn't get his comment to directly work for me.)

Here's what worked for me to make the background white:

convert -authenticate yourpassword -background white -alpha remove -alpha off -density 300 -quality 80 -verbose "Your file.pdf" "Your file.png"

My pdf had a password hence the authenticate. You can see a summary of the options here:

-authenticate value decipher image with this password

-background color background color

-alpha on, activate, off, deactivate, set, opaque, copy", transparent, extract, background, or shape the alpha channel

-density geometry horizontal and vertical density of the image

-quality value JPEG/MIFF/PNG compression level

-verbose print detailed information about the image

More detail: https://imagemagick.org/script/convert.php

And the alpha remove option: http://www.imagemagick.org/Usage/masking/#alpha_remove

User
  • 62,498
  • 72
  • 186
  • 247
0

Ran into the same issue. Reinstall Imagemagick to work in Mountain Lion. If you use brew the simply

  $brew unlink imagemagick
  $brew install imagemagick
aarti
  • 2,815
  • 1
  • 23
  • 31