题目:把订单用PDF打印出详情,再根据相应的位置放置信息。
图的效果如下:
//封装函数,传递三个参数(头部信息,订单详情数据,文件名)
//PDF也是要下载PDF插件放到库目录下面的
public function export_pdf($header=array(),$data=array(),$fileName=""){
$fileName =time();
set_time_limit(120);
if(empty($header) || empty($data)) $this->error("导出的数据为空!");
vendor("tcpdf.tcpdf"); //导入TCPDF类
require_cache(VENDOR_PATH . 'tcpdf/examples/lang/eng.php');
$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);//新建pdf文件
//设置文件信息,头部的信息设置
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("Author");
$pdf->SetTitle("Order Manage System");
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide'); //设置关键字
//设置页眉页脚
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'DongGuan University','Order Manage System',array(66,66,66), array(0,0,0));
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);//设置默认等宽字体
$pdf->SetMargins(PDF_MARGIN_LEFT, 24, PDF_MARGIN_RIGHT);//设置页面边幅
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);//设置自动分页符
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setLanguageArray($l);
$pdf->SetFont('droidsansfallback', '');
// $pdf->AddPage();
$pdf->SetFillColor(245, 245, 245);
$pdf->SetTextColor(0);
$pdf->SetDrawColor(66, 66, 66);
$pdf->SetLineWidth(0.3);
$pdf->SetFont('droidsansfallback', '',9);
// 填充数据
$fill = 0;
foreach($data as $list) {
$pdf->AddPage();
// Header
$num_headers = count($header);
for($i = 0; $i < $num_headers; ++$i) {
$pdf->Cell(180/$num_headers, 8, $header[$i], 1, 0, 'C', 1);
}
$pdf->Ln();
//每頁重复表格标题行
if(($pdf->getPageHeight()-$pdf->getY())<($pdf->getBreakMargin()+2)){
$pdf->SetFillColor(245, 245, 245);
$pdf->SetTextColor(0);
$pdf->SetDrawColor(66, 66, 66);
$pdf->SetLineWidth(0.3);
$pdf->SetFont('droidsansfallback', '',9);
// Header
for($i = 0; $i < $num_headers; ++$i) {
$pdf->Cell(180/$num_headers, $i, $header[$i], 1, 0, 'C', 1);
}
$pdf->Ln();
}
foreach($list as $key=>$row){
$pdf->MultiCell(180/$num_headers, 6, $row, $border=1, $align='C',$fill, $ln=0, $x='', $y='', $reseth=true, $stretch=0,$ishtml=false, $autopadding=true, $maxh=0, $valign='C', $fitcell=true);
}
$pdf->MultiCell(180/$num_headers, 12, "子订单详细信息", $border=3, $align='C',$fill, $ln=0, $x='20', $y='90', $reseth=true, $stretch=20,$ishtml=false, $autopadding=true, $maxh=0, $valign='C', $fitcell=true);
$pdf->line(5, 98, 180 ,98, null);
$details =$this->getDetails($list[0]);
$arr_detail =array('ID', '产品名称', '产品数量', '产品单价', '产品总价');
$pdf->Ln();
// $pdf->SetTextColor(225);
for($i = 0; $i < sizeof($arr_detail); ++$i) {
$pdf->MultiCell(180/7, 8, $arr_detail[$i], $border=1, $align='C',$fill, $ln=0, $x='15' +$i*(180/7), $y='100', $reseth=true, $stretch=0,$ishtml=false, $autopadding=true, $maxh=0, $valign='C', $fitcell=true);
}
$pdf->Ln();
foreach ($details as $details_list) {
foreach ($details_list as $key => $value) {
$pdf->MultiCell(180/7, 7, $value, $border=1, $align='C',$fill, $ln=0, $x='', $y='', $reseth=true, $stretch=0,$ishtml=false, $autopadding=true, $maxh=0, $valign='C', $fitcell=true);
}
$pdf->Ln();
}
$pdf->Ln();
$fill=!$fill;
}
$showType= 'D';//PDF输出的方式。I,在浏览器中打开;D,以文件形式下载;F,保存到服务器中;S,以字符串形式输出;E:以邮件的附件输出。
$pdf->Output("{$fileName}.pdf", $showType);
exit;
}