以下由Laravel教學專欄為大家介紹如何使用Laravel-snappy匯出PDF,希望對需要的朋友有幫助!
前言
論壇裡有很多關於如何使用Laravel-snappy 的文章,但是很多都停留在安裝、基本範例,對於實際的應用既沒有說明用法,也沒有解答一些問題和疑惑,因此在此整理一下,權當做個記錄吧。
安裝
以ubuntu 為例
1.執行安裝wkhtmltopdf:
composer require h4cc/wkhtmltopdf-amd64 0.12.x composer require h4cc/wkhtmltoimage-amd64 0.12.x
顧名思義,分別安裝的是wkhtmltopdf 和wkhtmltoimage。
2. 複製wkhtmltopdf 到ubuntu 執行指令的目錄中
sudo cp vendor/h4cc/wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64 /usr/local/bin/ sudo cp vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 /usr/local/bin/ //并使其可执行: sudo chmod +x /usr/local/bin/wkhtmltoimage-amd64 sudo chmod +x /usr/local/bin/wkhtmltopdf-amd64
3. 安裝laravel-snappy
composer require barryvdh/laravel-snappy
4. 將ServiceProvider 新增至config/app.php 中的providers 陣列中
Barryvdh\Snappy\ServiceProvider::class
5. 將Facades 加入到config/app.php 中的aliases 陣列中
'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class, 'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,
6. 執行產生設定檔
php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
可以看到預設的設定檔為config/snappy.php:
return [ 'pdf' => [ 'enabled' => true, 'binary' => env('WKHTML_PDF_BINARY', '/usr/local/bin/wkhtmltopdf'), 'timeout' => false, 'options' => [], 'env' => [], ], 'image' => [ 'enabled' => true, 'binary' => env('WKHTML_IMG_BINARY', '/usr/local/bin/wkhtmltoimage'), 'timeout' => false, 'options' => [], 'env' => [], ], ];
注意,這裡有個坑,預設binary 配置為/usr/local/bin/wkhtmltopdf 和/usr/local/bin/wkhtmltoimage,在第一個次使用的時候,會報錯/usr/local/bin/wkhtmltopdf不存在,這是因為在linux 系統下,wkhtmltopdf 和wkhtmltoimage 的真實路徑和名稱為:/usr/local/bin/wkhtmltopdf-amd64 和/usr/ local/bin/wkhtmltoimage-amd64。
因此,需要把設定資訊修改為:
'pdf' => [ ... 'binary' => env('WKHTML_PDF_BINARY', '/usr/local/bin/wkhtmltopdf-amd64'), ... ], 'image' => [ ... 'binary' => env('WKHTML_IMG_BINARY', '/usr/local/bin/wkhtmltoimage-amd64'), ... ],
開始使用
//使用方法1 $pdf = \PDF::loadView('welcome', $data); return $pdf->download('welcome.pdf'); //使用方法2 $html = '<html><head><meta charset="utf-8"></head><h1 id="订单id">订单id</h1><h2 id="">12346546</h2></html>'; $pdf = \PDF::loadHTML($html); return $pdf->inline();
很多部落格裡沒有提到,使用方法1中,會報這樣的錯誤:
The exit status code '1' says something went wrong: stderr: "Loading pages (1/6) [> ] 0% [======> ] 10% QSslSocket: cannot resolve CRYPTO_num_locks QSslSocket: cannot resolve CRYPTO_set_id_callback QSslSocket: cannot resolve CRYPTO_set_locking_callback QSslSocket: cannot resolve sk_free QSslSocket: cannot resolve sk_num QSslSocket: cannot resolve sk_pop_free QSslSocket: cannot resolve sk_value QSslSocket: cannot resolve SSL_library_init QSslSocket: cannot resolve SSL_load_error_strings QSslSocket: cannot resolve SSLv3_client_method QSslSocket: cannot resolve SSLv23_client_method QSslSocket: cannot resolve SSLv3_server_method QSslSocket: cannot resolve SSLv23_server_method QSslSocket: cannot resolve X509_STORE_CTX_get_chain QSslSocket: cannot resolve OPENSSL_add_all_algorithms_noconf QSslSocket: cannot resolve OPENSSL_add_all_algorithms_conf QSslSocket: cannot resolve SSLeay QSslSocket: cannot call unresolved function CRYPTO_num_locks QSslSocket: cannot call unresolved function CRYPTO_set_id_callback QSslSocket: cannot call unresolved function CRYPTO_set_locking_callback QSslSocket: cannot call unresolved function SSL_library_init QSslSocket: cannot call unresolved function SSLv23_client_method QSslSocket: cannot call unresolved function sk_num [==================> ] 31% QSslSocket: cannot call unresolved function SSLv23_client_method QSslSocket: cannot call unresolved function SSL_library_init [============================================================] 100% Counting pages (2/6) [============================================================] Object 1 of 1 Resolving links (4/6) [============================================================] Object 1 of 1 Loading headers and footers (5/6) Printing pages (6/6) [> ] Preparing [============================================================] Page 1 of 1 Done Exit with code 1 due to network error: UnknownNetworkError QSslSocket: cannot call unresolved function CRYPTO_num_locks QSslSocket: cannot call unresolved function CRYPTO_set_id_callback QSslSocket: cannot call unresolved function CRYPTO_set_locking_callback " stdout: "" command: /usr/local/bin/wkhtmltopdf-amd64 --lowquality '/tmp/knp_snappy612c3edcdfc855.21787864.html' '/tmp/knp_snappy612c3edcdfce49.80482557.pdf'.
執行:
sudo apt-get update sudo apt install libssl1.0-dev
修復完成,匯出welcome 頁面。
如果使用 save () 方法儲存,預設儲存到 /public 資料夾下,如果檔案名稱相同的情況下,第二次儲存會提示檔案已經存在。
以上是一文講解Laravel-snappy如何導出PDF的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Laravel10,releasedonFebruary7,2023,isthelatestversion.Itfeatures:1)Improvederrorhandlingwithanewreportmethodintheexceptionhandler,2)EnhancedsupportforPHP8.1featureslikeenums,and3)AnewLaravel\Promptspackageforinteractivecommand-lineprompts.

