一义图、說(shuō)明
公司新項(xiàng)目有一個(gè)將數(shù)據(jù)導(dǎo)出PDF文件格式的需求苞尝,所以花了半天的時(shí)間在網(wǎng)上到處找技術(shù)成熟的輪子,試了好幾個(gè),最后發(fā)現(xiàn)mPDF這個(gè)輪子最好用梳猪,做完了功能矩父,寫(xiě)篇總結(jié)劳坑,希望帶給有同樣需求的朋友一些幫助言津。
二、安裝
window環(huán)境需要在dos系統(tǒng)跳轉(zhuǎn)到項(xiàng)目根目錄进副,執(zhí)行命令:
composer require niklasravnsborg/laravel-pdf
三这揣、配置
- 打開(kāi)文件:/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' => 15,
'margin_right' => 15,
'margin_top' => 16,
'margin_bottom' => 16,
'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,
],
],
];
四给赞、基本使用
- 直接在網(wǎng)頁(yè)上渲染PDF文件
$pdf = \PDF::loadHTML('HTML內(nèi)容');
return $pdf->stream();
- 直接下載生成的PDF文件
$pdf = \PDF::loadHTML('HTML內(nèi)容');
return $pdf->download('document.pdf');
- 通過(guò)模版文件渲染PDF
// 準(zhǔn)備動(dòng)態(tài)數(shù)據(jù)
$data = ['abc', 'efg'];
// 調(diào)用模版文件渲染(模版文件位于/resource/view/pdf.php)
$pdf = \PDF::loadView('pdf', ['data'=>$data]);
// 直接在頁(yè)面渲染顯示PDF
return $pdf->stream('show.pdf');
// 直接下載生成的PDF文件
————————————————
版權(quán)聲明:本文為CSDN博主「追風(fēng)2019」的原創(chuàng)文章,遵循CC 4.0 by-sa版權(quán)協(xié)議矫户,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明片迅。
原文鏈接:https://blog.csdn.net/createNo_1/article/details/83341070