PHP needs to implement pdf to image conversion
- ImagickExtension
- PHP's spatie/pdf-to-image plugin package
- 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
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。