Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

perl wkhtmltopdf Error: Unable to write to destination

by djlerman (Sexton)
on May 07, 2014 at 16:14 UTC ( [id://1085338]=perlquestion: print w/replies, xml ) Need Help??

djlerman has asked for the wisdom of the Perl Monks concerning the following question:

Hello Wise Monks...

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.

Do you have any wisdom 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 2 +010 wkhtmltopdf: wkhtmltopdf 0.10.0 rc2 perl: This is perl 5, version 12, subversion 2 (v5.12.2) built for i38 +6-linux-thread-multi

Thank You in Advance. ~Donavon

perl CODE:

#!/usr/bin/perl #### takes string containing HTML and outputs PDF to browser to downlo +ad #### (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 st +reams immediately to STDOUT

error_log message:

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

Replies are listed 'Best First'.
Re: perl wkhtmltopdf Error: Unable to write to destination
by RichardK (Parson) on May 07, 2014 at 16:25 UTC

    '|-' means open a command and pipe data to it, & the help for open says :-

    You are not allowed to "open" to a command that pipes both in and out, + but see IPC::Open2, IPC::Open3,...

    So it's no great surprise that it doesn't work.

      Thank you for your response.

      I will try it with IPC::Open3.

      I'm trying to create a simple example for IPC::Open3. Should I create a separate post relating to getting Open3 to work?

      Below is code I am trying and here is the output I get:

      OUTPUT:
      err-> /bin/ls: write error: Bad file descriptor retval-> 512
      CODE:
      #!/usr/bin/perl use warnings; use strict; use IPC::Open3; print "Content-type: text/html\n\n"; print ' <!DOCTYPE HTML> <html lang="en"> <head> <title>IPC::Open3 Example</title> </head> <body>'; my $cmd = '/bin/ls -la'; my $pid = open3(\*WRITER, \*READER, \*ERROR, $cmd); while( my $output = <READER> ) { print "output-> $output\n"; } while(my $errout = <ERROR> ) { print "err-> $errout\n"; } waitpid($pid, 0 ) or die "$!\n"; my $retval = $?; print "retval-> $retval\n"; print " </body> </html>";

        Here is the final working solution I came up with. If there is anything I missed please let me know. I don't know if I am using "gensym()" correctly. The main thing that was an issue was rights on the server side.

        #!/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 : $c +md : $!\n"; my $string = '<html><head></head><body>Hello World!!!<br /><br /><br / +> IMG: <img id="image" src="data:image/gif;base64,R0lGODlhFwAPAKEAAP///w +AAAMzMzLi3tywAAAAAFwAPAAACQIyPqQjtD98RIVpJ66g3hgEYDdVhjThyXSA4aLq2rgp +78hxlyY0/ICAIBhu/HrEEKIZUyk4R1Sz9RFEkaIHNFgAAOw==" /> </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;
Re: perl wkhtmltopdf Error: Unable to write to destination
by choroba (Cardinal) on May 07, 2014 at 16:19 UTC
    Crossposted at StackOverflow. It is considered polite to inform about crossposting, so people not attending both sites don't waste their time hacking a solution to a problem already solved at the other end of the Internets.

    Also, googling for the error message brings up this link. Is it helpful?

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Thank you. I did not know about the rule about cross posting.

      The link related to windows specifically so I wasn't able to use it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1085338]
Approved by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-07-15 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.