laravel将HTML页面转换为PDF文件——laravel-dompdf和laravel-snappy扩展包

2019-03-2821:15:41后端程序开发Comments4,390 views字数 3324阅读模式

项目中需要将HTML页面转换为PDF文件方便打印,我在网上搜了很多资料。先后尝试了laravel-dompdf和laravel-snappy两种扩展包,个人感觉laravel-snappy比较好用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

一、使用laravel-dompdf扩展包文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

1、安装扩展包文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

我们通过composer来安装文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

composer require barryvdh/laravel-dompdf

2、将ServiceProvider添加到config / 中的providers数组中文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

Barryvdh\DomPDF\ServiceProvider::class,

3、添加facade到config / 中的aliases数组中文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

'PDF' => Barryvdh\DomPDF\Facade::class,

4、使用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

可以创建一个dompdf实例并加载HTML字符串、文件或者视图,然后使用stream()方法显示在浏览器中、save()方法保存到文件或者download()方法下载。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

$pdf = App::make('');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->stream();

也可以使用门面(facade),使用前先use PDF;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

$pdf = PDF::loadView('', $data);
return $pdf->download('');

也可以链式操作文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

return PDF::loadFile(public_path().'/')->save('/path-to/')->stream('');

可以更改方向(landscape将方向设为横向,一般使用的都是竖向的,使用时注意一下)和纸张大小,并隐藏或显示错误(默认情况下,调试打开时显示错误)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

PDF::loadHTML($html)->setPaper('a4', 'landscape')->setWarnings(false)->save('')

其他的一些基本使用和配置请参考文档资料https://github.com/barryvdh/laravel-dompdf文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

5、解决中文乱码问题文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

下载一个支持unicode编码的中文字体。例如:(微软雅黑)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

建议先在storage下创建fonts文件夹,把字体放在fonts文件夹下,不然会报错。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

在css中引入字体文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

<style>
            @font-face {
                font-family: 'msyh';
                font-style: normal;
                font-weight: normal;
                src: url({{ storage_path('your_path/') }}) format('truetype');
            }
            body {
                font-family: msyh, DejaVu Sans,sans-serif;
            }
</style>

注意:亲测只有unicode编码的中文,才能正常显示,这也是我觉得这个PDF扩展不太好用的原因。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

二、使用laravel-snappy扩展包文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

1、安装依赖软件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

laravel-snappy扩展包需要Wkhtmltopdff的支持,所以先安装Wkhtmltopdf文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

方法一:下载wkhtmltopdf安装包文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

wkhtmltopdf下载地址http://www.softpedia.com/get/Office-tools/PDF/wkhtmltopdf.shtml文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

windows直接下载安装就可以了,注意安装到你知道的地方,这个路径后面有用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

我的安装在G:wk目录下文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

laravel将HTML页面转换为PDF文件——laravel-dompdf和laravel-snappy扩展包文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

在bin目录下有wkhtmltoimage与wkhtmltopdf两个依赖软件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

laravel将HTML页面转换为PDF文件——laravel-dompdf和laravel-snappy扩展包文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

linux下载解压完成后,要将文件移动到 /usr/local/bin 下文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

方法二:通过 composer 来安装文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

以linux系统为例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

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

(uname -a 命令查看系统位数)

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

Linux 下使用该第三方插件需要几个库的支持文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

apt-get install libXrender*
apt-get install libfontconfig*

2、安装laravel-snappy扩展包文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

composer require barryvdh/laravel-snappy

3、将ServiceProvider添加到config / 中的providers数组文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

Barryvdh\Snappy\ServiceProvider::class,

3、添加facade到config / 中的aliases数组中文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

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

4、生成配置文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

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

此命令会在config/生成配置文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

具体配置如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

<?php

return array(


    'pdf' => array(
        'enabled' => true,
//        'binary'  => '/usr/local/bin/wkhtmltopdf',     //依赖软件的路径,linux默认在此目录下'binary'  => 'G:\wk\wkhtmltopdf\bin\wkhtmltopdf',
        'timeout' => false,
        'options' => array(),
        'env'     => array(),
    ),
    'image' => array(
        'enabled' => true,
//        'binary'  => '/usr/local/bin/wkhtmltoimage','binary'  => 'G:\wk\wkhtmltopdf\bin\wkhtmltoimage',
        'timeout' => false,
        'options' => array(),
        'env'     => array(),
    ),


);

5、使用 ( 与使用dompdf类似 )文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

先引入文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

use PDF;
use SnappyImage;

生成PDF文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

可以使用门面(facade)加载HTML字符串、文件或者视图,然后使用stream()方法显示在浏览器中、save()方法保存到文件或者download()方法下载。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

$pdf = PDF::loadView('', $data);
return $pdf->download('');

也可以链式操作文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

return PDF::loadFile(public_path().'/')->save('/path-to/')->stream('');

可以更改方向(landscape将方向设为横向,一般使用的都是竖向的,使用时注意一下)和纸张大小,并隐藏或显示错误(默认情况下,调试打开时显示错误)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

PDF::loadHTML($html)->setPaper('a4', 'landscape')->setWarnings(false)->save('')

生成图片文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

$pdf = SnappyImage::loadView('', $data);
return $pdf->download('');

6、解决中英文乱码问题 (linux)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

可以将例如:宋体或其他中文字体添加到 /usr/share/fonts/ 里文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10535.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/10535.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定