Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
司的某項業務需要與用戶線上簽訂協議,即用戶在線手寫一個簽名,后臺將公司公章信息和用戶的簽名以及合同信息生成一份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; }
這樣轉換其實性能影響挺大的,感覺性能不太好可以加一下緩存。
HP將HTML實體轉換為普通字符,我們可以使用PHP htmlspecialchars_decode()函數將特殊的HTML實體轉換為正常字符,如&,<,>(即&,<,>)等HTML實體字符恢復到正常字符。
下面我們就結合代碼示例,為大家介紹PHP將HTML實體轉換為普通字符的實現方法。
代碼示例如下:
<?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;
輸出結果如下:
htmlspecialchars_decode函數表示將特殊的 HTML 實體轉換回普通字符。
說明 :
string htmlspecialchars_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ] )
其參數:string表示要解碼的字符串,flags表示用下列標記中的一個或多個作為一個位掩碼,來指定如何處理引號和使用哪種文檔類型。默認為 ENT_COMPAT | ENT_HTML401。
此函數的作用和 htmlspecialchars() 剛好相反。它將特殊的HTML實體轉換回普通字符。
被轉換的實體有:&, " (沒有設置ENT_NOQUOTES 時), ' (設置了 ENT_QUOTES 時), < 以及>。
該htmlspecialchars_decode()函數與htmlspecialchars()將特殊HTML字符轉換為HTML實體的函數相反。
本篇文章就是關于PHP將HTML實體轉換為普通字符的方法介紹,簡單易懂,希望對需要的朋友有所幫助!
以上就是PHP怎么將HTML實體轉換為普通字符的詳細內容,更多請關注其它相關文章!
更多技巧請《轉發 + 關注》哦!
khtmltopdf是一個非常有用的應用程序,用于從html(網頁)創建pdf。本篇文章將介紹關于如何使用php腳本和Linux命令行工具創建網頁的pdf。
步驟1:在Linux中安裝wkhtmltopdf
從google code下載wkhtmltopdf并安裝到Linux系統。
# 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:使用命令行創建PDF
首先檢查wkhtmltopdf腳本,它在命令行中正常工作。下面的命令將創建http://google.com網頁的pdf。
# /usr/bin/wkhtmltopdf http://google.com google.pdf
步驟3:使用wkhtmltopdf創建pdf的php代碼
使用下面的PHP代碼塊從HTML(網頁)生成PDF。此腳本需要為Apache啟用shell_exec函數。大多數共享主機不允許此函數。
使用下面的代碼創建一個文件名getpdf.php,并將其放到網站文檔根目錄中。
<?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);
?>
打開以下網址生成網站的pdf(html)。
語法:http:
//youdomain.com/getPdf.php?url = <website url>&pdf = <pdf name>
示例:
https : //tecadmin.net/getPdf.php?url = http : //google.com&pdf=google.pdf
本篇文章到這里就已經全部結束了,更多其他精彩內容可以關注的Linux視頻教程欄目!
以上就是使用Qtwebkit和PHP將HTML轉換為PDF的詳細內容,更多請關注其它相關文章!
更多技巧請《轉發 + 關注》哦!
*請認真填寫需求信息,我們會在24小時內與您取得聯系。