html转图片 laravel,基于laravel-snappy pdf微服务 html转pdf html转图片

本文介绍了如何在 Laravel 框架中使用 laravel-snappy 扩展包来将 HTML 转换为 PDF 和图片。详细步骤包括:1) 安装 Laravel 和 laravel-snappy 相关依赖;2) 配置 snappy;3) 创建 PDF 和图片的方法。此外,还提到了支持中文和设置 PDF 属性的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、通过composer下载laravel框架

composer create-project --prefer-dist laravel/laravel laravel-pdf

cd laravel-pdf

安装依赖yum install libXrender*

yum install libssl*

yum install libXext*

yum  install libfontconfig*

二、新增barryvdh/laravel-snappy  、h4cc/wkhtmltoimage-amd64、h4cc/wkhtmltopdf-amd64 包文件

"barryvdh/laravel-snappy": "^0.4.6",

"h4cc/wkhtmltoimage-amd64": "0.12.x",

"h4cc/wkhtmltopdf-amd64": "0.12.x",

1588003693383718.png

执行composer update

三、配置laravel-snappy 扩展包

打开config/app.php  将  Barryvdh\Snappy\ServiceProvider::class,  添加到 provider数组中

1588004057782497.png

添加facade到config / app.php中的aliases数组中

'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,

'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,

1588004171367139.png

四、通过pushlish 发布pdf配置

php artisan vendor:publish

1588004450291660.png

打开 /config/snappy.php 修改成如下配置:

63a7055d2789c4cf764c0a11eb626df8.png

'pdf' => [

'enabled' => true,

'binary' => base_path('vendor' . DIRECTORY_SEPARATOR . 'h4cc' . DIRECTORY_SEPARATOR . 'wkhtmltopdf-amd64'

. DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'wkhtmltopdf-amd64'),

'timeout' => false,

'options' => [],

'env'     => [],

],

'image' => [

'enabled' => true,

'binary' => base_path('vendor' . DIRECTORY_SEPARATOR . 'h4cc' . DIRECTORY_SEPARATOR . 'wkhtmltoimage-amd64'

. DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'wkhtmltoimage-amd64'),

'timeout' => false,

'options' => [],

'env'     => [],

],

五、生成pdf

为了方便,使用功能测试进行pdf文件生成:

1、首先创建PdfTest测试类

[root@iz25vpndgurz laravel-pdf]# php artisan make:test PdfTest

Test created successfully.

1588005821693155.png

2、创建pdfCreate方法

public function testPdfCreate(){

$htmlContent=<<

html>

h1{

color:red;

}

.hh{

color: aqua;

font-weight: bold;

}

title h1

this is content

hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

TOT;

$pdf = app('snappy.pdf.wrapper')->loadHTML($htmlContent)->setOption('dpi', 300)->setOption('margin-bottom','10')

->setOption('margin-left','0')->setOption('margin-right','0')->setOption('margin-top','10')

->setOption('page-offset','1')->setOption('page-height','100');

$res = $pdf->save('aa.pdf',true);

$this->assertIsBool(true);

}

执行 PdfTest 中的 testPdfCreate方法./vendor/bin/phpunit tests/Feature/PdfTest --filter testPdfCreate

1588007030286322.png

1588007244893939.png

sz  aa.pdf 下载到本地

1588007308805837.png

支持中文:

将字体文件上传至 /usr/share/fonts

9769bcf0f903349c58ac5383f7152013.png

./vendor/bin/phpunit tests/Feature/PdfTest --filter testPdfCreate

1588037397900796.png

效果如图:

f5aacaa9ed691931f63a2b8254c1d3ab.png

六、生成图片public function testImgCreate(){

$htmlContent=<<

html>

h1{

color:red;

}

.hh{

color: aqua;

font-weight: bold;

}

title h1 我是标题   这是生成的图片

this is content

hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh图片图片hhhhhhhhhhh

TOT;

$pdf = app('snappy.image.wrapper')->loadHTML($htmlContent);

$res = $pdf->save('aa.jpg',true);

$this->assertIsBool(true);

}

./vendor/bin/phpunit tests/Feature/PdfTest --filter testImgCreate

1588037303769960.png

4b0f517b516a48413d61ec3d90cad3fc.png

1588037548870229.png

