【问题标题】:How do I output a PDF file using Perl's Mason?如何使用 Perl 的 Mason 输出 PDF 文件?
【发布时间】:2012-11-12 20:04:12
【问题描述】:

我必须编写从 .pdf 文件中获取代码,并将其复制到任何其他 pdf 文件。我为打开文件而编写的代码如下所示:

<%args>
$fullname
$filename

</%args>
<%init>
use IO::File;

$r->content_type('application/pdf');
$r->header_out( 'Content-disposition' => "attachment; filename=$filename" );


my $tmpfile = $filename;
my $forread = new IO::File "< $fullname";

my @lines = <$forread>;


foreach my $key (@lines){ 
      print $key;
       }

return $fullname;

</%init>

其中 filename 是保存 pdf 内容的文件的名称,“fullname”是从

获取内容的 pdf

【问题讨论】:

    标签: perl file-io mason


    【解决方案1】:

    您当前正在阅读文本文件。对于非文本(如 PDF),您应该首先 binmode。而且,永远不要使用间接对象语法

    my $fh = IO::File->new($fullname, 'r');
    
    $fh->binmode(1);
    

    所以试试这样的东西,改编自Mason Book

    use Apache::Constants qw(OK);
    
    my $fh = IO::File->new($fullname, 'r');
    
    $fh->binmode(1);
    
    $m->clear_buffer; # avoid extra output (but it only works when autoflush is off)
    
    $r->content_type('application/pdf');
    $r->send_http_header;
    
    while ( my $data = $fh->getline ) {
        $m->print($data);
    }
    $fh->close;
    
    $m->abort;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      相关资源
      最近更新 更多