所用插件: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>