使用wkhtmltopdf+pdftk 实现多个不同宽高pdf合成一个pdf

  1. wkhtmltopdf
    简单方便且强大的一个导出PDF工具
    https://github.com/mreiferson/php-wkhtmltox
  2. pdftk
    强大的pdf文件操作工具
    http://www.pdflabs.com/docs/install-pdftk/
    为了实现不同格式的pdf共存,我想到一个笨办法~,不过还挺有用,先使用wkhtmktopdf把目标html页面导成pdf,当所有的pdf都导出来后,在用pdftk把所有的pdf合成一个pdf输出。
// An highlighted block
 public function downpdf($html){
        ob_start();
        file_put_contents("first.html", $html['first']);
        file_put_contents("second.html", $html['second']);
        file_put_contents("three.html", $html['three']);
        file_put_contents("four.html", $html['four']);
        ob_end_clean();
        while (!file_exists("first.pdf") || !file_exists("second.pdf")|| !file_exists("three.pdf")|| !file_exists("four.pdf")){
            if(!file_exists("first.pdf")){
                shell_exec("wkhtmltopdf  --page-size A4 --margin-top 20mm  first.html  first.pdf");
            }else if(!file_exists("second.pdf")){
                shell_exec("wkhtmltopdf --margin-top 20mm  --page-width 420mm --page-height 297mm second.html  second.pdf");
            }else if(!file_exists("three.pdf")){
                shell_exec("wkhtmltopdf  --margin-top 20mm --page-size A4 three.html  three.pdf");
            }else{
                shell_exec("wkhtmltopdf --margin-top 20mm --page-width 420mm --page-height 297mm four.html  four.pdf");
            }
        }
       unlink("first.html");
        unlink("second.html");
        unlink("three.html");
        unlink("four.html");
        if(file_exists("first.pdf") && file_exists("second.pdf") && file_exists("three.pdf") && file_exists("four.pdf")){
        $result = shell_exec("pdftk first.pdf second.pdf three.pdf four.pdf  cat output SBCX.pdf");
        header("Content-type:application/pdf");
        header("Content-Disposition:attachment;filename=SBCX.pdf");
        echo file_get_contents("SBCX.pdf");
            unlink("first.pdf");
            unlink("second.pdf");
            unlink("three.pdf");
            unlink("four.pdf");
            unlink("SBCX.pdf");
        }else{
            $this->error('异常','',500);
            exit;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值