首页 > 代码库 > php实现pdf导出和打印功能。
php实现pdf导出和打印功能。
所用插件:TCODF
//导出pdf
public function export_pdf($order_sn){
require_once(FCPATH .‘../shared/plugin/tcpdf/tcpdf.php‘);
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, "pt", "A4", true, ‘UTF-8‘, false);
$pdf->SetFooterMargin(50);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
if (@file_exists(FCPATH .‘../shared/plugin/tcpdf/examples/lang/eng.php‘)) {
require_once(FCPATH .‘../shared/plugin/tcpdf/examples/lang/eng.php‘);
$pdf->setLanguageArray($l);
}
$pdf->SetMargins(20,50,10 ,true);
$pdf->AddPage();
$pdf->SetFont(‘stsongstdlight‘, ‘‘, 12);
$buffer = $this->assign_value($order_sn);
$startpos = strpos($buffer,‘<!--startprint1-->‘);
$endpos = strpos($buffer,‘<!--endprint1-->‘);
$html = substr($buffer,$startpos,$endpos-$startpos+strlen(‘<!--endprint1-->‘));
header("Content-type: text/html; charset=utf-8");
$name = "customs_declaration";
$export_name = $name.‘_‘.$order_sn;
$pdf->writeHTML($html,true,false,false,false,false);
$pdf->Output($export_name.‘.pdf‘, ‘d‘);
}
//将获取的网页赋值
public function assign_value($order_sn){
ob_start();
$this->declaration_print($order_sn);
$buffer = ob_get_contents();
ob_clean();
return $buffer;
}
通过ob缓存,将动态的页面缓存,已用来导出。
js打印
<script>
function preview(oper){
if (oper < 10){
//get_page_info();
bdhtml=window.document.body.innerHTML;//获取当前页的html代码
sprnstr="<!--startprint"+oper+"-->";//设置打印开始区域
eprnstr="<!--endprint"+oper+"-->";//设置打印结束区域
prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+18); //从开始代码向后取html
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));//从结束代码向前取html
window.document.body.innerHTML=prnhtml;
window.print();
//prnhtml.print();
window.document.body.innerHTML=bdhtml;
} else {
window.print();
}
}
</script>
php实现pdf导出和打印功能。