laravel MPDF打印
最近有一个需求需要在网页上打开pdf 直接可以打印那种
因为我用的是laravel 框架
于是就找到了 carlos-meneses/laravel-mpdf
github 地址 https://github.com/carlos-meneses/laravel-mpdf
码云地址 https://toscode.gitee.com/bilier/laravel-mpdf
一、composer 安装 laravel-mpdf
composer require carlos-meneses/laravel-mpdf
二、使用步骤
1. 在 config/app.php 文件 添加
'providers' => [
// ...
Meneses\LaravelMpdf\LaravelMpdfServiceProvider::class
]
'aliases' => [
// ...
'PDF' => Meneses\LaravelMpdf\Facades\LaravelMpdf::class
]
2. 新建 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,
],
],
];
3. 使用
use PDF;
class ReportController extends Controller {
public function generate_pdf()
{
//loadView(视图,数据);
$pdf = PDF::loadView('member.pdf',['data'=>$data]);
return $pdf->stream();//直接在网页显示
// return $pdf->download('/pdf文件路径.pdf');//下载
}
}
视图示例 resources\views\member\pdf.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<table style="border-collapse: collapse; width: 900px; height: 56px; font-family: SimHei; font-size: 18px; position: relative;" id="pdf">
<tbody>
<tr style="text-align:left;" class="firstRow">
<td style="text-align:left;" colspan="5">
{{$data['order']['order_num'] ?? ''}}
</td>
</tr>
<tr style="height:75px;text-align:center;" class="firstRow">
<td style="text-align:left;" colspan="3">
<img src="static/img/pdflogo.png"/>
</td>
<td style="text-align:right" colspan="2">
{{ date('Y').'年'.date('m').'月'.date('d').'日'}}
</td>
</tr>
<tr style="height:50px;text-align:center;" class="firstRow">
<td style="font-weight: bold;font-size:40px;text-align:center;" colspan="5">
買取申込書
</td>
</tr>
<tr style="height:100px;">
<td style="border:2px solid;" colspan="5">
<table style="width:900px;border:0px;text-align:center;" border="1" cellspacing="0" cellpadding="0">
<tr>
<td style="width:20%;background:#C0C0C0;">ふりがな</td>
<td style="width:30%">{{$data['examine']['name'] ?? ''}}</td>
<td style="width:10%;background:#C0C0C0;">性別</td>
<td style="width:10%">{{$data['examine']['sex']==1 ?'男':'女' }}</td>
<td style="width:30%;background:#C0C0C0;">生年月日</td>
</tr>
<tr>
<td style="background:#C0C0C0;">お名前</td>s
<td>{{$data['examine']['name'] ?? ''}}</td>
<td style="background:#C0C0C0;">年齢</td>
<td>{{$data['examine']['age'] ?? ''}}才</td>
<td>西暦 {{$data['examine']['birth'] ?? ''}}</td>
</tr>
<tr>
<td style="background:#C0C0C0;">ご住所〒</td>
<td colspan="4">{{$data['examine']['addr'] ?? ''}}</td>
</tr>
<tr>
<td style="background:#C0C0C0;">お電話番号</td>
<td>{{$data['member']['mobile'] ?? ''}}</td>
<td colspan="2" rowspan="2" style="background:#C0C0C0;">ご職業</td>
<td rowspan="2">{{$data['examine']['occupation'] ?? ''}}</td>
</tr>
<tr>
<td style="background:#C0C0C0;">メールアドレス</td>
<td>{{$data['member']['email'] ?? ''}}</td>
</tr>
</table>
</td>
</tr>
<tr style="height:32px;text-align:left;">
<td style="font-weight: normal;width:15%;">
【必要本人確認種類】
</td>
<td style="font-weight: normal; font-size:10px;color:red;width:85%;" colspan="4">
※申込者本人の身分証に限ります<br/>
※古物営業法により取引相手の義務付けられている為、本人確認種類は氏名、現住所、生年月日が記載されているものをご用意下さい。
</td>
</tr>
<tr style="height:200px">
<td style="border:2px solid;" colspan="5">
<table style="width:900px;border:0px;text-align:center;" border="1" cellspacing="0" cellpadding="0">
<tr>
<td style="background:#C0C0C0;" colspan="3">銀行振込</td>
</tr>
<tr>
<td style="text-align:left;" colspan="3">
►住民票原本(原本)
►印鑑証明書原本(原本)
►戸籍の謄本原本(原本)のいずれか
</td>
</tr>
<tr>
<td style="background:#C0C0C0;" colspan="2">お振込先銀行名</td>
<td style="background:#C0C0C0;">支店名</td>
</tr>
<tr>
<td colspan="2">{{$data['examine']['bank_name'] ?? ''}}</td>
<td>{{$data['examine']['branch_name'] ?? ''}}</td>
</tr>
<tr>
<td style="width:20%;background:#C0C0C0;">口座種別</td>
<td style="width:40%;background:#C0C0C0;">口座番号</td>
<td style="width:40%;background:#C0C0C0;">口座名義</td>
</tr>
<tr>
<td>{{$data['examine']['port_category'] ?? ''}}</td>
<td>{{$data['examine']['slogans'] ?? ''}}</td>
<td>{{$data['examine']['name_mouth'] ?? ''}}</td>
</tr>
<tr>
<td style="text-align:left;background:#C0C0C0;border:0px;" colspan="3">►法人の場合</td>
</tr>
<tr>
<td colspan="3" style="text-align:left;">
►登記事項証明書(原本)
►印鑑登録証明書(原本)
<span style="color:red;font-size:10px;">※発行より3ヶ月以内のもの</span> のいずれか
</td>
</tr>
</table>
</td>
</tr>
<tr style="height:32px">
<td style="font-weight: bold;" colspan="5">
【商品内容】
</td>
</tr>
<tr style="height:170px">
<td style="border:2px solid;" colspan="5">
<table style="width:900px;border:0px;text-align:center;" border="1" cellspacing="0" cellpadding="0">
<tr>
<td style="width:20%;background:#C0C0C0;">商品名</td>
<td style="width:20%;background:#C0C0C0;">品番</td>
<td style="width:10%;background:#C0C0C0;">状態</td>
<td style="width:15%;background:#C0C0C0;">サイズ</td>
<td style="width:15%;background:#C0C0C0;">査定値段</td>
<td style="width:10%;background:#C0C0C0;">数量</td>
<td style="width:10%;background:#C0C0C0;">合計</td>
</tr>
<?php echo $data['html'];?>
<tr>
<td style="color:red;font-size:10px;text-align:left;" colspan="4"></td>
<td>総合計</td>
<td>{{$data['order']['num'] ?? ''}}</td>
<td>{{number_format($data['order']['price']) ?? ''}}</td>
</tr>
</table>
</td>
</tr>
<tr><td colspan="5"> </td> </tr>
<tr style="height:120px;border:1px solid;">
<td colspan="5" style="font-size:12px;">
<b style="color:red;">【ご確認事項】</b></br>
・18才未満のお客様は買取行っていません。</br>
・盗難品、偽造品は買取できません。(上記事項に該当したばあい返金、損害賠償請求を行い、警察署へ被害届を提出致します。)</br>
・ご本人確認のため、身分証を確認致します。(確認できない場合お引き取りできません。)</br>
・買取成立後、(お振込後)のご返品は出来ませんので、ご注意ください。</br>
・配送時の傷や破損、紛失は一切保証出来ません。</br>
(尚、不正転売目的にて入手した商品の買取を行うことは出来兼ねます。何卒ご了承ください。)</br>
・お客様の個人情報の保護に関する法律、その他法令を尊守し、また「個人情報保護方針」を定め、個人情報の適切な取り扱いを行います。</br>
・お客様にご提供頂きました本同意書の個人情報は、下記目的のために利用致します。</br>
①買取申込・同意の確認及び所有者の確認。②古物営業法に則った売買履歴の管理。
</td>
</tr>
<tr><td colspan="5"> </td> </tr>
<tr style="height:80px;text-align:left;font-size:13px;">
<td style="text-align:right;font-weight:bold;width:20%" colspan="2">
ご名欄 <br/>
{{$data['examine']['signature'] ?? ''}}
</td>
</tr>
</tbody>
</table>
</body>
</html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律