问题
From within embperl, I am trying to call wkhtmltopdf with the IPC::Open3 module.
I get output (thanks to ikegami ) from wkhtmltopdf but no input is going to wkhtmltopdf.
This is related to this question: perl / embperl — IPC::Open3
Here is the code:
[-
use warnings;
use strict;
use IPC::Open3;
use POSIX;
use Symbol;
my $cmd = '/usr/local/bin/wkhtmltopdf - -';
my $pdf = '';
my $string = '<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
Hello World!!!
</body>
</html>';
my $fhOUT = gensym();
open($fhOUT, '>', '/dev/null') or die $!;
dup2(fileno($fhOUT), 1) or die $! if fileno($fhOUT) != 1;
local *STDOUT;
open(STDOUT, '>&=', 1) or die $!;
my $pid = open3(*HIS_IN, *HIS_OUT, *HIS_ERR, $cmd) or die "could not run cmd : $cmd : $!\n";
print HIS_IN $string;
close(HIS_IN);
while( <HIS_OUT> ) {
$pdf .= $_;
}
waitpid($pid, 0 ) or die "$!\n";
my $retval = $?;
# print "retval-> $retval<br />\n";
$http_headers_out{'Content-Type'} = "application/pdf";
$http_headers_out{'Content-Disposition'} = "attachment; filename=pdfTest.pdf";
$escmode = 0;
-]
[+ $pdf +]
回答1:
Same idea for STDIN, but fd 0 instead of 1.
open(my $fhIN, '<', '/dev/null') or die $!;
dup2(fileno($fhIN), 0) or die $! if fileno($fhIN) != 0;
local *STDIN; open(STDIN, '<&=', 0) or die $!;
open(my $fhOUT, '>', '/dev/null') or die $!;
dup2(fileno($fhOUT), 1) or die $! if fileno($fhOUT) != 1;
local *STDOUT; open(STDOUT, '>&=', 1) or die $!;
my $pid = open3(
local *HIS_IN,
local *HIS_OUT,
'>&STDERR',
$cmd
);
...
This assumes fd 0 and 1 are closed, as is the case here.
来源:https://stackoverflow.com/questions/23838336/embperl-using-ipcopen3-to-call-wkhtmltopdf-stdin-not-working