如何使用Perl的Mason输出PDF文件?

时间:2012-11-12 20:04:12

标签: perl file-io mason

我必须编写.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

1 个答案:

答案 0 :(得分:2)

您目前正在阅读文本文件。对于非文本(例如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;