1. 程式人生 > 實用技巧 >html生成pdf

html生成pdf

連結:http://mpdf.github.io/

需下載包https://packagist.org/packages/mpdf/mpdf

ob_clean();
ob_start();
$this->display('catalogue/export');
$html = ob_get_contents(); // retrieve buffer contents
ob_end_clean();

require_once ROOT_PATH . '/Public/mpdf60/mpdf.php';
//例項化mpdf $mode='',$format='A4',$default_font_size=0,$default_font='',$mgl=15,$mgr=15,$mgt=16,$mgb=16,$mgh=9,$mgf=9, $orientation='P'
// $mpdf = new mPDF('utf-8', 'A4', 10, '微軟雅黑', 10, 10, 20, 20);
$mpdf = new mPDF('utf-8', 'A4', 10, 'siyuanhei', 0, 0, 0, 0, 0, 0);
//設定字型,解決中文亂碼
$mpdf->useAdobeCJK = true;
$mpdf->autoLangToFont = true;
$mpdf->autoScriptToLang = true;
//設定PDF頁首內容
// $header = '<div style="float:right;font-size:12px;color:#000;text-align:right;vertical-align:bottom;">引領客戶實現組織效能與人才價值最大化</div>';

//設定PDF頁尾內容
// $footer = '<div style="float:right;text-align:right;font-size:12px;">第{PAGENO}頁/共{nb}頁</div>';

//新增頁首和頁尾到pdf中
// $mpdf->SetHTMLHeader($header);
// $mpdf->SetHTMLFooter($footer);
//設定pdf顯示方式
$mpdf->SetDisplayMode('fullpage');
//設定pdf的尺寸為270mm*397mm
//$mpdf->WriteHTML(‘<pagebreak sheet-size=”270mm 397mm” />’);
$mpdf->watermark_font = 'GB';
// $mpdf->SetWatermarkText('華夏智業', 0.1);
$mpdf->showWatermarkText = true;
//建立pdf檔案
$mpdf->WriteHTML($html);
//刪除pdf第一頁(由於設定pdf尺寸導致多出了一頁)
//$mpdf->DeletePages(1,1);
//輸出pdf $name);$projectname
$mpdf->Output($name.'.pdf', TRUE); #. '_' . date('YmdHis')

目錄:
<bookmark content="目錄" />
<tocpagebreak links="on" toc-margin-left="30mm" toc-margin-right="30mm" toc-margin-top="30mm" toc-margin-bottom="30mm" toc-odd-header-name="html_toch" toc-odd-header-value="on" toc-odd-footer-name="html_tocf" toc-odd-footer-value="on" toc-resetpagenum="1" toc-pagenumstyle="I" resetpagenum="1" margin-left="30mm" margin-right="30mm" margin-top="30mm" margin-bottom="30mm" odd-header-name="html_contenth" odd-header-value="on" odd-footer-name="html_contentf" odd-footer-value="on" pagenumstyle="1" paging="0"/>
<bookmark content="<?php echo $tv['text']; ?>" />
<tocentry content="<?php echo $tv['text']; ?>" />
<bookmark content="<?php echo $tv1['text']; ?>" level="1" />
<tocentry content="<?php echo $tv1['text']; ?>" level="1" />