简体   繁体   English

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

[英]How do I output a PDF file using Perl's Mason?

I have to write taking a code from the .pdf file, and copy it to anyother pdf file.我必须从 .pdf 文件中编写一个代码,然后将其复制到任何其他 pdf 文件中。 The code i have written to open the file is given as under :我编写的用于打开文件的代码如下:

<%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>

where filename is the name for the file to save the pdf content to and "fullname" is the pdf getting the content from其中 filename 是将 pdf 内容保存到的文件的名称,“fullname”是从中获取内容的 pdf

You're currently reading in a text file.您当前正在阅读文本文件。 You should binmode first for non-text (like PDF).对于非文本(如 PDF),您应该首先使用binmode And, never use indirect object syntax .并且,永远不要使用间接对象​​语法

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

$fh->binmode(1);

So try something like this, adapted from the 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM