dongluobei9359 2012-09-18 19:29
浏览 205
已采纳

apache日志中的wkhtmltopdf错误

exec("wkhtmltopdf test.html output.pdf");

In the php command line it works but if I try to call it from apache it doesn't create the pdf file and the log show this:

$ tail -f  /var/log/apache2/error.log
wkhtmltopdf: cannot connect to X server
  • 写回答

3条回答 默认 最新

  • douniao7308 2012-09-18 19:33
    关注

    You can use Xvfb to emulate a x-environment

    <?php exec("xvfb-run -a wkhtmltopdf test.html output.pdf");
    

    OR

    <?php exec("startx -- `which Xvfb` :1 -screen 0 800x600x24 && DISPLAY=:1 && wkhtmltopdf test.html output.pdf");
    

    I made this script for myself to get wkhtml running on centos 6+:

     yum install xorg-x11-server-Xvfb
        nano /etc/init.d/xvfbd
        #paste this:
        #!/bin/bash
    
    #
    # /etc/rc.d/init.d/xvfbd
    #
    # chkconfig: 345 95 28
    # description: Starts/Stops X Virtual Framebuffer server
    # processname: Xvfb
    #
    
    . /etc/init.d/functions
    
    [ "${NETWORKING}" = "no" ] && exit 0
    
    PROG="/usr/bin/Xvfb"
    PROG_OPTIONS=":1 -screen 1 1024x768x24 -nolisten tcp"
    PROG_OUTPUT="/tmp/Xvfb.out"
    
    case "$1" in
    start)
    echo -n "Starting : X Virtual Frame Buffer "
    $PROG $PROG_OPTIONS>>$PROG_OUTPUT 2>&1 &
    disown -ar
    /bin/usleep 500000
    status Xvfb & >/dev/null && echo_success || echo_failure
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
    /bin/touch /var/lock/subsys/Xvfb
    /sbin/pidof -o %PPID -x Xvfb > /var/run/Xvfb.pid
    fi
    echo
    ;;
    stop)
    echo -n "Shutting down : X Virtual Frame Buffer"
    killproc $PROG
    RETVAL=$?
    [ $RETVAL -eq 0 ] && /bin/rm -f /var/lock/subsys/Xvfb
    /var/run/Xvfb.pid
    echo
    ;;
    restart|reload)
    $0 stop
    $0 start
    RETVAL=$?
    ;;
    status)
    status Xvfb
    RETVAL=$?
    ;;
    *)
    echo $"Usage: $0 (start|stop|restart|reload|status)"
    exit 1
    esac
    
    exit $RETVAL
    
    
    #END
    chmod 550 /etc/init.d/xvfbd
    chkconfig xvfdb on
    service xvfdb start
    
    #Compile wkhtml compelety:
    http://code.google.com/p/wkhtmltopdf/wiki/compilation
    
    ./configure -nomake examples,demos,docs,translations -opensource -webkit -prefix "../wkqt"
    
    
    #Install fonts:
    yum install xorg-x11-fonts*
    

    After that run it in PHP:

    exec("DISPLAY=:1  timeout 60 wkhtmltoimage --height 1024 --width 1280 --quality 80 \"" . $site . "\" " . $filename);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制