注意:生成PDF需要引入Tcpdf.mypdf类。
<?php
/* 生成pdf */
public function createPdf(){
// set_time_limit(0); // 一小时
// ini_set("memory_limit","512M");
ini_set('max_execution_time', '0'); //改变最大执行时间限制值
vendor('Tcpdf.mypdf');//引入PDF类库
$path_img = rtrim($_SERVER["SCRIPT_FILENAME"],"admin.php") ."Down/ceyan/report/img/";
if (!file_exists($path_img)) { mkdir($path_img, 0777 ,true); }
// 查询数据
$info = $this->objModel->getPdfInfo();
//循环数组
foreach( $info as $data ){
$pdf = new \MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$name = iconv("UTF-8","GB2312//IGNORE",$data['name']);
$pdfname = $name.'_'.uniqid();//保存文件名
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Lby');
$pdf->SetTitle('创新素质测验');
$pdf->setPrintHeader(false);
// 设置页眉显示的内容
// 设置默认等宽字体
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// 设置行高
$pdf->setCellHeightRatio(1);
// 设置左、上、右的间距
$pdf->SetMargins('10', '10', '10');
// 设置是否自动分页 距离底部多少距离时分页
$pdf->SetAutoPageBreak(false);
// 设置图像比例因子
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setFontSubsetting(false);
//设置pdf第一页
$pdf->AddPage();
//首页背景图
$bMargin = $pdf->getBreakMargin();
// get current auto-page-break mode
$auto_page_break = $pdf->getAutoPageBreak();
// disable auto-page-break
$pdf->SetAutoPageBreak(false, 0);
// 设置背景图
$path_image = rtrim($_SERVER["SCRIPT_FILENAME"],"admin.php") ."/Public/Template/";
$pdf->Image($path_image . '/pdf1.png', '0', '0', 210, 297, '', '', '', false, 300, '', false, false, 0);
// restore auto-page-break status
$pdf->SetAutoPageBreak($auto_page_break, $bMargin);
// 设置字体
$pdf->SetFont('stsongstdlight', '', 15, '', true);
$pdf->setCellPaddings(0, 0, 0, 0);//未知,行上下边距
$pdf->setCellMargins(10, 50, 0, 0);//横向边距
$html_page_1 = '<div class="riqi"> '.date("Y年m月d日").' </div>';
$pdf->writeHTML($html_page_1,$ln = true, $fill = false, $reseth = false, $cell = false, $align = '1');
//设置pdf第二页
$pdf->AddPage();
$pdf->SetFont('stsongstdlight', '', 15, '', true);
$pdf->setCellPaddings(35, 3, 5, 0);//未知,行上下边距
$pdf->setCellMargins(0, 0, 0, 0);
$html_page_2 = '';
$pdf->SetAutoPageBreak(false, 0);
// 设置背景图
$pdf->Image($path_image . 'pdf2.png', '0', '0', 210, 297, '', '', '', false, 300, '', false, false, 0);
$pdf->setPageMark(0);
$pdf->writeHTML($html_page_2,$ln = true, $fill = false, $reseth = false, $cell = false, $align = '1');
//设置pdf第三页
$pdf->AddPage();
$pdf->SetFont('stsongstdlight', '', 15, '', true);
$pdf->setCellPaddings(35, 3, 5, 0);//未知,行上下边距
$pdf->setCellMargins(0, 0, 0, 0);
$html_page_3 = '';
$pdf->SetAutoPageBreak(false, 0);
// 设置背景图
$pdf->Image($path_image . 'pdf3.png', '0', '0', 210, 297, '', '', '', false, 300, '', false, false, 0);
$pdf->setPageMark(0);
$pdf->writeHTML($html_page_3,$ln = true, $fill = false, $reseth = false, $cell = false, $align = '1');
//设置pdf第四页
$pdf->AddPage();
$pdf->SetFont('stsongstdlight', '', 15, '', true);
$pdf->setCellPaddings(35, 3, 5, 0);//未知,行上下边距
$pdf->setCellMargins(0, 0, 0, 0);
$html_page_4 = '';
$pdf->SetAutoPageBreak(false, 0);
// 设置背景图
$pdf->Image($path_image . 'pdf4.png', '0', '0', 210, 297, '', '', '', false, 300, '', false, false, 0);
$pdf->setPageMark(0);
$pdf->writeHTML($html_page_4,$ln = true, $fill = false, $reseth = false, $cell = false, $align = '1');
//设置pdf第五页
$pdf->AddPage();
$pdf->SetFont('stsongstdlight', '', 15, '', true);
$pdf->setCellPaddings(0, 0, 0, 0);//未知,行上下边距
$pdf->setCellMargins(33, 28, 0, 0);
$html_page_5 = '第五页内容';
// echo $html_page_5;exit();
$pdf->SetAutoPageBreak(false, 0);
// 设置背景图
$pdf->Image($path_image . 'pdf5.png', '0', '0', 210, 297, '', '', '', false, 300, '', false, false, 0);
$pdf->setPageMark(0);
$pdf->writeHTML($html_page_5,$ln = true, $fill = false, $reseth = false, $cell = false, $align = '1');
//设置pdf第六页
$pdf->AddPage();
$pdf->SetFont('stsongstdlight', '', 15, '', true);
$pdf->setCellPaddings(0, 1, 0, 0);//未知,行上下边距
$pdf->setCellMargins(10, 2, 10, 0);
$html_page_6 = '第六页内容';
$pdf->SetAutoPageBreak(false, 0);
// 设置背景图
$pdf->Image($path_image .'pdf6.png', '0', '0', 210, 297, '', '', '', false, 300, '', false, false, 0);
$pdf->setPageMark(0);
$pdf->writeHTML($html_page_6,$ln = true, $fill = false, $reseth = false, $cell = false, $align = '1');
//设置pdf第七页
$pdf->AddPage();
$pdf->SetFont('stsongstdlight', '', 15, '', true);
$pdf->setCellPaddings(0, 1, 0, 0);//未知,行上下边距
$pdf->setCellMargins(10, 2, 10, 0);
$html_page_7 = '第七页内容根据需求自行调整';
$pdf->SetAutoPageBreak(false, 0);
// 设置背景图
$pdf->Image($path_image .'pdf7.png', '0', '0', 210, 297, '', '', '', false, 300, '', false, false, 0);
$pdf->setPageMark(0);
$pdf->writeHTML($html_page_7,$ln = true, $fill = false, $reseth = false, $cell = false, $align = '1');
//设置pdf第八页
$pdf->AddPage();
$pdf->SetFont('stsongstdlight', '', 15, '', true);
$pdf->setCellPaddings(0, 0.6, 0, 0);//未知,行上下边距
$pdf->setCellMargins(5, 2, 5, 0);
$html_page_8 = '第八页内容';
$pdf->SetAutoPageBreak(false, 0);
// 设置背景图
$pdf->Image($path_image .'pdf8.png', '0', '0', 210, 297, '', '', '', false, 300, '', false, false, 0);
$pdf->setPageMark(0);
$pdf->writeHTML($html_page_8,$ln = true, $fill = false, $reseth = false, $cell = false, $align = '1');
$path_a = rtrim($_SERVER["SCRIPT_FILENAME"],"admin.php") ."Down/ceyan/report/";
if (!file_exists($path_a)) { mkdir($path_a, 0777 ,true); }
$path = $path_a . '/' . $pdfname .'.pdf';
$pdf->Output($path, 'f');// i 输出到浏览器,D下来php://output S保存 f保存在服务器
ob_end_clean();
unset($pdf);
}
$this->redirect('Home/Notice/succ', '完成');
exit();
}
public function succ(){
pri('完成');
exit();
}