Home > PHP Framework > Laravel > body text

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

藏色散人
Release: 2020-10-14 16:15:55
forward
2984 people have browsed it

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
Copy after login
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
Copy after login

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
Copy after login

4. Install the laravel-snappy package

composer require barryvdh/laravel-snappy
Copy after login

Configuration

1. Add ServiceProvider to the providers array in config/app.php

Barryvdh\Snappy\ServiceProvider::class,
Copy after login
2. Add Facades to config/app. In the aliases array in php
'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,
'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,
Copy after login

3. Generate the configuration file

php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
Copy after login

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();
Copy after login
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();
Copy after login

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
Copy after login
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
Copy after login

After the installation is complete, we can view the fonts already installed in the system <div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">Copy after login</div></div> 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
Copy after login

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
Copy after login

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
Copy after login

Then create the font index information and update the font cache

cd /usr/share/fonts/mkfontscale
mkfontdir
fc-cache
Copy after login

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
Copy after login

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template