1

PHP needs to implement pdf to image conversion

    1. ImagickExtension
    1. PHP's spatie/pdf-to-image plugin package
    1. Ghostscript software

One: Imagick expansion installation

Reference: Imagick extension installation for PHP

Two: php spatie/pdf-to-image plugin package installation

 composer require spatie/pdf-to-image

Three: Ghostscript software installation

1: Ghostscript download address: Ghostscript download address

2: Install Ghostscript

 tar -xzf ghostscript-9.56.1.tar.gz
cd ghostscript-9.56.1
./configure
make && make install

3: Configure Ghostscript

Modify the /etc/ImageMagick-6/policy.xml file
(1): Modify the line pattern="{PS,PDF,XPS}" to

 <policy domain="module" rights="read|write" pattern="{PS,PDF,XPS}" />

(2): Modify pattern="PDF" to

 <policy domain="coder" rights="read|write" pattern="PDF" />

Four: Example of converting pdf to picture

 $pdfPath = 'XXX';//PDF地址
$pdfToImg = new \Spatie\PdfToImage\Pdf($pdfPath);
$pages = $pdfToImg->getNumberOfPages();
$fullPath = 'XXX';//图片保存地址
$imgs = [];
for ($i = 1; $i <= $pages; $i++) {
    $imgFile =$i . '.png';
    $pdfToImg->setPage($i)->saveImage($fullPath . '/' . $imgFile);
    $imgFiles[] =  $fullPath . $imgFile;
}

return $imgFiles;//图片地址数组

According to the above, pdf can be converted into pictures, and multiple pdfs will be converted into multiple pictures


huaweichenai
673 声望114 粉丝