laravel导出PDF

安装

composer require carlos-meneses/laravel-mpdf

请将Provider和Facade添加到您的config/app.php

'providers' => [
    // ...
    Meneses\LaravelMpdf\LaravelMpdfServiceProvider::class
]
'aliases' => [
    // ...
    'PDF' => Meneses\LaravelMpdf\Facades\LaravelMpdf::class
]

config

在config目录下创建pdf.php文件,添加配置

<?php

return [
    'mode'                 => 'zh-cn',
    'format'               => 'A4',
    'default_font_size'    => '12',
    'default_font'         => 'sans-serif',
    'margin_left'          => 10,
    'margin_right'         => 10,
    'margin_top'           => 10,
    'margin_bottom'        => 10,
    'margin_header'        => 0,
    'margin_footer'        => 0,
    'orientation'          => 'P',
    'title'                => 'PDF',
    'author'               => '',
    'watermark'            => '',
    'show_watermark'       => false,
    'watermark_font'       => 'sans-serif',
    'display_mode'         => 'fullpage',
    'watermark_text_alpha' => 0.1,
    'custom_font_dir'      => storage_path('app/public/pdf/'),
    'custom_font_data'     => [],
    'auto_language_detection'  => false,
    'tempDir'               => storage_path('app/public/pdf/'),
];

基本用法

// 注意保存路径不存在,请手动创建
PDF::loadView('pdf', $data)->save($pdfFilePath);
readfile($pdfFilePath);

参考文档:https://github.com/mccarlosen/laravel-mpdf

 400 total views,  2 views today

Revisions

Comments are closed.