perl wkhtmltopdf错误:无法写入目标

时间:2014-05-06 23:51:40

标签: linux perl cgi wkhtmltopdf

我有以下代码作为CGI运行。它开始运行并将一个空的PDF文件返回给浏览器,并将错误消息写入error_log。

有人有关于如何解决这个问题的建议吗?

linux:Linux版本2.6.35.6-48.fc14.i686.PAE(...)(gcc版本4.5.1 20100924(Red Hat 4.5.1-4)(GCC))#1 SMP Fri Oct 22 15 :27:53 UTC 2010

wkhtmltopdf:wkhtmltopdf 0.10.0 rc2

perl:这是为i386-linux-thread-multi构建的perl 5,版本12,subversion 2(v5.12.2)

先谢谢你。 〜Donavon

perl CODE:

#!/usr/bin/perl
#### takes string containing HTML and outputs PDF to browser to download
#### (otherwise would output to STDOUT)

print "Content-Disposition: attachment; filename='testPDF.pdf'\n";
print "Content-type: application/octet-stream\n\n";

my $htmlToPrint = "<html>a bunch of html</html>";

### open a filehandle and pipe it to wkhtmltopdf
### *the arguments "- -" tell wkhtmltopdf to get
###  input from STDIN and send output to STDOUT*
open(my $makePDF, "|-", "/usr/local/bin/wkhtmltopdf", "-", "-") || die("$!");
print $makePDF $htmlToPrint;  ## sends my HTML to wkhtmltopdf which streams immediately to STDOUT

error_log消息:

Loading pages (1/6)
QPainter::begin(): Returned false============================] 100%
Error: Unable to write to destination

1 个答案:

答案 0 :(得分:0)

这是我开始工作的代码。希望有些人会发现它很有用。

确保在服务器端正确设置了权限。我们这里有一个sysadmin,它在服务器端设置模块,所以我不能告诉你那些需要什么,只是它会导致问题。

#!/usr/bin/perl
use warnings;
use strict;
use IPC::Open3;
use Symbol;

my $cmd = '/usr/local/bin/wkhtmltopdf - -';

my $err = gensym(); 
my $in  = gensym();
my $out = gensym();

my $pdf = '';

my $pid = open3($in, $out, $err, $cmd)  or die "could not run cmd : $cmd : $!\n";

my $string = '<html><head></head><body>Hello World!!!</body></html>';

print $in $string;
close($in);

while( <$out> ) {
  $pdf .= $_
}

# for trouble shooting
while( <$err> ) {
  # print "err-> $_<br />\n";
}

# for trouble shooting
waitpid($pid, 0 ) or die "$!\n";
my $retval =  $?;
# print "retval-> $retval<br />\n";

print "Content-Disposition: attachment; filename='testPDF.pdf'\n";
print "Content-type: application/octet-stream\n\n";
print $pdf;