wkhtmltopdf 与 wkhtmltoimage(wkhtmltox安装)+ php

作者: 时间:2024-09-29 17:44:47

wkhtmltopdf 与 wkhtmltoimage(wkhtmltox安装)+ php


背景:将HTML 转为 PDF/jpg/png  ;中文问题,请看下面的问题二

参考:http://blog.sina.com.cn/s/blog_6dab20b401019e19.html

1、下载wkhtmltopdf 与 wkhtmltoimage(wkhtmltox)
 wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
  
 2、解压压缩包
 
 tar -xvf wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
 
 3、移动文件夹
 
 3-1,wkhtmltopdf  
 
 mv wkhtmltox/bin/wkhtmltopdf  /usr/local/bin
 
 3-2,wkhtmltoimage
 
 mv wkhtmltox/bin/wkhtmltoimage  /usr/local/bin
 
  
 4、测试是否安装成功
 wkhtmltopdf  http://www.baidu.com ./baidu.pdf
 
 wkhtmltoimage  http://www.baidu.com ./baidu.pdf

 5、找到要转换的HTML字体文件上传(可以直接使用windows里的字体如雅黑:msyh.ttf , msyhbd.ttf)
 上传到 /usr/share/fonts/
function convert($type='pdf')
        {
            $filename=time();
            $url=$this->input->get("url");
            if($type=='pdf')
            {
                $filename="F:/upload/tmp/".$filename.".pdf";
                exec("F:/home/phptool/wkhtmltopdf-i386 $url $filename");
                header('Content-Type: application/pdf');
            }
            elseif($type=='image')
            {
                $filename="upload/tmp/".$filename.".jpg";
                exec("/home/phptool/wkhtmltoimage-i386 $url $filename");
                header('Content-Type: image/jpeg');
            }
            header('Pragma: public');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Content-Transfer-Encoding: binary');
            header('Content-Length: '.filesize($filename));
            readfile("$filename");
        }
版权所有 博曦云 2019