January 29, 2014 2:01 pm

Make PDF to JPEG Converter in PHP

We have published an article on How to Convert HTML to PDF in PHP with fpdf and received many emails to write something on conversion from PDF to JPEG so today I am going to show you how we can convert PDF to JPEG and its a 4 lines script and very easy and simple to understand. We use PHP imagick extension which is mostly built-in in PHP installation so no need to include any thing.

How to convert PDF to JPEG in PHP

Simple PHP Code:

In this code we have to give a PDF file and in output it will produce JPEG files for each page of your given PDF file.

If you want to convert first page of your PDF file only then define PDF file name like this mytest.pdf[0] and run the script it will show convert only first page of your PDF file.

If you are using shared hosting and there is most of the time imagick extension not compiled with PHP only binaries available so here is to code to convert PDF to JPEG with imagick binaries.

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

I have used binaries to perform this task in demo and download code if you have a PHP with compiled imageick then use first code and edit it as per your requirement.

This is a very simple and easy tutorial mostly for PHP Beginners I hope you like it please write down your reviews in comments.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.

Tutorial Categories:
  • Shahbaz Ahmed Bhatti

    Php Gang Team, Please Can u Post a Tutorial Email Attatchment with (pdf, or .doc or .jpg FIle) it nice to see pdf to jpg
    but the link is not working?

    • huzoorbux

      Demo is working properly please check again.

      • Jayesh

        Demo is not working for me also.

      • nandar

        It doesn’t upload and show images? And any error doesn’t show up.Please help how to get images..

      • nandar

        demo is working properly but I download the code and run in my localhost, it doesn’t generate images.Please help me.

  • binil billu

    when checking in my localhost with pdf file. the sample code not generating the images

  • binil billu


    How to convert word files to jpeg ? .is that possible?

  • ZeeStyl

    Can you please explain which binary will be used and should point to run the “exec” command?

  • Edgarito

    hi, sorry bbut the code do not work, I have Imagick extension installed and I try many times your code but no responde…. only upload pdf file but no convert nothing… thanks..

  • k balu

    Hi ,

    I am balu… I really liked your article. I installed imagick and path is C:imagick … But i getting this error shown below. Will you please help me to solve this issue….

    • Jai Agarwal

      Do you found the solution for this ??

      • install goshscript before imagemagick

    • huzoorbux

      Turn on errors and try again.

    • Zul Azmi Harris

      did you have solution for this problem??

      • Mega Jaya

        check variable location, if you running on linux make sure path of convert imagick at /usr/local/bin/convert, so after your download code change at file index.php line 46 $location = “……”; become to $location = “/usr/local/bin/convert”; or depend on your path imagick convert

  • neetu

    in my localhost it is not working..please provide me library so i can implement

  • Tran Huy

    Hello, I can not download the source code, I subscribed, but when I download the site show that I haven’t subscribed. Please check again! Thanks

  • thanks, I am being found it!!

  • Jai Agarwal

    I installed both demo script and imagick extension on my server but after uploading it is showing blank images. Please help me how to fix it, i noticed that same problem asked below but there is answer for it.

    • huzoorbux

      Add this line on top of the pages and run it again u must get some errors then share those errors here.

      • Jai Agarwal

        It is showing PDF converted to JPEG sucessfully!! but getting blank images and i’m also not getting them on my server folder location.

        Please help me.

  • delarosa

    I can’t download the source code,i already subscribed but when i download the site show that i haven’t subscribed. Please check again..Thanks

  • lanner

    How to use this script directly without upload ? just with the name of the pdf file

  • got it

  • i have checked your tutorial i have configured my localhost xamp everything goes fine for image to image conversion but if i put .pdf file then its not working for this

    $imagick = new Imagick();

    also i need to put file path like in my code because images/ss.pdf etc its not working for images but if i put the path like above in my code then images are working but not working for pdf files thanks!

    i can see you are not replying lots of people commenting in this post may be you think we are fool to comment. If you are not going to reply back to your visitors or subscribers comment then shut this off from ur posts that please comment or comment box thanks!

  • Caff

    the problem is if my PDF as 2 page I have 2 jpg I just 1 jpg how do i make 1 jpg
    right now like this

    123-0.jpg and 123-1.jpg
    and i just need

    123.jpg no the rest I’m not a very god programmer but I try to understand what I’m doing but I lost my self so easy can any one help me with this problem
    Thank to every one.

    $pid = mysqli_insert_id($con);
    // Place image in the folder
    mkdir (“uploads/$pid”, 0755);
    $output_dir = “uploads/$pid/”;

    $ImageName = preg_replace(“/.[^.s]{3,4}$/”, “”, $ImageName);
    $NewImageName = $pid.’.’.$ImageExt;

    move_uploaded_file($_FILES[“myfile”][“tmp_name”],$output_dir. $NewImageName);

    $location = “/usr/local/bin/convert”;
    $name = $output_dir. $NewImageName;
    $nameto = $output_dir.$pid.”.jpg”;
    $convert = $location . ” ” . $name . ” “.$nameto;

    $message = “PDF converted to JPEG sucessfully!!”;

  • TTom911

    Your demo is NOT working ! Getting “success” message on your demo site bit no actual image is converted. Also in our local installation, with iMagick installed, same result: PDF uploaded but there are no converted images in the “uploads” folder. Any solution? Thanks !

  • banana

    HELP PHP GANG! the source code for the demo is working on the localhost. but when i uploaded it on my site all i see is blank images. What to do? Pls pls pls pls help 🙁

    • huzoorbux

      Turn on errors there must be some error on the page that’s why it shows a blank page.

  • Guadalupe Lip Curo ☮

    Hi, I downloaded your code and when I upload a pdf convert it into an another pdf, I don’t know if is maybe the route of the “convert.exe” or what? Please help me

  • RC

    After using your code….
    image not display or upload folder only pdf upload image not upload
    ple help i am truble

    • huzoorbux

      Try to debug it and show the errors.

      • Kerry

        I had the same problem too.

        But after I changed “exec($convert);” to “exec(“convert “.$convert);”,

        Everything works fine!!!

        • Mayur HD

          but it’s not working for me 🙁
          please help…

  • Karthick

    same problem as image not display in front of view

  • Karthick


  • Karthick

    Hai rc i can solve this pbm example at screen shots above

  • skyfuckingwalker

    Can someone show how to solve this fucking problem?
    I got stuck in this tutorial and I can’t solve it.
    Please make a video explaining >:(

    I tried to install imagick but it doesn’t work.

  • skyfuckingwalker

    Come on man, video tutorial please. There is nothing on youtube.

  • Karthick

    are you using windows or linux

  • thasneem077

    Hi nice stuff. Thank you… But i need to set resolution of the jpg image…
    Please help me

    • Karthick

      Easily set resolution of the converted image using exec(convert -density 150 -quality 100 example.pdf example.jpg) function,but -density 150 -quality 100 is optional,which resolution u will be set.

  • shafeeq

    this code is for convert pdf to image, is okey. but anyone could you help me to get php code for convert docs file to pdf file………..thanks

  • nandar

    The code can’t generate images on my localhost with codeigniter imagick library,how to do, pls help to get images.

  • Amith Dandin

    Bro, i have added your code in my website. its working fine.

    But in some case -> the converted image will look black. so unable to read that.

    so please do something.

  • Sunny Patial

    Thankyou so much brother.. its working fine for me.. excellent tutorial. great job

  • kishore Dharany

    i just tried this sample code in localhost…but the image was not display in the result page during upload…and only pdf file is upload in described directory but no jpg files…

  • Buyi Wen

    I’m not a developer, i always use this free online pdf to jpg converter

  • Buyi Wen

    I’m not a developer, i always use this free online pdf to jpg converter

  • Rk Sinha

    can we generate png image from fpdf object

  • images are getting converted to separate pdf file not image files

  • Mohd Farhan Rizwan

    Huzoor sir i’m a big fan of your. Please can you tell me how to convert Pdf to Doc using php

    Thanks in Advance.

  • Abubakar Siddique

    It’s not working fine even in demo

  • chintan vadi

    can you give me your demo project of convert pdf to Image in php.

  • Athira Ravindranathan

    Could someone please attach the code of demo url, while i click on upload button i couldnt see from view source code