司的某項業務需要與用戶線上簽訂協議,即用戶在線手寫一個簽名,后臺將公司公章信息和用戶的簽名以及合同信息生成一份PDF文件,供用戶查看和下載。
比對了一些插件,我們最終決定使用dompdf這個插件,插件的github在這里:https://github.com/dompdf/dompdf。
1. 使用方法
// 引入命名空間 use Dompdf\Dompdf; // 初始化dompdf對象 $dompdf = new Dompdf(); // 加載html文檔內容 $dompdf->loadHtml('hello world'); // 設置紙張類型和方向 $dompdf->setPaper('A4', 'landscape'); // 渲染HTML為PDF $dompdf->render(); // 流輸出 $dompdf->stream();
2. 常見問題和解決辦法
2.1 中文亂碼的問題
插件對于字體和編碼問題是這樣形容的:
PDF documents internally support the following fonts: Helvetica, Times-Roman, Courier, Zapf-Dingbats, & Symbol. These fonts only support Windows ANSI encoding. In order for a PDF to display characters that are not available in Windows ANSI, you must supply an external font. Dompdf will embed any referenced font in the PDF so long as it has been pre-loaded or is accessible to dompdf and reference in CSS @font-face rules. See the font overview for more information on how to use fonts.The DejaVu TrueType fonts have been pre-installed to give dompdf decent Unicode character coverage by default. To use the DejaVu fonts reference the font in your stylesheet, e.g. body { font-family: DejaVu Sans; } (for DejaVu Sans). The following DejaVu 2.34 fonts are available: DejaVu Sans, DejaVu Serif, and DejaVu Sans Mono.
嘗試了一下,默認帶的字體是無法渲染中文的,使用CSS的@font-face引入會報錯(也可能是我打開方式不對)。這樣就只好自己引入一個字體了。
插件給了一個安裝語言文件的工具,地址再這里:https://github.com/dompdf/utils。
使用步驟:
這樣,我們就可以在html文檔的css中使用font-family屬性來指定字體了。
html { font-family: simkai; }
2.2 圖片無法展示
插件應該是無法直接顯示網絡圖片,所以需要將圖片轉換為BASE64格式才能顯示。
將HTML文檔中的所有圖片轉換為BASE64的方式:
function imgToBase64($html) { $html = preg_replace_callback('/<img(?:.*?)src="(.*?)"(?:.*?)\/?>/', function($matches) { $imageInfo = getimagesize($matches[1]); $base64 = "" . chunk_split(base64_encode(file_get_contents($matches[1]))); $base64_image = 'data:' . $imageInfo['mime'] . ';base64,' . $base64; return str_replace($matches[1], $base64_image, $matches[0]); }, $html); return $html; }
這樣轉換其實性能影響挺大的,感覺性能不太好可以加一下緩存。
ompdf是一個可以將HTML生成PD并保留樣式效果的PHP第三方擴展。
下面就一步步講解如何使用:
一、通過composer安裝
composer require dompdf/dompdf
安裝過程
二 、編寫測試代碼
(1)引用autoload.php
include 'vendor/autoload.php';
(2)實例化Dompdf
$dompdf=new \Dompdf\Dompdf();
(3)加載HTML
$dompdf->loadHtml($html); //$html 為HTML字符串
(4)設置紙張和方向
$dompdf->setPaper('A4', 'landscape'); //紙張大小和紙張方向
(5)生成PDF并下載
$dompdf->render();
$dompdf->stream('數據字典.pdf');
三、導出PDF測試,發現中文亂碼了
導出PDF發現中文亂碼了
四、解決中文亂了問題
(1)下載支持中文的字體包放到根目錄下(和vendor目錄同級),這里演示使用的是阿里巴巴的普惠字體(字體格式是ttf的,小編原先下載使用的字體格式是otf格式的無效)
(2)下載dompdf字體安裝工具解壓到根目錄(和vendor目錄同級)
下載地址:https://github.com/dompdf/utils
(3)在命令行(CMD定位到根目錄)下執行命令
php load_font.php "puhui" Alibaba-PuHuiTi-Light.ttf
執行成功后在路徑(vendor\dompdf\dompdf\lib\fonts)下就會出現剛才的字體
(4)在樣式文件中指定使用剛才安裝的字體
body{font-family:puhui;}
(5)再次導出PDF測試成功
亂碼問題解決
段時間做打印合同時,要生成pdf文件,所以就看了一下,有很多種生成方式,我這里用的是TCPDF,想用可以自行下載一下 下面舉例說明很簡單
if($_REQUEST['act']=='pdf'){
//1引入文件
require( './TCPDF/tcpdf.php');
//2獲取到頁面內容
$html=file_get_contents("http://localhost/demo.html");
//3實例化類
$pdf = new TCPDF(PDF_PAGE_ORIENTATIN, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
//4設置自帶的字體
$pdf->SetFont('stsongstdlight', '', 12);
// 5.添加個頁面
$pdf->AddPage();
// 6.將HTML生成PDF
$pdf->writeHTML($html);
// 7.輸出(默認直接在瀏覽器顯示)
$pdf->Output();
//生成的pdf可能會有樣式問題,自行調節一下即可。
}
//上面只是簡單的介紹一下如何使用,如果有特殊需求在自行了解,僅供入門參考。
在網上找到一個很好用的函數,順便分享給大家,合同中需要轉大寫中文,下面函數即可。
//自動轉換數字金額為 大寫中文金額
function toChineseNumber($money){
$money = round($money,2);
$cnynums = array("零","壹","貳","叁","肆","伍","陸","柒","捌","玖");
$cnyunits = array("圓","角","分");
$cnygrees = array("拾","佰","仟","萬","拾","佰","仟","億");
list($int,$dec) = explode(".",$money,2);
$dec = array_filter(array($dec[1],$dec[0]));
$ret = array_merge($dec,array(implode("",cnyMapUnit(str_split($int),$cnygrees)),""));
$ret = implode("",array_reverse(cnyMapUnit($ret,$cnyunits)));
return str_replace(array_keys($cnynums),$cnynums,$ret);
}
function cnyMapUnit($list,$units) {
$ul=count($units);
$xs=array();
foreach (array_reverse($list) as $x) {
$l=count($xs);
if ($x!="0" || !($l%4))
$n=($x=='0'?'':$x).($units[($l-1)%$ul]);
else $n=is_numeric($xs[0][0])?$x:'';
array_unshift($xs,$n);
}
return $xs;
}
作者:吾林愛分享
*請認真填寫需求信息,我們會在24小時內與您取得聯系。