thinkphp通过html生成pdf
thinkphp 生成pdf
{__NOLAYOUT__}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>合同模板</title>
<style>
/*打印内容*/
@media print {
@page {
size: A4 landscape; /* auto is the initial value */
margin: 0 auto; /* this affects the margin in the printer settings */
}
}
.printMask {
width: 100%;height: 100%;
position: absolute;top: 0;left: 0;
background-color: #F2F2F2;
z-index: -1;
}
.printContent {
/*display: none;*/
width: 1123px;height: auto;
position: absolute;top: 10px;left: 0;
z-index: -2;
background: #fff;
font-size: 14px;color: #606266 !important;
}
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
.header {
text-align: center;
margin-bottom: 20px;
}
.content {
margin-bottom: 20px;
}
.signature {
text-align: right;
}
</style>
</head>
<body>
<div class="printContent">
<div class="header">
<h1>合同</h1>
</div>
<div class="content">
<p>
这里是合同的条款,你可以根据具体需求编辑这里的文本。
</p>
<!-- 其他合同条款 -->
</div>
<div class="signature">
<p>
签名:<br><br><br>
日期:
</p>
</div>
</div>
</body>
</html>
composer require mpdf/mpdf 下载类库
<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use Mpdf\Mpdf;
use think\Config;
class Pdf extends Backend
{
protected $file_directory = THINK_PATH.'..'.DS.'public'.DS.'pdf'.DS.'agreement'.DS;
protected $noNeedRight = ['*'];
protected $noNeedLogin = ["*"];
public function _initialize() {
parent::_initialize();
}
public function index(){
$params = $this->request->param();
$order_sn = '';
$mark_text = '';
$file_path = $this->file_directory."{$order_sn}.pdf";
if(file_exists($file_path)){
header("location:{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}/pdf/agreement/{$order_sn}.pdf");
}
$html = view('common@print/order_contract')
->assign('order',[])
->getContent();
if(!empty($html)){
$dest = 'I';
$file_path = $order_sn.'.pdf';
// $mpdf = new Mpdf(['mode' => 'utf-8', 'format'=> 'A4-L','orientation'=>'L']);
$mpdf = new Mpdf(['mode' =>'utf-8','format'=>'A4','margin_left'=> 10,'margin_right'=>10,'margin_top'=>0,'margin_bottom'=>20,'margin_header'=>0,'margin_footer'=>0,'orientation'=>'P']);
$mpdf->showWatermarkText = true;
$mpdf->useSubstitutions = true;
$mpdf->watermark_font = 'GB';
$mpdf->SetDisplayMode('fullpage');
$mpdf->SetWatermarkText($mark_text, 0.05);
$mpdf->WriteHTML($html);
//$mpdf->Output("{$order_sn}.pdf","D");
$mpdf->Output($file_path,$dest);
if(file_exists($file_path) && $dest=='f'){
header("location:{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}/pdf/agreement/{$order_sn}.pdf");
}
exit();
}
}
}