thelatestlaravelververversionenhancesdevelopmentwith:1)簡化的inimpliticmodelbinding,2)增強EnhancedeloquentcapabibilitionswithNewqueryMethods和3)改善了supportorfortormodernphpfortornphpforternphpfeatureserslikenamedargenamedArgonedArgonsemandArgoctess,makecodingMoreftermeforefterMealiteFficeAndEnjoyaigaigaigaigaigaiganigaborabilyaboipaigyAndenjoyaigobyabory。

你可以在laravel.com/docs找到最新Laravel版本的發布說明。 1)發布說明提供了新功能、錯誤修復和改進的詳細信息。 2)它們包含示例和解釋,幫助理解新功能的應用。 3)注意新功能的潛在復雜性和向後兼容性問題。 4)定期審查發布說明可以保持更新並激發創新。

theessentialToolSforStayingConnectedIndistributedTeamSinclude:1)CommunicationToolSlikeZoom,Microsoftteams,Slack和DiscordForeFforeffectiveCommunication; 2)ProjectManagementtoolsSuchastrello,Asana,Asana和JirafortaskManagementAngeandWorkFloworGanization jiralagementtoolssuchastrello,Asana和Jiraforterforloganization;

Laravel通過簡化Web開發過程和提供強大功能脫穎而出。其優勢包括:1)簡潔的語法和強大的ORM系統,2)高效的路由和認證系統,3)豐富的第三方庫支持,使得開發者能專注於編寫優雅的代碼並提高開發效率。

laravelispredminandermanthandermanthandermanthandermanthermanderframework,設計Forserver-SideLogic,databasemagement,andapideplupment,thryitalsosupportsfortfortsfrontenddevelopmentwithbladeTemplates。

Laravel和Python在性能和可擴展性方面的表現各有優劣。 Laravel通過異步處理和隊列系統提升性能,但受PHP限制在高並發時可能有瓶頸;Python利用異步框架和強大的庫生態系統表現出色,但在多線程環境下受GIL影響。

Laravel適合團隊熟悉PHP且需功能豐富的項目,Python框架則視項目需求而定。 1.Laravel提供優雅語法和豐富功能,適合需要快速開發和靈活性的項目。 2.Django適合複雜應用,因其“電池包含”理念。 3.Flask適用於快速原型和小型項目,提供極大靈活性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)