該套件提供了一個易於使用的類,用於將 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)。請參閱許可證文件以獲取更多資訊。