0

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
1

1 Answer 1

0

Here is my code that I got to work. Hopefully some folks will find it useful.

Make sure the rights are set up correctly on the server side. We have a sysadmin here that set the module up on the server side so I can't tell you what those need to be, just that it can cause problems.

#!/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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.