Convert PDF to JPG image with PHP

前端 未结 5 1637
时光说笑
时光说笑 2020-12-18 09:40

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

相关标签:
5条回答
  • 2020-12-18 09:48

    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!

    0 讨论(0)
  • 2020-12-18 09:51
    convert -normalize yourfile.pdf[0] yourdestination.jpg
    
    0 讨论(0)
  • 2020-12-18 09:53

    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

    0 讨论(0)
  • 2020-12-18 09:59

    Try this.

    <?php
        $pdf = 'testfile.pdf';
        $save = 'output.jpg';
    
        exec('convert "'.$pdf.'" -colorspace RGB -resize 800 "'.$save.'"', $output, $return_var);
    
    ?>
    
    0 讨论(0)
  • 2020-12-18 10:10

    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();
    
    0 讨论(0)
提交回复
热议问题