Laravel PDF 导出

导读:本篇文章讲解 Laravel PDF 导出,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

网上推荐的两个laravel pdf 导出,分别是 laravel-dompdf 和 laravel-snappy,其实两个都好强大,不过我最终选择了一个小众的mpdf niklasravnsborg/laravel-pdf。

laravel-dompdf 对带有中文的数据不友好,中文乱码的,按照教程提示,下载一个支持中文的字体,但是依然有部分字体是乱码的,不明原因,所以最后放弃了

laravel-snappy 看了教程在使用前要安装wkhtmltopdf,心想以后要迁移到服务器上可能会麻烦,所以直接忽略。

mpdf 测试了一下,完全满足自己的工作需求!

以下是使用方法:

安装:

composer require niklasravnsborg/laravel-pdf

配置:

/config/app.php,添加如下两条配置信息:

'providers' => [

    niklasravnsborg\LaravelPdf\PdfServiceProvider::class
]
 
'aliases' => [
    'PDF' => niklasravnsborg\LaravelPdf\Facades\Pdf::class

]

在config下添加pdf.php配置文件

<?php
return [
    /*
     * Logging
     * This will log to a file in storage_path('logs/pdf.log')
     * To use Laravel default logging set to default
     * Comment out or set to null to disable logging
     */
    'logging'           => 'custom',
    // ConstructorParams
    'mode'              => 'zh-cn',    // 中文显示
    'format'            => 'A4',
    'default_font_size' => 0,
    'default_font'      => '',
    'margin_left'       => 5,
    'margin_right'      => 5,
    'margin_top'        => 5,
    'margin_bottom'     => 5,
    'margin_header'     => 9,
    'margin_footer'     => 9,
    'orientation'       => 'P',
    /**
     * Set custom temporary directory
     * The default temporary directory is vendor/mpdf/mpdf/tmp
     *
     * @see https://mpdf.github.io/installation-setup/folders-for-temporary-files.html
     * Comment the following line to keep the temporary directory
     */
    'tempDir' => storage_path('app/pdf/tmp'), // absolute path
    /**
     * Custom Fonts
     *  1) Add your custom fonts to one of the directories listed under
     * 'fontDir' in which Mpdf will look for fonts
     *
     * 2) Define the name of the font and its configuration under the array 'fontdata'
     *
     * @see https://mpdf.github.io/fonts-languages/fonts-in-mpdf-7-x.html
     */
    // List of Directories Containing fonts (use absolute path)
    'fontDir'           => [
        storage_path('app/pdf/fonts'), // absolute path
        // add extra directory here
    ],
    // Fonts Configurations
    'fontdata'          => [
        // font name should be lower-case and contain no spaces
        'customfontname' => [
            'R'          => 'RegularCustomFont.ttf',
            'B'          => 'BoldCustomFont.ttd',
            'useOTL'     => 255,
            'useKashida' => 75,
        ],
        // lower-case and contain no spaces
        'arabic-font' => [
            'R'          => 'Montserrat-Arabic-Regular.ttf',
            'B'          => 'Montserrat-Arabic-Bold.ttf',
            'useOTL'     => 255,
            'useKashida' => 75,
        ],
        'shabnam' => [
            'R'          => 'Shabnam.ttf',
            'useOTL'     => 255,
            'useKashida' => 75,
        ],
    ],
];

通过模版文件渲染PDF

use PDF;

$data = ['111', '222'];
$pdf = PDF::loadView('pdf', ['data'=>$data]);
// 直接在页面渲染显示PDF
return $pdf->stream();
// 直接下载生成的PDF文件
return $pdf->download('test.pdf');

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/97977.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!