4

I am using ImageMagik to try and convert the contents of a PDF to JPG, but keep getting an empty jpg. I have made sure the perms are 777 on everything for testing so I am a little lost how to continue.

Here is the script I am running

<?php

    exec('convert testfile.pdf output.jpg', $output, $return_var);

?>
1
  • You may need to specify the full path to convert. Is your input and output in the same directory as where you run convert? Check the $output variable for error messages? Does Imagemagick include delegates Ghostscript and libjpeg?
    – fmw42
    Sep 11, 2017 at 18:04

5 Answers 5

6

Try this.

<?php
    $pdf = 'testfile.pdf';
    $save = 'output.jpg';

    exec('convert "'.$pdf.'" -colorspace RGB -resize 800 "'.$save.'"', $output, $return_var);

?>
2
  • To my recent experiences ghostScript is more faster than convert command, try this complete library I wrote which does this super fast github.com/imalhasaranga/PDFBox Sep 5, 2016 at 10:45
  • What are the values $output, $return_var
    – V.P
    Feb 26, 2019 at 12:21
1

Use the absolute path to the binary, like this:

exec('/usr/bin/convert testfile.pdf output.jpg', $output, $return_var);

But make sure your convert binary is actually on /usr/bin you can check that out with the following command:

which convert

1
convert -normalize yourfile.pdf[0] yourdestination.jpg
0

ImageMagick internally use GhostScript and Generally the conversion of ImageMagick is slow Comparing to Ghoastscript, so If you are only interested on getting convert pdf to images then Ghostscript gs command is faster. below is an sample wrapper around Ghostscript which I wrote few days back.

PDFLib-Php

$pdflib = new ImalH\PDFLib\PDFLib();
$pdflib->setPdfPath($pdf_file_path);
$pdflib->setOutputPath($folder_path_for_images);
$pdflib->setImageQuality(95);
$pdflib->setDPI(300);
$pdflib->setPageRange(1,$pdflib->getNumberOfPages());
$pdflib->convert();
2
  • No, that isn't PDFBox. The correct URL for PDFBox is pdfbox.apache.org . Sep 12, 2016 at 15:48
  • what is your problem mate ? PDFBox is for java and this for php and the question is asking how to do it in php no in java Sep 13, 2016 at 3:19
0

Here you have my solution. Use Imagick directly in your php code.

Convert all PDF pages to JPG

 // create Imagick object
 $imagick = new Imagick();
 // Reads image from PDF
 $imagick->readImage('file.pdf');
 // Writes an image
 $imagick->writeImages('converted.jpg', false);

Convert specific PDF page to JPG

 // create Imagick object
 $imagick = new Imagick();
 // Read image from PDF
 $imagick->readImage('test.pdf[0]');
 // Writes an image
 $imagick->writeImages('converted_page_one.jpg');

Another way to deal with this problem is to use spatie/pdf-to-image library.

Cheers!

2
  • trying to convert pdf to image and got error, Imagick extension enabled , please help me Fatal error: Uncaught ImagickException: UnableToOpenBlob 'sample.pdf': No such file or directory @ error/blob.c/OpenBlob/3315 in E:\XAMPP\htdocs\pdf\convert\index.php:6 Stack trace: #0 E:\XAMPP\htdocs\pdf\convert\index.php(6): Imagick->readimage('sample.pdf') #1 {main} thrown in E:\XAMPP\htdocs\pdf\convert\index.php on line 6 Apr 18, 2019 at 12:54
  • again Fatal error: Uncaught ImagickException: PDFDelegateFailed `The system cannot find the file specified. ' @ error/pdf.c/ReadPDFImage/794 in E:\XAMPP\htdocs\pdf\convert\index.php:8 Stack trace: #0 E:\XAMPP\htdocs\pdf\convert\index.php(8): Imagick->readimage('E:/XAMPP/htdocs...') #1 {main} thrown in E:\XAMPP\htdocs\pdf\convert\index.php on line 8 Apr 18, 2019 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.