1. 程式人生 > >PHP將HTML轉換成各種格式圖片或PDF

PHP將HTML轉換成各種格式圖片或PDF

安裝軟體清單

  • 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,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