How to Convert a PDF to JPEG using PHP

Hey, Today I would like to show you how we can convert PDF to JPEG using imagick extension. Imagick is a native php extension to create and modify images using the ImageMagick API, which is mostly built-in in PHP installation so no need to include any thing. ImageMagick software suite allow us to create, read, edit, and compose bitmap images easily.

PHP – Convert all PDF pages to JPEG

Using following simple example you can convert all pages of PDF to JPEG images.

As you are seeing, you have to pass a PDF file and it will produce JPEG files for each page of your given PDF file as output. writeImages() function second parameter is false, so it will not join the images, means it will produce image sequence(create images for each page) Example – converted-0.jpg, converted-1.jpg.

How to Convert a PDF to JPEG using PHP

PHP – Convert specific PDF page to JPEG

If you want to convert specific page for example first page of your PDF file only then define PDF file name like this myfile.pdf[0] and run the script it will show convert only first page of your PDF file. Following is the example –

PHP – Convert specific PDF page to JPEG with quality

If you need better quality, try adding $imagick->setResolution(150, 150); before reading the file. setResolution() must be called before loading or creating an image.

If you experience transparency problems when converting PDF to JPEG (black background), try flattening your file:

shared hosting – Convert a PDF to JPEG using PHP

Most of the shared hosting providers do not compile imagick extension with PHP, but imagick binaries will be available, so here is the code to convert PDF to JPEG with imagick binaries.

You have to change binaries location (/usr/local/bin/convert) to your server location which you can get from your hosting admin.

Lots of things can be done with Imagick extension, explore more about it at – http://php.net/manual/en/book.imagick.php

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

20 Comments

  1. Hey Arjun,

    I used to convert pdf to jpg, i went good but recently i got one issue with converting images from pdf
    and also it’s getting error like below

    **** Warning: File has an invalid xref entry: 32. Rebuilding xref table.

    **** This file had errors that were repaired or ignored.

    **** The file was produced by:

    **** >>>> Mac OS X 10.10.5 Quartz PDFContext <<<<

    **** Please notify the author of the software that produced this

    **** file that it does not conform to Adobe's published PDF

    **** specification.

  2. Fatal error: Uncaught exception ‘ImagickException’ with message ‘UnableToOpenBlob `file1.pdf’: No such file or directory @ error/blob.c/OpenBlob/2702′ in C:xampphtdocswebbleutrypdftohtmldemo1.php:19 Stack trace: #0 C:xampphtdocswebbleutrypdftohtmldemo1.php(19): Imagick->readimage(‘file1.pdf’) #1 {main} thrown in C:xampphtdocswebbleutrypdftohtmldemo1.php on line 19

    I found this error

      1. $imagick = new Imagick();
        // Reads image from PDF
        $imagick->readImage(‘file1.pdf[0]’);
        // Writes an image or image sequence Example- converted-0.jpg, converted-1.jpg
        $imagick->writeImages(‘converted.jpg’, false);

  3. Uncaught exception ‘ImagickException’ with message ‘PDFDelegateFailed `The system cannot find the file specified. ‘ @ error/pdf.c/ReadPDFImage/801’ in C:xampphtdocstesttestPDF handlerMagicimageimagic.php:14 Stack trace: #0 C:xampphtdocstesttestPDF handlerMagicimageimagic.php(14): Imagick->__construct(‘htdocs\testtest…’) #1 {main} thrown in C:xampphtdocstesttestPDF handlerMagicimageimagic.php on line 14

  4. i got this error pdf to jpg convertion
    Fatal error: Uncaught exception ‘ImagickException’ with message ‘PDFDelegateFailed `The system cannot find the file specified. ‘ @ error/pdf.c/ReadPDFImage/801’ in E:xampphtdocshtml2_imagehtml2image.php:27 Stack trace: #0 E:xampphtdocshtml2_imagehtml2image.php(27): Imagick->readimage(‘E:/xampp/htdocs…’) #1 {main} thrown in E:xampphtdocshtml2_imagehtml2image.php on line 27

      1. Hi i installed Ghostscript
        i run code
        Initial Directory Listing: dir /o/a:-d
        Volume in drive E has no label.
        Volume Serial Number is 6C73-D760

        Directory of E:xampphtdocshtml2_image

        31-01-2018 17:09 3,709 html2image.php
        04-05-2013 00:11 2,953 html2image_demo.php
        25-01-2018 11:43 275,518 kumar.pdf
        31-01-2018 14:42 2,137 temp.pdf
        4 File(s) 284,317 bytes
        0 Dir(s) 405,017,812,992 bytes free
        Execute command: C:Program Files (x86)ImageMagick-7.0.7-Q8convert temp.pdf -resize x100 test7.jpg
        New Directory Listing: dir /o/a:-d
        Volume in drive E has no label.
        Volume Serial Number is 6C73-D760

        Directory of E:xampphtdocshtml2_image

        31-01-2018 17:09 3,709 html2image.php
        04-05-2013 00:11 2,953 html2image_demo.php
        25-01-2018 11:43 275,518 kumar.pdf
        31-01-2018 14:42 2,137 temp.pdf
        4 File(s) 284,317 bytes
        0 Dir(s) 405,017,812,992 bytes free
        No image file create

  5. Thanks for taking the time for making this tutorial, really appreciated. I used to convert PDF to JPEG with Acethinker PDF Converter Lite, free and works pretty well for me, share it here.

  6. Hello Arjun,

    when i trying this code than its showing this error

    $imagick = new Imagick();
    // Sets the image resolution
    $imagick->setResolution(150, 150);
    // Reads image from PDF
    $imagick->readImage(‘data.pdf[0]’);
    // Writes an image
    $imagick->writeImages(‘converted_page_one.jpg’);

    Error

    Fatal error: Uncaught Error: Class ‘Imagick’ not found in /opt/lampp/htdocs/PRINT/pdftojpg.php:3 Stack trace: #0 {main} thrown in /opt/lampp/htdocs/PRINT/pdftojpg.php on line 3

  7. An outstanding share! I have just forwarded this onto a friend
    who has been doing a little homework on this. And he actually bought me lunch due to the fact that I found it for him…
    lol. So allow me to reword this…. Thank YOU for the
    meal!! But yeah, thanks for spending time to talk about this
    subject here on your web page.

Leave a Reply

Your email address will not be published.