在embperl中,我试图用IPC :: Open3模块调用wkhtmltopdf。
我从wkhtmltopdf获得输出(感谢ikegami),但没有输入到wkhtmltopdf。
这与此问题有关:perl / embperl — IPC::Open3
以下是代码:
[-
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 +]
答案 0 :(得分:0)
STDIN的想法相同,但fd 0而不是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
);
...
这假设fd 0和1关闭,就像这里的情况一样。