司的某項(xiàng)業(yè)務(wù)需要與用戶線上簽訂協(xié)議,即用戶在線手寫一個(gè)簽名,后臺將公司公章信息和用戶的簽名以及合同信息生成一份PDF文件,供用戶查看和下載。
比對了一些插件,我們最終決定使用dompdf這個(gè)插件,插件的github在這里:https://github.com/dompdf/dompdf。
1. 使用方法
// 引入命名空間 use Dompdf\Dompdf; // 初始化dompdf對象 $dompdf = new Dompdf(); // 加載html文檔內(nèi)容 $dompdf->loadHtml('hello world'); // 設(shè)置紙張類型和方向 $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.
嘗試了一下,默認(rèn)帶的字體是無法渲染中文的,使用CSS的@font-face引入會報(bào)錯(cuò)(也可能是我打開方式不對)。這樣就只好自己引入一個(gè)字體了。
插件給了一個(gè)安裝語言文件的工具,地址再這里:https://github.com/dompdf/utils。
使用步驟:
這樣,我們就可以在html文檔的css中使用font-family屬性來指定字體了。
html { font-family: simkai; }
2.2 圖片無法展示
插件應(yīng)該是無法直接顯示網(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í)性能影響挺大的,感覺性能不太好可以加一下緩存。
hp修改html標(biāo)簽中的內(nèi)容php與html如何配合使用php改變htmlphp過濾htmlphp輸出html標(biāo)簽
PHP刪除HTMl標(biāo)簽的三種解決方法_流年-CSDN博客_php去除htm...
2017年9月19日 在PHP中可以使用strip_tags函數(shù)去除HTML標(biāo)簽,看下面示例: 復(fù)制代碼代碼如下: <?php $str = ‘www<p>dreamdu</p>.com'; echo(htmlspecialchars($str).”<br>”);...
CSDN技術(shù)社區(qū)
百度快照
php去除HTML標(biāo)簽實(shí)例_php實(shí)例_腳本之家
2013年11月6日 在php中要去除字符串中的HTML標(biāo)簽方法有很多種,最常用的就是使用strip_tags函數(shù)一并去了,只保留字符了,還在就是有選擇性的去除了這里要用正則表達(dá)式了,下面寫二...
CMSYOU分享PHPCMS V9模板風(fēng)格管理設(shè)置技巧之后,我們繼續(xù)分享一篇關(guān)于自定義PHPCMS文章URL的技巧:Phpcms V9文章內(nèi)容頁自定義HTML網(wǎng)址。
這一方法,改變html默認(rèn)采用數(shù)字生成的地址,可以自定義成字母、單詞、拼音,對于網(wǎng)址的識別性、SEO,有很大幫助!
下面是來自rhongsheng發(fā)布的教程,分享在此。
用過2008版的網(wǎng)友都知道,內(nèi)容模型在發(fā)布內(nèi)容的時(shí)候可以自定義生成的HTML文件名,這個(gè)功能對于SEO來說非常有好處,但是到了V9之后卻很遺憾,這個(gè)功能卻沒有了,現(xiàn)在你只要對V9進(jìn)行一個(gè)小小的修改即可令V9重新?lián)碛?8版的自定義HTML文件名的功能,操作方法如下:
1、修改你需要設(shè)置的模型,添加一個(gè)字段,配置如下:2、打開/phpcms/modules/content/create_html.php,找到代碼
$urls = $this->url->show($r['id'], '', $r['catid'],$r['inputtime']);
批量替換成
$urls = $this->url->show($r['id'], '', $r['catid'],$r['inputtime'], $r['prefix']);
共三處
3、打開/phpcms/modules/content/classes/url.class.php,找到代碼.$day = date('d',$time);
在其下方插入$tmp_id = $content_ishtml && $prefix ? $prefix : $id; //增加自定義HTML文件名支持
找到
$urls = str_replace(array('{$categorydir}','{$catdir}','{$year}','{$month}','{$day}','{$catid}','{$id}','{$page}'),array($categorydir,$catdir,$year,$month,$day,$catid,$id,$page),$urlrule);
修改為
$urls = str_replace(array('{$categorydir}','{$catdir}','{$year}','{$month}','{$day}','{$catid}','{$id}','{$page}'),array($categorydir,$catdir,$year,$month,$day,$catid,$tmp_id,$page),$urlrule);
完畢。
Enjoy it!
*請認(rèn)真填寫需求信息,我們會在24小時(shí)內(nèi)與您取得聯(lián)系。