简体   繁体   中英

convert pdf to images using php

i am trying to get images of a pdf file. but the problem is that my code produce error for some pdf files. probably file size problem

this is my code

$saved_file_location = "images.pdf";
$destination_dir = "t";
$Name = "123";

exec('convert -verbose -density 150 '.$saved_file_location.' -crop 1020x490+128+40 -quality 90 '.$destination_dir."/".$Name.'-%03d.jpg');
sleep(5);
//echo filesize($destination_dir."/".$Name.'-000.jpg'). "<br>";
if(filesize($destination_dir."/".$Name.'-000.jpg') < 10000)
{
    exec('convert -verbose -density 150 '.$saved_file_location.' -crop 1020x490+128+580 -quality 90 '.$destination_dir."/".$Name.'-%03d.jpg');
    sleep(5);
    //echo filesize($destination_dir."/".$Name.'-001.jpg')."<br>";
    if(filesize($destination_dir."/".$Name.'-001.jpg') < 10000)
    {
        exec('convert -verbose -density 150 '.$saved_file_location.' -crop 1020x490+128+1125 -quality 90 '.$destination_dir."/".$Name.'-%03d.jpg');
    }
}

and this error i get

PHP Warning:  filesize(): stat failed for t/123-000.jpg in /home3/domain/pdf_to_images.php on line 10

some time the code work fine and produce multiple images of pdf file but some time i get this error

First of all ImageMagick convert command internally uses ghostscript's gs command and gs is faster comparing to convert so it is always good to use ghostscript if you are not using ImageMagick specific features. and the other thing is rather than constructing the command by yourself I would recommend using library already written and tested to avoid waste of time trying to understand all the combinations of a commands.

you will find good libraries packagist.org

here a library I wrote which will be useful PdfLib who reads your question in the future

$pdflib = new ImalH\PDFLib\PDFLib();
$pdflib->setPdfPath($pdf_file_path);
$pdflib->setOutputPath($folder_path_for_images);
$pdflib->setImageFormat(\ImalH\PDFLib\PDFLib::$IMAGE_FORMAT_PNG);
$pdflib->setDPI(300);
$pdflib->setPageRange(1,$pdflib->getNumberOfPages());
$pdflib->convert();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM