1.composer下载TCPDF插件
https://packagist.org/packages/tecnickcom/tcpdf
2.把TCPDF类引入到 vendor目录下
3.thinkphp5下新建Application/common/common/function.php
SetCreator(PDF_CREATOR);
$pdf->SetAuthor("作者");
$pdf->SetTitle($title);
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');//设置关键字
// 是否显示页眉
$pdf->setPrintHeader(false);
// 设置页眉显示的内容
$pdf->SetHeaderData('logo.png', 60, 'baijunyao.com', '', array(0,64,255), array(0,64,128));
// 设置页眉字体
$pdf->setHeaderFont(Array('deja2vusans', '', '12'));
// 页眉距离顶部的距离
$pdf->SetHeaderMargin('5');
// 是否显示页脚
$pdf->setPrintFooter(true);
// 设置页脚显示的内容
$pdf->setFooterData(array(0,64,0), array(0,64,128));
// 设置页脚的字体
$pdf->setFooterFont(Array('dejavusans', '', '10'));
// 设置页脚距离底部的距离
$pdf->SetFooterMargin('10');
// 设置默认等宽字体
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// 设置行高
$pdf->setCellHeightRatio(1);
// 设置左、上、右的间距
$pdf->SetMargins('10', '10', '10');
// 设置是否自动分页 距离底部多少距离时分页
$pdf->SetAutoPageBreak(TRUE, '15');
// 设置图像比例因子
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->setFontSubsetting(true);
$pdf->AddPage("A4","Landscape",true,true);
// 设置字体
$pdf->SetFont('stsongstdlight', '', 14, '', true);
$pdf->writeHTML($html);//HTML生成PDF
//$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
$showType= 'I';//PDF输出的方式。I,在浏览器中打开;D,以文件形式下载;F,保存到服务器中;S,以字符串形式输出;E:以邮件的附件输出。
$pdf->Output("{$fileName}.pdf", $showType);
}
4.入口文件(index.php)引入function.php(例:require APP_PATH.'/common/common/function.php';)
5.新建控制器Admin.php
select();
// $html = '';
// foreach($data as $k =>$v){
// foreach($v as $key=>$val ){
// $html .= $val;
// }
// }
// pdf($html);die;
//如果是把HTML页面转PDF格式执行以下代码
$html = "http://localhost/aaa.html";
$data = file_get_contents($html);//获取html页面的url
pdf($data);die;
}
}