该包提供了一个易于使用的类,用于将 PDF 转换为一张或多张图像。
您应该安装 Imagick 和 Ghostscript。有关详细信息,请参阅有关 Ghostscript 和 Imagick 的问题。
该软件包可以通过 Composer 安装,并且需要 PHP 8.2+:
composer require spatie/pdf-to-image
如果您使用的 PHP < 8.2,请使用此软件包的 2.0 版。
将 PDF 转换为图像非常简单。
$ pdf = new Spatie PdfToImage Pdf ( $ pathToPdf );
$ pdf -> save ( $ pathToWhereImageShouldBeStored );
如果传递给saveImage
文件名具有扩展名jpg
、 jpeg
、 png
或webp
,则图像将以该格式保存;否则输出格式将为jpg
。
如果保存了多个图像,则save()
方法返回一个包含已保存图像的文件名的数组,否则返回一个包含已保存图像路径的字符串。
获取pdf的总页数:
/** @var int $numberOfPages */
$ numberOfPages = $ pdf -> pageCount ();
检查文件类型是否是受支持的输出格式:
/** @var bool $isSupported */
$ isSupported = $ pdf -> isValidOutputFormat ( ' jpg ' );
默认情况下,仅渲染 PDF 的第一页。要渲染另一个页面,请调用selectPage()
方法:
$ pdf -> selectPage ( 2 )
-> save ( $ pathToWhereImageShouldBeStored ); //saves the second page
或者,使用selectPages()
方法选择多个页面:
$ pdf -> selectPages ( 2 , 4 , 5 )
-> save ( $ directoryToWhereImageShouldBeStored ); //saves the 2nd, 4th and 5th pages
更改输出格式:
$ pdf -> format ( Spatie PdfToImage Enums OutputFormat :: Webp )
-> save ( $ pathToWhereImageShouldBeStored ); //the saved image will be in webp format
将输出质量(压缩质量)设置为 0 到 100:
$ pdf -> quality ( 90 ) // set an output quality of 90 %
-> save ( $ pathToWhereImageShouldBeStored );
设置输出分辨率DPI:
$ pdf -> resolution ( 300 ) // resolution of 300 dpi
-> save ( $ pathToWhereImageShouldBeStored );
指定输出图像的缩略图大小:
$ pdf
-> thumbnailSize ( 400 ) // set thumbnail width to 400 px; height is calculated automatically
-> save ( $ pathToWhereImageShouldBeStored );
// or:
$ pdf
-> thumbnailSize ( 400 , 300 ) // set thumbnail width to 400 px and the height to 300 px
-> save ( $ pathToWhereImageShouldBeStored );
设置输出图像宽度:
$ pdf -> size ( 400 ) // set the width to 400 px; height is calculated automatically
-> save ( $ pathToWhereImageShouldBeStored );
设置输出图像的宽度和高度:
$ pdf -> size ( 400 , 300 ) // set the width to 400 px and the height to 300 px
-> save ( $ pathToWhereImageShouldBeStored );
获取 PDF 的尺寸。这可用于确定 PDF 是否具有极高分辨率。
/** @var SpatiePdfToImageDTOsPageSize $size */
$ size = $ pdf -> getSize ();
$ width = $ size -> width ;
$ height = $ size -> height ;
笔记
$directoryToWhereImagesShouldBeStored 必须是现有目录
将所有页面保存为图像:
$ pdf -> saveAllPages ( $ directoryToWhereImagesShouldBeStored );
设置Imagick的合并图层方法:
$ pdf -> layerMethod ( Spatie PdfToImage Enums LayerMethod :: Merge );
// or disable layer merging:
$ pdf -> layerMethod ( Spatie PdfToImage Enums LayerMethod :: None );
设置输出图像的背景颜色:
$ pdf -> backgroundColor ( ' white ' ) // simple text for ' white ' color
-> save ( $ pathToWhereImageShouldBeStored );
$ pdf -> backgroundColor ( ' #fff ' ) // code for ' white ' color
-> save ( $ pathToWhereImageShouldBeStored );
$ pdf -> backgroundColor ( ' rgb(255,255,255) ' ) // rgb for ' white ' color
-> save ( $ pathToWhereImageShouldBeStored );
该软件包通过 Imagick 使用 Ghostscript。为此,Ghostscripts gs
命令应该可以从 PHP 进程访问。对于 PHP CLI 进程(例如 Laravel 的异步作业、命令等),通常已经是这种情况。
然而,对于 FPM 上的 PHP(例如,当“在浏览器中”运行此包时),您可能会遇到以下问题:
Uncaught ImagickException: FailedToExecuteCommand 'gs'
可以通过在php-fpm.conf
文件末尾添加以下行并重新启动 PHP FPM 来修复此问题。如果您不确定php-fpm.conf
文件所在的位置,您可以检查phpinfo()
。如果您使用 Laravel Valet,则php-fpm.conf
文件将位于/usr/local/etc/php/YOUR-PHP-VERSION
目录中。
env[PATH] = /usr/local/bin:/usr/bin:/bin
这将指示 PHP FPM 在正确的位置查找gs
二进制文件。
如果您收到错误消息attempt to perform an operation not allowed by the security policy 'PDF'
,您可能需要将以下行添加到您的policy.xml
文件中。该文件通常位于/etc/ImageMagick-[VERSION]/policy.xml
,例如/etc/ImageMagick-7/policy.xml
。
< policy domain = " coder " rights = " read | write " pattern = " PDF " />
spatie/pdf-to-image
使用 PEST 框架进行单元测试。可以使用以下命令运行它们:
./vendor/bin/pest
请参阅变更日志以了解有关最近更改内容的更多信息。
详细信息请参阅贡献。
请查看我们的安全政策,了解如何报告安全漏洞。
麻省理工学院许可证 (MIT)。请参阅许可证文件以获取更多信息。