Home > Article > PHP Framework > How Laravel uses the laravel-snappy package to convert HTML to PDF and PNG
The following tutorial column will show you how to convert HTML to PDF and PNG using laravel-snappy package. I hope it will be helpful to friends in need!
PrefaceSince the company wants to develop a new tool called cloud contract, it can preset several Set a fixed contract template (html page), use parameter filling method to flexibly change the data in the contract, and then generate PDF and PNGAfter checking, we found the laravel-snappy
package, which uses iswkhtmltopdf
, wkhtmltopdf
is built on QT Webkit
, and QT Webkit
is based on Webkit
Yes, the same rendering engine as our browser. ##Installation
1. First check whether the system is 32-bit or 64-bit
uname -a2. Install through composer wkhtmltopdf
32位: $ composer require h4cc / wkhtmltopdf-i386 0.12.x $ composer require h4cc / wkhtmltoimage-i386 0.12.x 64位: $ composer require h4cc/wkhtmltopdf-amd64 0.12.x $ composer require h4cc/wkhtmltoimage-amd64 0.12.x3. Next, copy the installed wkhtmltopdf to the directory of the Linux system executable command
cp vendor/h4cc/wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64 /usr/local/bin/ cp vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 /usr/local/bin/ //并使其可执行: chmod +x /usr/local/bin/wkhtmltoimage-amd64 chmod +x /usr/local/bin/wkhtmltopdf-amd644. Install the laravel-snappy package
composer require barryvdh/laravel-snappy
Configuration
1. Add ServiceProvider to the providers array in config/app.php
Barryvdh\Snappy\ServiceProvider::class,2. Add Facades to config/app. In the aliases array in php
'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class, 'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,3. Generate the configuration file
php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
Note: snappy.php in the config folder is its configuration file, and binary points to the one installed above wkhtml related path.
Use
//一、下载 $pdf = \PDF::loadView('welcome', $data); return $pdf->download('welcome.pdf'); //二、渲染页面,在浏览器中显示 $html = '<html><head><meta charset="utf-8"></head><h1>订单id</h1><h2>12346546</h2></html>'; $pdf = \PDF::loadHTML($html); return $pdf->inline();Image
//一、下载 $img = \SnappyImage::loadView('welcome', $data); return $img->download('welcome.png'); //二、渲染页面,在浏览器中显示 $html = '<html><head><meta charset="utf-8"></head><h1>订单id</h1><h2>12346546</h2></html>'; $img = \SnappyImage::loadHTML($html); return $img->inline();
Training record
1. When executing the generation, if it promptslibXrender or The libssl
library does not exist and you need to install the corresponding library.//apt-get 安装方法 apt-get install libXrender* apt-get install libssl* //yum 安装方法 yum install libXrender* yum install libssl* //若 yum 安装时最后报错 Error: GPG check FAILED yum install libXrender* --nogpgcheck yum install libssl* --nogpgcheck
2. The problem of garbled Chinese display. This problem needs to be solved by installing Chinese fonts in Linux.
View system fonts
Before starting the installation, we first check the fonts that have been installed in the system. To check the fonts that have been installed in the system, we can use the fc-listcommand to view it. If there is no such command in the system, we need to Install the relevant software packages first.
On centos, use the following command to install:yum install -y fontconfig mkfontscale
<pre class="brush:php;toolbar:false">[root@8d079be5059a php]# fc-list
/usr/share/fonts/urw-base35/NimbusMonoPS-Italic.otf: Nimbus Mono PS:style=Italic
/usr/share/fonts/urw-base35/D050000L.t1: D050000L:style=Regular
...</pre>
If you want to view the Chinese fonts that have been installed in the system For fonts, we can use the following command [root@8d079be5059a php]# fc-list :lang=zhFrom the above picture we can see that there are no Chinese fonts in the system We enter the Windows font directory
C:\Windows\Fontsand change simfang. Copy the ttf font remotely or put it in the server's font directory in other ways
//远程复制 scp simfang.ttf root@ip:/usr/share/fonts
Then create the font index information and update the font cache
cd /usr/share/fonts/mkfontscale mkfontdir fc-cache
[root@8d079be5059a php]# fc-list :lang=zh /usr/share/fonts/simfang.ttf: FangSong,仿宋:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
The above is the detailed content of How Laravel uses the laravel-snappy package to convert HTML to PDF and PNG. For more information, please follow other related articles on the PHP Chinese website!