七、编写方法通过url形式生成pdf文件

使用接口的方式访问,返回生成对应pdf文件的路径

针对目录下的文件,开启脚本,对文件进行隔天删除,否则占用空间。

文件查找 推荐一个包: symfony/finder

我的博客另一篇文章:

安装 laravel-snappy 扩展包 生成pdf、图片文件

踩坑经验- 中文字体支持

http://www.xiaosongit.com/index/detail/id/865.html

pdf其他属性:

/**

* -options: array:132 [

* "collate" => null

* "no-collate" => null

* "cookie-jar" => null

* "copies" => null

* "dpi" => null

* "extended-help" => null

* "grayscale" => null

* "help" => null

* "htmldoc" => null

* "ignore-load-errors" => null

* "image-dpi" => null

* "image-quality" => null

* "license" => null

* "log-level" => null

* "lowquality" => true

* "manpage" => null

* "margin-bottom" => null

* "margin-left" => null

* "margin-right" => null

* "margin-top" => null

* "orientation" => null

* "page-height" => null

* "page-size" => null

* "page-width" => null

* "no-pdf-compression" => null

* "quiet" => null

* "read-args-from-stdin" => null

* "readme" => null

* "title" => null

* "use-xserver" => null

* "version" => null

* "dump-default-toc-xsl" => null

* "dump-outline" => null

* "outline" => null

* "no-outline" => null

* "outline-depth" => null

* "output-format" => null

* "allow" => null

* "background" => null

* "no-background" => null

* "bypass-proxy-for" => null

* "cache-dir" => null

* "checkbox-checked-svg" => null

* "checkbox-svg" => null

* "cookie" => null

* "custom-header" => null

* "custom-header-propagation" => null

* "no-custom-header-propagation" => null

* "debug-javascript" => null

* "no-debug-javascript" => null

* "default-header" => null

* "encoding" => null

* "disable-external-links" => null

* "enable-external-links" => null

* "disable-forms" => null

* "enable-forms" => null

* "images" => null

* "no-images" => null

* "disable-internal-links" => null

* "enable-internal-links" => null

* "disable-javascript" => null

* "enable-javascript" => null

* "javascript-delay" => null

* "keep-relative-links" => null

* "load-error-handling" => null

* "load-media-error-handling" => null

* "disable-local-file-access" => null

* "enable-local-file-access" => null

* "minimum-font-size" => null

* "exclude-from-outline" => null

* "include-in-outline" => null

* "page-offset" => null

* "password" => null

* "disable-plugins" => null

* "enable-plugins" => null

* "post" => null

* "post-file" => null

* "print-media-type" => null

* "no-print-media-type" => null

* "proxy" => null

* "proxy-hostname-lookup" => null

* "radiobutton-checked-svg" => null

* "radiobutton-svg" => null

* "redirect-delay" => null

* "resolve-relative-links" => null

* "run-script" => null

* "disable-smart-shrinking" => null

* "enable-smart-shrinking" => null

* "ssl-crt-path" => null

* "ssl-key-password" => null

* "ssl-key-path" => null

* "stop-slow-scripts" => null

* "no-stop-slow-scripts" => null

* "disable-toc-back-links" => null

* "enable-toc-back-links" => null

* "user-style-sheet" => null

* "username" => null

* "viewport-size" => null

* "window-status" => null

* "zoom" => null

* "footer-center" => null

* "footer-font-name" => null

* "footer-font-size" => null

* "footer-html" => null

* "footer-left" => null

* "footer-line" => null

* "no-footer-line" => null

* "footer-right" => null

* "footer-spacing" => null

* "header-center" => null

* "header-font-name" => null

* "header-font-size" => null

* "header-html" => null

* "header-left" => null

* "header-line" => null

* "no-header-line" => null

* "header-right" => null

* "header-spacing" => null

* "replace" => null

* "cover" => null

* "toc" => null

* "disable-dotted-lines" => null

* "toc-depth" => null

* "toc-font-name" => null

* "toc-l1-font-size" => null

* "toc-header-text" => null

* "toc-header-font-name" => null

* "toc-header-font-size" => null

* "toc-level-indentation" => null

* "disable-toc-links" => null

* "toc-text-size-shrink" => null

* "xsl-style-sheet" => null

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值