整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

PHP转换HTML为PDF文档的方法和常见问题

司的某项业务需要与用户线上签订协议,即用户在线手写一个签名,后台将公司公章信息和用户的签名以及合同信息生成一份PDF文件,供用户查看和下载。



比对了一些插件,我们最终决定使用dompdf这个插件,插件的github在这里:https://github.com/dompdf/dompdf。

1. 使用方法

  • 安装可以使用composer或者直接下载源代码,使用require或者include引入。
  • 具体的使用方式,可以参考以下示例代码。
// 引入命名空间
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。

使用步骤:

  • 下载或者复制load_font.php文件,放到dompdf文件夹内,与src和test文件夹同级
  • 修改load_font.php文件中引入的autoload.php为项目实际的位置
  • 在命令行中执行php load_font.php simkai /path/to/simkai.ttf

这样,我们就可以在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!