perl wkhtmltopdf Error: Unable to write to destination

2,035 views
Skip to first unread message

Donavon Lerman

unread,
May 6, 2014, 7:31:13 PM5/6/14
to wkhtmltop...@googlegroups.com
Hello...

I have the following code running as CGI.  It starts to run and returns an empty PDF file to the browser and writes an error message to the error_log. 

Does anybody have suggestions on how to solve this?

linux: Linux version 2.6.35.6-48.fc14.i686.PAE (...) (gcc version 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: This is perl 5, version 12, subversion 2 (v5.12.2) built for i386-linux-thread-multi

Thank You in Advance.
~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 message:
Loading pages (1/6)
QPainter::begin(): Returned false============================] 100%
Error: Unable to write to destination



Ashish Kulkarni

unread,
May 7, 2014, 5:46:06 AM5/7/14
to wkhtmltop...@googlegroups.com
Use the CentOS binary for 0.12.1 instead of using such an old version.

Donavon Lerman

unread,
May 7, 2014, 2:27:26 PM5/7/14
to wkhtmltop...@googlegroups.com
I updated to wkhtmltopdf 0.12.1-c22928d (with patched qt)

The PDF is still 0 bytes.

error_log shows:
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done

kulkarn...@gmail.com

unread,
May 8, 2014, 2:26:57 AM5/8/14
to wkhtmltop...@googlegroups.com
Does it work from the command line? If not, please post an issue with the sample HTML.

Donavon Lerman

unread,
May 9, 2014, 1:51:08 PM5/9/14
to wkhtmltop...@googlegroups.com
For those of your interested, this is the script that I finally got to work.

The rights have to be set up for wkhtmltopdf to be run correctly.

#!/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!!!<br /><br /><br />
IMG: <img id="saveSignature1" src="data:image/gif;base64,R0lGODlhFwAPAKEAAP///wAAAMzMzLi3tywAAAAAFwAPAAACQIyPqQjtD98RIVpJ66g3hgEYDdVhjThyXSA4aLq2rgp78hxlyY0/ICAIBhu/HrEEKIZUyk4R1Sz9RFEkaIHNFgAAOw==" /></img>
</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;
Reply all
Reply to author
Forward
0 new messages