embperl - 使用 IPC::Open3 调用 wkhtmltopdf.标准输入不工作 [英] embperl - Using IPC::Open3 to call wkhtmltopdf. STDIN Not working

查看:38
本文介绍了embperl - 使用 IPC::Open3 调用 wkhtmltopdf.标准输入不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 embperl 中,我尝试使用 IPC::Open3 模块调用 wkhtmltopdf.

我从 wkhtmltopdf 得到输出(感谢 ikegami ),但没有输入到 wkhtmltopdf.

这与这个问题有关:perl/embperl — IPC::Open3>

代码如下:

[-使用警告;使用严格;使用 IPC::Open3;使用POSIX;使用符号;我的 $cmd = '/usr/local/bin/wkhtmltopdf - -';我的 $pdf = '';我的 $string = '<!DOCTYPE html><头><title>Hello World</title><身体>你好,世界!!!';我的 $fhOUT = gensym();open($fhOUT, '>', '/dev/null') 否则死 $!;dup2(fileno($fhOUT), 1) 否则死 $!如果文件号($fhOUT) != 1;本地*标准输出;open(STDOUT, '>&=', 1) or die $!;我的 $pid = open3(*HIS_IN, *HIS_OUT, *HIS_ERR, $cmd) 或者死无法运行 cmd : $cmd : $!\n";打印 HIS_IN $string;关闭(HIS_IN);而( <HIS_OUT> ) {$pdf .= $_;}waitpid($pid, 0 ) 或死 "$!\n";我的 $retval = $?;# 打印 "retval-> $retval<br/>\n";$http_headers_out{'Content-Type'} = "application/pdf";$http_headers_out{'Content-Disposition'} = "附件;文件名=pdfTest.pdf";$escmode = 0;-][+ $pdf +]

解决方案

与 STDIN 相同的想法,但 fd 0 而不是 1.

 open(my $fhIN, '<', '/dev/null') or die $!;dup2(fileno($fhIN), 0) 否则死 $!如果文件号($fhIN) != 0;本地 *STDIN;open(STDIN, '<&=', 0) or die $!;open(my $fhOUT, '>', '/dev/null') 否则死 $!;dup2(fileno($fhOUT), 1) 否则死 $!如果文件号($fhOUT) != 1;本地*标准输出;open(STDOUT, '>&=', 1) or die $!;我的 $pid = open3(本地 *HIS_IN,本地 *HIS_OUT,'>&STDERR',$cmd);...

这里假设 fd 0 和 1 是闭合的,就像这里的情况一样.

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 +]

解决方案

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.

这篇关于embperl - 使用 IPC::Open3 调用 wkhtmltopdf.标准输入不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