Home  >  Article  >  PHP Framework  >  How Laravel uses the laravel-snappy package to convert HTML to PDF and PNG

How Laravel uses the laravel-snappy package to convert HTML to PDF and PNG

藏色散人
藏色散人forward
2020-10-14 16:14:452924browse

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!

How Laravel uses the laravel-snappy package to convert HTML to PDF and PNG

Preface

Since 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 is

wkhtmltopdf
, 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 -a
2. 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.x
3. 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-amd64

4. 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 = \PDF::loadView('welcome', $data);
return $pdf->download('welcome.pdf');

//二、渲染页面,在浏览器中显示
$html = &#39;<html><head><meta charset="utf-8"></head><h1>订单id</h1><h2>12346546</h2></html>&#39;;
$pdf = \PDF::loadHTML($html);
return $pdf->inline();
Image
//一、下载
$img = \SnappyImage::loadView(&#39;welcome&#39;, $data);
return $img->download(&#39;welcome.png&#39;);

//二、渲染页面,在浏览器中显示
$html = &#39;<html><head><meta charset="utf-8"></head><h1>订单id</h1><h2>12346546</h2></html>&#39;;
$img = \SnappyImage::loadHTML($html);
return $img->inline();

Training record

1. When executing the generation, if it prompts

libXrender 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-list

command 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

After the installation is complete, we can view the fonts already installed in the system <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=zh
From the above picture we can see that there are no Chinese fonts in the system

We enter the Windows font directory

C:\Windows\Fonts
and 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

At this point, the font has been installed . Now let’s check the Chinese fonts and see if they were installed successfully
[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!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete