Converting a multi page pdf to multiple pages using a single command
Asked Answered
V

4

44

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.

Verdellverderer answered 10/6, 2013 at 13:50 Comment(4)
This answer works perfectly when I directly use ghostscript - #11003482 so why would imagemagick not work?Verdellverderer
ImageMagick convert works as expected on CentOS. At this time it seems to be an issue on the Mac.Verdellverderer
this seem to output only the first page of the pdf, I'm on macAgree
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.Advocacy
V
59

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.

Vachell answered 1/7, 2014 at 23:34 Comment(2)
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.Fireback
When using jpg as a target the result was very blury. even when adding -quality 100 and -density 300. When using png as a target the text was readable. So I ended up using: convert x.pdf -background white -alpha remove -alpha off -density 300 -quality 100 x-%04d.png alpha necessary or page was transparent and unreadable on dark themed apps.Vedis
E
9

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
Epidote answered 16/7, 2018 at 11:1 Comment(2)
"My OS is Mac OSX Lion but I do need this working on CentOS 6 as well." So I'm afraid he is not working on Debian :-)Mall
Didn't need to add this on macOS today.Wigfall
S
8

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

Sunflower answered 15/5, 2020 at 1:32 Comment(0)
G
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
Glassy answered 12/12, 2013 at 0:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.