php 转换成pdf且下载,并带悬浮图片 Tcpdf

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ylwsn21/article/details/79159831

项目需要把html页面转换成pdf,并带图片。

网上试了好几种,发现tcpdf这个方法,很管用。

其中主要问题有html页面带css样式展示出来,图片也附带,下载不显示文件名等


文件下载:TCPDF 《======下载 ,放到自己的项目里。

源代码:

require APP_ROOT_PATH."/system/utils/Tcpdf/tcpdf.php";
//实例化
$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);

// 设置文档信息

$pdf->SetCreator('Victor');

$pdf->SetAuthor('Victor');

$pdf->SetTitle('xxx');

$pdf->SetSubject('');

$pdf->SetKeywords('');

//设置页眉信息 参数分别是LOGO地址,LOGO大小,两行标题,标题颜色,分割线颜色。。颜色是RGB

$pdf->SetHeaderData('', 30, '', '', array(0,0,0), array(0,0,0));

//设置页脚信息

$pdf->setFooterData(array(0,0,0), array(0,0,0));

// 设置页眉和页脚字体

$pdf->setHeaderFont(Array('stsongstdlight', '', '12'));

$pdf->setFooterFont(Array('helvetica', '', '8'));

//设置默认等宽字体

$pdf->SetDefaultMonospacedFont('courier');

//设置间距

$pdf->SetMargins(15, 27, 15);

$pdf->SetHeaderMargin(5);

$pdf->SetFooterMargin(10);

//设置分页

$pdf->SetAutoPageBreak(TRUE, 15);

//设置图片比例

$pdf->setImageScale(1.25);

//将页眉页脚的信息输出出来。

$pdf->AddPage();

//设置字体 - 正文标题的哦。B是加粗,15是大小

$pdf->SetFont('stsongstdlight', 'B', 15);

$pdf->Write(20, '', '', 0, 'C', true, 0, false, false, 0);

//设置字体 - 正文内容的哦。B是加粗,15是大小

$pdf->SetFont('stsongstdlight', '', 10);

ob_end_clean();

$pdf->writeHTMLCell(0, 0, '', '', 输出模板(我用的TP,所以用的fetch), 0, 1, 0, true, '', true);

//40:x轴,95:y轴,40:图片大小
$pdf->Image('/xxx/xxx/xxx.png', 40, 95, 40, '', '', '', '', false, 100); 
//输出PDF。第二个参数默认是I,是浏览器预览。D是下载
$name="xxx.pdf";
$pdf->Output($name,'D');  
如果下载发现文件名不显示,去tcpdf.php中的方法Output的7565行左右,把这段屏蔽了:

if ($dest[0] != 'F') {
	$name = preg_replace('/[\s]+/', '_', $name);
	$name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
}
就可以显示了



猜你喜欢

转载自blog.csdn.net/ylwsn21/article/details/79159831