PHP使用TCPDF生成PDF文件
首先通过composer安装TCPDF
安装命令:
composer require tecnickcom/tcpdf
使用TCPDF
require_once 引入 TCPDF里的 tcpdf.php文件
在公用文件里封装好pdf方法随时调用,需要传递HTML代码和文件名
function pdf($html='<h1 style="color:red">这里可以随便写一句话~</h1>',$file_name='TestFile.pdf'){
require_once (ROOT_PATH.'vendor/tecnick.com/tcpdf/tcpdf.php');
$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// 设置打印模式
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('DaZe');
$pdf->SetTitle('TCPDF Example 001');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// 是否显示页眉
$pdf->setPrintHeader(false);
// 设置页眉显示的内容
$pdf->SetHeaderData('http://www.baidu.com', 60, 'cainiao.cn', '神梦一刀', array(0,64,255), array(0,64,128));
// 设置页眉字体
$pdf->setHeaderFont(Array('dejavusans', '', '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__).