PHP 使用 wkhtmltopdf 的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kkshell
V2EX    PHP

PHP 使用 wkhtmltopdf 的问题

  •  
  •   kkshell 2019-09-07 14:01:03 +08:00 5967 次点击
    这是一个创建于 2306 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问题麻烦跳转看下 https://segmentfault.com/q/1010000020318067

    搞了一早上没得头绪,求支招

    第 1 条附言    2019-09-07 15:52:18 +08:00
    wkhtmltopdf 0.12.1
    第 2 条附言    2019-09-07 16:56:24 +08:00
    问题已经解决,这个文档里面有提到一个问题,我尝试升级一下 wkhtmltopdf,然后就可以咯- -

    https://github.com/KnpLabs/snappy/blob/master/doc/faq.md

    Q: It says wkhtmltopdf: cannot connect to X server or xvfb-run: error: Xvfb failed to start.
    A: Please, check your wkhtmltopdf version. It is recommended to use at least 0.12.2.1 and what is important - starting from wkhtmltopdf >= 0.12.2 it doesn't require X server or emulation anymore. You can download new version from http://wkhtmltopdf.org/downloads.html or install via composer for Linux servers as stated in README. If there is no possibility to update wkhtmltopdf, please check http://stackoverflow.com/questions/9604625/wkhtmltopdf-cannot-connect-to-x-server
    19 条回复    2019-09-08 23:46:48 +08:00
    kkshell
        1
    kkshell  
    OP
       2019-09-07 14:08:58 +08:00
    youyou 会的大佬不
    AlloVince
        2
    AlloVince  
       2019-09-07 14:46:25 +08:00
    和 php 无关,wkhtmltopdf 需要启动图形界面,你可以借助 xvfb 封装一个无界面的命令行

    具体可以参考我以前写过的一个 wkhtmltopdf 微服务 dockerfile

    https://gist.github.com/AlloVince/947a2d08bc934a592b6eb58cc6410fde
    kkshell
        3
    kkshell  
    OP
       2019-09-07 15:01:23 +08:00
    @AlloVince 好的谢谢,我参考一下
    manhere
        4
    manhere  
       2019-09-07 15:04:21 +08:00
    可以 composer require "knplabs/knp-snappy"
    kkshell
        5
    kkshell  
    OP
       2019-09-07 15:05:23 +08:00
    @manhere 有什么作用
    manhere
        6
    manhere  
       2019-09-07 15:06:30 +08:00
    @kkshell https://github.com/KnpLabs/snappy 一直使用,没有问题
    gamesme
        7
    gamesme  
       2019-09-07 15:25:51 +08:00 via iPhone
    虽然我是个小白 但是有看过一个 laravel 项目使用 wkhtmltopdf 项目叫 Bookstackapp 里面也是用楼上提到的 knpsnappy 包来调用 wkhtmltopdf
    具体代码我就看不懂了 不知道能不能帮到你
    gamesme
        8
    gamesme  
       2019-09-07 15:26:56 +08:00 via iPhone
    好像不是用 exec 是用 proc_open 执行
    kkshell
        9
    kkshell  
    OP
       2019-09-07 15:44:23 +08:00
    @manhere
    $snappy = new Pdf('/usr/bin/wkhtmltopdf');
    $snappy->generateFromHtml($content, '/tmp/bill-123.pdf');

    问题依旧如下:The exit status code '1' says something went wrong:
    stderr: "wkhtmltopdf: cannot connect to X server
    "
    stdout: ""
    command: /usr/bin/wkhtmltopdf --lowquality '/tmp/knp_snappy5d735f969db923.75691036.html' '/tmp/bill-123.pdf'.
    kkshell
        10
    kkshell  
    OP
       2019-09-07 15:44:48 +08:00
    @gamesme 嗯,但是还是有这个问题 wkhtmltopdf: cannot connect to X server
    kkshell
        11
    kkshell  
    OP
       2019-09-07 15:45:07 +08:00
    @AlloVince 网站无法访问
    sleepm
        12
    sleepm  
       2019-09-07 16:10:25 +08:00 via Android
    你把它整复杂了
    exec 直接调用 wkhtmltopdf 完了,要啥 xserver
    kkshell
        13
    kkshell  
    OP
       2019-09-07 16:17:19 +08:00
    @sleepm 直接调用就是报这个错 string(41) "wkhtmltopdf: cannot connect to X server"
    gamesme
        14
    gamesme  
       2019-09-07 16:25:13 +08:00
    目前为止我觉得二楼的回答是最靠谱的
    For CentOS PHP environment the WkHTMLtoPDF tool not need xvfb-run to exec the command, But for Ubuntu PHP environment need xvfb-run to exec the command! I had revised my code as below and the issues was resolved

    这是楼主您在 segmentfault 贴的解决办法链接,错误出现的原因应该是 Ubuntu 需要图形界面来运行 wkhtmltopdf
    wangyongbo
        15
    wangyongbo  
       2019-09-07 16:31:04 +08:00   1
    你可以尝试升级到这个版本。wkhtmltopdf 0.12.4 (with patched qt)

    我在 python 里面用的这个版本。 运行正常。
    zjsxwc
        16
    zjsxwc  
       2019-09-07 16:33:11 +08:00
    基于 Qt Webkit 的工具你想不依赖 x server 就能运行?
    183shl
        17
    183shl  
       2019-09-07 22:52:33 +08:00 via Android
    借帖子问一下,我用 python 写了一个 markdown 转 pdf,表格渲染不出来怎么解决呢
    linxiaojialin
        18
    linxiaojialin  
       2019-09-08 01:31:45 +08:00
    Laravel:

    # config/snappy.php
    return [
    'pdf' => array(
    'enabled' => true,
    'binary' => '/usr/local/bin/wkhtmltopdf',
    'timeout' => false,
    'options' => [],
    'env' => [],
    ),
    'image' => array(
    'enabled' => true,
    'binary' => '/usr/local/bin/wkhtmltoimage',
    'timeout' => false,
    'options' => [],
    'env' => [],
    ),
    ];

    # Controller@method:
    $snappy = resolve('snappy.image.wrapper');
    return $snappy->getOutput($url);
    MeteorCat
        19
    MeteorCat  
       2019-09-08 23:46:48 +08:00 via Android
    这个玩意需要 x 桌面环境,也就是图形化处理;这个没办法,我记得蚂蚁笔记服务器部署也有这个,一安装这个划拉一堆 X 桌面环境
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3021 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 12:44 PVG 20:44 LAX 04:44 JFK 07:44
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86