apache 日志中的 wkhtmltopdf 错误 [英] wkhtmltopdf error in apache log

查看:37
本文介绍了apache 日志中的 wkhtmltopdf 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在 php 命令行中它可以工作,但是如果我尝试从 apache 调用它,它不会创建 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

推荐答案

您可以使用 Xvfb 来模拟 x-环境

You can use Xvfb to emulate a x-environment

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

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

我为自己编写了这个脚本来让 wkhtml 在 centos 6+ 上运行:

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*

然后在 PHP 中运行它:

After that run it in PHP:

exec("DISPLAY=:1  timeout 60 wkhtmltoimage --height 1024 --width 1280 --quality 80 \"" . $site . "\" " . $filename);

这篇关于apache 日志中的 wkhtmltopdf 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