Theres a way, using PHP exec and Image MagicK, to get the first PDF page and convert it to JPG?

How to get PDF first page and convert it to JPG


There are many search results on SO already.

You have the choice between two duplicates:

  • ImageMagick/Imagick convert PDF to JPG using native PHP API using the PHP IM bindings
  • Imagemagick convert pdf to png using the command line.

Note that for this to work, you need Ghostscript installed along with ImageMagick. (I think this usually is the case.)

To answer your question, you convert just the nth page page as follows:

convert file.pdf[n] output_file.jpg

Note that this is zero based, so for the first page you would want to use file.pdf[0].

If you wish to convert the entire file, you can do:

convert file.pdf output_file.jpg

And this will produce a bunch of files in the form of output_file-0.jpg, output_file-1.jpg, ..., output_file-n.jpg