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與html如何配合使用php改變htmlphp過濾htmlphp輸出html標簽
PHP刪除HTMl標簽的三種解決方法_流年-CSDN博客_php去除htm...
2017年9月19日 在PHP中可以使用strip_tags函數去除HTML標簽,看下面示例: 復制代碼代碼如下: <?php $str = ‘www<p>dreamdu</p>.com'; echo(htmlspecialchars($str).”<br>”);...
CSDN技術社區
百度快照
php去除HTML標簽實例_php實例_腳本之家
2013年11月6日 在php中要去除字符串中的HTML標簽方法有很多種,最常用的就是使用strip_tags函數一并去了,只保留字符了,還在就是有選擇性的去除了這里要用正則表達式了,下面寫二...
CMSYOU分享PHPCMS V9模板風格管理設置技巧之后,我們繼續分享一篇關于自定義PHPCMS文章URL的技巧:Phpcms V9文章內容頁自定義HTML網址。
這一方法,改變html默認采用數字生成的地址,可以自定義成字母、單詞、拼音,對于網址的識別性、SEO,有很大幫助!
下面是來自rhongsheng發布的教程,分享在此。
用過2008版的網友都知道,內容模型在發布內容的時候可以自定義生成的HTML文件名,這個功能對于SEO來說非常有好處,但是到了V9之后卻很遺憾,這個功能卻沒有了,現在你只要對V9進行一個小小的修改即可令V9重新擁有08版的自定義HTML文件名的功能,操作方法如下:
1、修改你需要設置的模型,添加一個字段,配置如下: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!
*請認真填寫需求信息,我們會在24小時內與您取得聯系。