Thinkphp5整合wkhtmltox扩展将html转image或pdf

Thinkphp5整合wkhtmltox扩展将html转image或pdf

安装php-wkhtmltox扩展

参看 https://blog.csdn.net/LAMPDOWN/article/details/87785359

实现转换的类

<?php

namespace app\common\helper;

use think\image\Exception as ImageException;

/**
 * 转换器助手
 * 提供html转image、pdf的功能
 * Class Conversion
 */
class Conversion{

    /**
     * @var array 图片后缀名
     */
    private $imageSuffix = ['.jpg','.png','.jpeg','.bmp','.gif','.ico'];

    /**
     * @var string 图片存储路径
     */
    private $imagePath = RUNTIME_PATH . 'image'. DS ;

    /**
     * @var string pdf文档存储路径
     */
    private $pdfPath = RUNTIME_PATH . 'pdf'. DS ;

    /**
     * @var string 图片名
     */
    private $imageName;

    /**
     * @var string pdf文档名
     */
    private $pdfName;

    public function __construct()
    {
        if (!file_exists($this->imagePath)) {
            mkdir($this->imagePath, 0755, true);
        }

        if (!file_exists($this->pdfPath)) {
            mkdir($this->pdfPath, 0755, true);
        }
    }

    /**
     *
     * 将html网页转化为图片
     *
     * 网页需要以http://或者https://开头
     * 目标图片路径可为空,为空时图片将放到 RUNTIME_PATH . 'image'目录,并且图片名为创建时的时间戳;
     * 如果只传图片名而不传完整路径,图片同样放到RUNTIME_PATH . 'image'目录下,图片名保留;
     * 如果传完整路径,将按路径存放。
     *
     * 注意图片目录的可写性
     *
     * @param string          $url          html网址
     * @param null|string     $image        目标图片路径
     * @return string         $destination  图片路径
     */
    public function convertHtmlToImage($url,$image=null){

        //url检查
        if('http://'!=substr($url,0,7) && 'https://'!=substr($url,0,8)){
            throw new ImageException('html url is invalid');
        }

        //图片格式检查,获取生成的图片路径
        if($image==null){
            $this->imageName = time().'.jpg';
            $this->imagePath = RUNTIME_PATH . 'image'. DS . $this->imageName;
        }else{

            if(!in_array(strrchr($image,'.'),$this->imageSuffix)){

                throw new ImageException('image name or path is invalid');
            }

            //检查传的是不是路径还是图片名
            if( substr( strrchr($image,'/'),1 )){

                $this->imagePath = $image;
                $this->imageName = substr( strrchr($image,'/'),1 );

            }else{

                $this->imagePath = RUNTIME_PATH . 'image'. DS .$image;
                $this->imageName = $image;
            }

        }

        try{
            //HTML 转成图片,输出的图片路径放到$_SERVER['DOCUMENT_ROOT']下,
            //out的值如果时其他自定义的路径,将导致无法生成图片,即使是$_SERVER['DOCUMENT_ROOT']也无法生成,暂不清楚缘由
            wkhtmltox_convert(
                'image',
                array(
                    'out' => $this->imageName,
                    'in'  => $url
                )
            );

            //移动图片到指定目录
            $source = $_SERVER['DOCUMENT_ROOT'].DS.$this->imageName;
            copy($source,$this->imagePath);
            unlink($source);

            return $this->imagePath;
        }catch (\Exception $e){
            throw new ImageException($e->getMessage());
        }

    }


    /**
     *
     * 将html网页转化为pdf文档
     *
     * 网页url可为字符串或数组,为数组时,可实现将多个网页内容合并到一个pdf文档
     * 目标文档存储路径可为空,为空时文档将放到 RUNTIME_PATH . 'pdf' 目录,并且文档名为创建时的时间戳;
     * 如果只传文档名而不传完整路径,文档同样放到RUNTIME_PATH . 'pdf' 目录下,文档名保留;
     * 如果传完整路径,将按路径存放。
     *
     * 注意目录的可写性
     *
     * @param  string|array    $url         html网址
     * @param  null|string     $file        目标文档存储路径
     * @return string          $destination 生成的文档路径
     */
    public function convertHtmlToPdf($url,$file=null){
        $page=[];

        //url检查并设置page数组
        if(empty($url)){
            throw new ImageException('url不能为空');
        }
        if(is_string($url)){
            $page[]['page']=$url;
        }

        elseif(is_array($url)){
            foreach ($url as $subUrl){
                $page[]['page']=$subUrl;
            }

        }
        else{
            throw new ImageException('不支持的url参数格式');
        }


        if($file==null){
            $this->pdfName = time().'.pdf';
            $this->pdfPath = RUNTIME_PATH . 'pdf'. DS . $this->pdfName;
        }else{

            if(strrchr($file,'.')!='.pdf'){

                throw new ImageException('无效的文件名');
            }

            //检查传的是不是路径还是图片名
            if( substr( strrchr($file,'/'),1 )){

                $this->pdfPath = $file;
                $this->pdfName = substr( strrchr($file,'/'),1 );

            }else{

                $this->pdfPath = RUNTIME_PATH . 'pdf'. DS .$file;
                $this->pdfName = $file;
            }

        }

        try{
            wkhtmltox_convert(
                'pdf',
                array(
                    'out' =>  $this->pdfName,
                    'imageQuality' => '95'
                ),
                $page
            );
            //移动文件到指定目录
            $source = $_SERVER['DOCUMENT_ROOT'].DS.$this->pdfName;
            copy($source,$this->pdfPath);
            unlink($source);

            return $this->pdfPath;
        }catch (\Exception $e){
            throw new ImageException($e->getMessage());
        }

    }

}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值