您的位置:首页 > 编程语言 > PHP开发

PHP将HTML转换成各种格式图片或PDF

2015-10-14 16:22 736 查看

安装软件清单

libwkhtmltox linux下的webkit内核

phpwkhtmltox php扩展,可调用webkit内核将网页转换成各种格式图片或者pdf

font-chinese 中文字体

msyh,Consolas 字体(优雅字体)

安装步骤

安装libwkhtmltox (linux内核、Centos-6 .5-64bit)

[root@platform tmp]#rpm -ivh wkhtmltox-0.12.2.1_linux-centos6-amd64.rpm


安装phpwkhtmltox

[root@platform tmp]#wget https://github.com/mreiferson/php-wkhtmltox/archive/master.zip [root@platform tmp]#unzip master.zip
[root@platform tmp]#cd php-wkhtmltox-master
[root@platform tmp]#phpize
[root@platform tmp]#./configure
[root@platform tmp]#make && make install


修改php.ini配置文件

extension=phpwkhtmltox.so


检查PHP是否已经支持phpwkhtmltox

[root@platform tmp]#php -m


检测phpwkhtmltox是否安装成功

测试生成图片

<?php
wkhtmltox_convert(
'image',
array(
'out' => '/tmp/test.jpg',
'in'  => 'http://www.baidu.com/'
)
);
?>


wkhtmltox_convert说明

wkhtmltox_convert(type,type, globalsetting, $objectsetting)说明

html转换为pdf示例

<?php
wkhtmltox_convert('pdf',
array(
'out' => '/tmp/test.pdf',
'imageQuality' => '95'
), // global settings
array(
array(
'page' => 'http://www.google.com/'
),
array(
'page' => 'http://www.baidu.com/'
)
)// object settings
);
?>


让CentOS支持中文及字体

支持中文

[root@platform tmp]#yum groupinstall chinese-support


让linux CentOS支持微软雅黑字体

把字体文件拷贝到/usr/share/fonts/xxx,其中xxx为新增字体文件夹,如msyh

[root@platform tmp]#cd /usr/share/fonts/
[root@platform tmp]#mkdir msyh
[root@platform tmp]#cd msyh


建立字体缓存

[root@platform tmp]#mkfontscale
[root@platform tmp]#mkfontdir
[root@platform tmp]#fc-cache -fv


让Linux CentOS支持Consolas字体

把字体文件拷贝到/usr/share/fonts/xxx,其中xxx为新增字体文件夹,如Consolas

[root@platform tmp]#cd /usr/share/fonts/
[root@platform tmp]#mkdir Consolas
[root@platform tmp]#cd Consolas


建立字体缓存

[root@platform tmp]#mkfontscale
[root@platform tmp]#mkfontdir
[root@platform tmp]#fc-cache -fv
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: