温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

wkhtmltopd 下载安装以及php环境下的使用

发布时间:2020-06-07 21:03:06 来源:网络 阅读:1589 作者:哈士奇1234 栏目:web开发
  1. 首先打开下载wkhtmltopd需要的网址 https://wkhtmltopdf.org/downloads.html
    我的参考地址 https://gitee.com/srchen1987/dawdler-series
    首先查看自己服务端的操作系统 :

    uname -a //linux 查看操作系统

     # lsb_release -a   
    
     我的是 CentOS 7.5  ,所以下载  CentOS 7    x86_64 / i686   Package (.rpm) built on CentOS 7.4.1804
     https://downloads.wkhtmltopdf.org/0.12/0.12.5/wkhtmltox-0.12.5-1.centos7.x86_64.rpm   //rpm文件
     但是如果你想用tar.xz文件,下载地址在
     https://github.com/wkhtmltopdf/wkhtmltopdf/releases/0.12.4   选择对应操作系统即可,我选择的是
     https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz

2.下面进行安装

  由于上面我们分了两种文件,下面我们介绍两种安装方式:
        不管哪种方式,我们需要先进入到安装的目录,进入shell命令:
        cd /       #进入根目录
        cd usr/local    #进入指定目录
【1】rpm文件安装
进入指定目录后执行下面命令 :
    这里按章rpm文件,遇到了很多需要下载的包,所以我尝试换一下tar.xz文件下载
【2】tar.xz文件下载安装
命令:
    $ : wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz   #下载文件
    $ :  tar wkhtmltox-0.12.4_linux-generic-amd64.tar.xz    #解压文件
    在usr/local 出现 wkhtmltox  文件夹即为成功
    为了能从任意路径执行程序,将 wkhtmltopdf 安装到 /usr/bin 目录下 ,执行命令
    $ : cp wkhtmltox/bin/wkhtmltopdf /usr/bin/

3.额外问题

     本人中途遇到了很多报错问题 
     fontconfig is needed by wkhtmltox-1:0.12.5-1.centos7.x86_64
    libXext is needed by wkhtmltox-1:0.12.5-1.centos7.x86_64
    libXrender is needed by wkhtmltox-1:0.12.5-1.centos7.x86_64
    xorg-x11-fonts-75dpi is needed by wkhtmltox-1:0.12.5-1.centos7.x86_64
    xorg-x11-fonts-Type1 is needed by wkhtmltox-1:0.12.5-1.centos7.x86_64

    上面的提示,我缺少 fontconfig libXext libXrender,于是我按照缺少的一直用 yum安装(操作系统里面没有yum可自行百度安装方法)
  下面是我安装的一些插件:
    $ : yum install libXrender*
    $ : yum install libfontconfig*
    $ : yum install libXext*

4.使用wkhtmltopdf

    如果你没有碰到我第三部的问题,可以直接再shell控制台直接使用
    $ : wkhtmltopdf http://www.baidu.com pdftest.pdf
    成功显示
    Loading pages (1/6)
    libpng warning: iCCP: known incorrect sRGB profile           ] 49%
    libpng warning: iCCP: known incorrect sRGB profile           ] 53%
    libpng warning: iCCP: known incorrect sRGB profile           ] 56%
    Counting pages (2/6)                                               
    Resolving links (4/6)                                                       
    Loading headers and footers (5/6)                                           
    Printing pages (6/6)
    Done 
    如果你不知道生成的pdf路径在哪里 ,我们需要查找刚才生成的pdf文件
    $ :find / -name pdftest.pdf
    输出 /usr/local/pdftest.pdf ,可以看到生成的pdf文件默认在 wkhtmltox 同级目录下

5.在php下使用 wkhtmltopdf

    刚才是在控制台使用的wkhtmltopdf命令,而我们需要在php文件执行生成pdf,总结网上给的方法:
    (1)安装libwkhtmltox (linux内核、Centos-6 .5-64bit),相当于一个扩展
    (2)shell_exec()  执行shell命令,确保该函数可以执行
             我这里最开始碰到的一个问题是,在在服务器控制台用wkhtmltopdf http://www.baidu.com pdftest.pdf可以 
    生成pdf文件,用自己的php代码生成一直行不通,以为是扩展和shellexec必须要同时满足,其实只要满足其中
        一个就行,用shellexec快一点,用第一种方法需要×××下载或者git查找需要的扩展压缩。*

    【1】这里我要实现的是 前端给一个地址访问    http://www.xxx.com/test/test,生成对应的pdf文件在服务器端口,
    于是简单的给一个可以访问的地址生成test.php文件
    php代码 :
    <?php  $res = shell_exec('wkhtmltopdf http://www.baidu.com make.pdf');  ?>
    运行 http://www.xxx.com/test/test  ,但是一直没有执行生成对应的pdf 
  【2】我尝试另外的解决方式 我在 /usr/local下生成 test.php文件 代码如上,用服务端自己运行
    $ : php test.php 
    竟然在 /usr/local 下生成了make.pdf
    【3】但是为什么放在可访问的地址上写一样的代码,第二种方法可以生成,于是我在第一种test.php 改写代码
       <?
         php  $res = shell_exec('wkhtmltopdf -V);    #注意这里是 大写的 V
         ?>  
    可以看到浏览器里面返回了 :
    wkhtmltopdf 0.12.4 (with patched qt)
    【4】对比两种方法,说明wkhtmltopdf是可以执行了,因为可以返回其中的版本,只是为什么一个可以生成一
    个不可以,最后发现是你进入shell是以root身份进去,有着读写命令, 但是直接访问你地址的话,你写入pdf没
    有这个权限
    需要赋予这个权限,你可以在你的项目里面给与一个有权限的,我生成的是 /app/pdf
    所以test.php代码改进,直接访问 http://www.xxx.com/test/test 
     <?php 
         $name = 'test.pdf';
             $url = 'http://www.baidu.com';
             $file = '/usr/local/nginx/html/xiaohua/App/pdf/'.$name; 
             $res = shell_exec('wkhtmltopdf '.$url.' '.$file);
     ?>

     第二个根据所传的url 和输出pdf名称,进行浏览器下载pdf,
     访问: http://www.xxx.com/test/test?url=http://www.baidu.com&name=test.pdf
     <?php  
         $url = $_GET['url'];
     $name = $_GET['name'];
     $file = '/usr/local/nginx/html/xiaohua/App/pdf/'.$name;
     $res = shell_exec('wkhtmltopdf '.$url.' '.$file);
    header('Content-type: application/pdf'); 
    header('Content-Disposition: attachment; filename='.$name );
    readfile( $file);
     ?>

6.生成pdf中文乱码问题

    可以看到你通过访问  http://www.xxx.com/test/test?url=http://www.baidu.com&name=test.pdf 生成的pdf文件,
    存在中文乱码问题,可以在自己 C:\Windows\Fonts  找到 simsun.ttc 中文宋体,simsun.ttc 而不是  simsun.ttf,
    把字体文件放在 放在 /usr/share下面就行
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI