1、安装dompdf
composer require dompdf/dompdf
2、安装字体解决中文乱码问题
下载字体 simsun,将simsun.ttf放到vendor/dompdf/dompdf文件夹下,与load_font.php同一目录下
安装字体:
php load_font.php simsun simsun.ttf
(安装其他字体,同上)
3、使用
public function exportPdf($content){
$dompdf = new DOMPDF();
$html = <<<eof
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<style>
*{font-family: "simsun"}
.clearfix:after{ content: ''; display:block; height: 0; clear: both; visibility: hidden;}
.clearfix{ zoom: 1;}
.cover{width:100%; height:900px;text-align: center; }
.cover h1{ font-family:"simsunbd"; padding-top:100px; font-size:36px; }
.cover h2,h3{ font-family:"simsunbd"; font-size:28px;}
.cover h2:nth-child(2){padding-top:400px;}
.cover h3{padding-top:20px; }
.list{ padding-top:20px;}
.list strong{ font-family:"simsunbd";}
.list div{ margin-bottom:20px;max-width:100%;}
.img-list{width:100%; max-width:100%; }
.img-list img{width:30%; float:left; margin-left:10px;}
.img-list img:first-child{margin-left:0;}
.line{border-bottom:1px solid dashed;}
</style>
</head>
<body>
<div>$content</div>
</body>
</html>
eof;
$dompdf->load_html($html);
$dompdf->render();
$pdf_content = $dompdf->output();
return $pdf_content;
}