司的某項(xiàng)業(yè)務(wù)需要與用戶(hù)線上簽訂協(xié)議,即用戶(hù)在線手寫(xiě)一個(gè)簽名,后臺(tái)將公司公章信息和用戶(hù)的簽名以及合同信息生成一份PDF文件,供用戶(hù)查看和下載。
比對(duì)了一些插件,我們最終決定使用dompdf這個(gè)插件,插件的github在這里:https://github.com/dompdf/dompdf。
1. 使用方法
// 引入命名空間 use Dompdf\Dompdf; // 初始化dompdf對(duì)象 $dompdf = new Dompdf(); // 加載html文檔內(nèi)容 $dompdf->loadHtml('hello world'); // 設(shè)置紙張類(lèi)型和方向 $dompdf->setPaper('A4', 'landscape'); // 渲染HTML為PDF $dompdf->render(); // 流輸出 $dompdf->stream();
2. 常見(jiàn)問(wèn)題和解決辦法
2.1 中文亂碼的問(wèn)題
插件對(duì)于字體和編碼問(wèn)題是這樣形容的:
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.
嘗試了一下,默認(rèn)帶的字體是無(wú)法渲染中文的,使用CSS的@font-face引入會(huì)報(bào)錯(cuò)(也可能是我打開(kāi)方式不對(duì))。這樣就只好自己引入一個(gè)字體了。
插件給了一個(gè)安裝語(yǔ)言文件的工具,地址再這里:https://github.com/dompdf/utils。
使用步驟:
這樣,我們就可以在html文檔的css中使用font-family屬性來(lái)指定字體了。
html { font-family: simkai; }
2.2 圖片無(wú)法展示
插件應(yīng)該是無(wú)法直接顯示網(wǎng)絡(luò)圖片,所以需要將圖片轉(zhuǎn)換為BASE64格式才能顯示。
將HTML文檔中的所有圖片轉(zhuǎn)換為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; }
這樣轉(zhuǎn)換其實(shí)性能影響挺大的,感覺(jué)性能不太好可以加一下緩存。
HP將HTML實(shí)體轉(zhuǎn)換為普通字符,我們可以使用PHP htmlspecialchars_decode()函數(shù)將特殊的HTML實(shí)體轉(zhuǎn)換為正常字符,如&,<,>(即&,<,>)等HTML實(shí)體字符恢復(fù)到正常字符。
下面我們就結(jié)合代碼示例,為大家介紹PHP將HTML實(shí)體轉(zhuǎn)換為普通字符的實(shí)現(xiàn)方法。
代碼示例如下:
<?php $my_str = "I'm good & <b>"boy "</b>."; echo htmlspecialchars_decode($my_str); echo "<br>"; echo htmlspecialchars_decode($my_str, ENT_QUOTES); echo "<br>"; echo $my_str;
輸出結(jié)果如下:
htmlspecialchars_decode函數(shù)表示將特殊的 HTML 實(shí)體轉(zhuǎn)換回普通字符。
說(shuō)明 :
string htmlspecialchars_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ] )
其參數(shù):string表示要解碼的字符串,flags表示用下列標(biāo)記中的一個(gè)或多個(gè)作為一個(gè)位掩碼,來(lái)指定如何處理引號(hào)和使用哪種文檔類(lèi)型。默認(rèn)為 ENT_COMPAT | ENT_HTML401。
此函數(shù)的作用和 htmlspecialchars() 剛好相反。它將特殊的HTML實(shí)體轉(zhuǎn)換回普通字符。
被轉(zhuǎn)換的實(shí)體有:&, " (沒(méi)有設(shè)置ENT_NOQUOTES 時(shí)), ' (設(shè)置了 ENT_QUOTES 時(shí)), < 以及>。
該htmlspecialchars_decode()函數(shù)與htmlspecialchars()將特殊HTML字符轉(zhuǎn)換為HTML實(shí)體的函數(shù)相反。
本篇文章就是關(guān)于PHP將HTML實(shí)體轉(zhuǎn)換為普通字符的方法介紹,簡(jiǎn)單易懂,希望對(duì)需要的朋友有所幫助!
以上就是PHP怎么將HTML實(shí)體轉(zhuǎn)換為普通字符的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注其它相關(guān)文章!
更多技巧請(qǐng)《轉(zhuǎn)發(fā) + 關(guān)注》哦!
khtmltopdf是一個(gè)非常有用的應(yīng)用程序,用于從html(網(wǎng)頁(yè))創(chuàng)建pdf。本篇文章將介紹關(guān)于如何使用php腳本和Linux命令行工具創(chuàng)建網(wǎng)頁(yè)的pdf。
步驟1:在Linux中安裝wkhtmltopdf
從google code下載wkhtmltopdf并安裝到Linux系統(tǒng)。
# cd /opt
# wget https://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-i386.tar.bz2
# tar xjf wkhtmltopdf-0.9.9-static-i386.tar.bz2
# mv wkhtmltopdf-i386 /usr/bin/wkhtmltopdf
# chown apache:apache /usr/bin/wkhtmltopdf
# chmod +x /usr/bin/wkhtmltopdf
步驟2:使用命令行創(chuàng)建PDF
首先檢查wkhtmltopdf腳本,它在命令行中正常工作。下面的命令將創(chuàng)建http://google.com網(wǎng)頁(yè)的pdf。
# /usr/bin/wkhtmltopdf http://google.com google.pdf
步驟3:使用wkhtmltopdf創(chuàng)建pdf的php代碼
使用下面的PHP代碼塊從HTML(網(wǎng)頁(yè))生成PDF。此腳本需要為Apache啟用shell_exec函數(shù)。大多數(shù)共享主機(jī)不允許此函數(shù)。
使用下面的代碼創(chuàng)建一個(gè)文件名getpdf.php,并將其放到網(wǎng)站文檔根目錄中。
<?php
$url = $_GET['url']; // Website URL to Create pdf
$name = $_GET['pdf']; // Output pdf name
$command = "/usr/bin/wkhtmltopdf ";
$pdf_dir = "/var/www/html/pdfs/"; // Pdf files will be saved here
$ex_cmd = "$command $url " . $pdf_dir . $name;
$output = shell_exec($ex_cmd);
?>
打開(kāi)以下網(wǎng)址生成網(wǎng)站的pdf(html)。
語(yǔ)法:http:
//youdomain.com/getPdf.php?url = <website url>&pdf = <pdf name>
示例:
https : //tecadmin.net/getPdf.php?url = http : //google.com&pdf=google.pdf
本篇文章到這里就已經(jīng)全部結(jié)束了,更多其他精彩內(nèi)容可以關(guān)注的Linux視頻教程欄目!
以上就是使用Qtwebkit和PHP將HTML轉(zhuǎn)換為PDF的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注其它相關(guān)文章!
更多技巧請(qǐng)《轉(zhuǎn)發(fā) + 關(guān)注》哦!
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。