[英]How do I output a PDF file using Perl's 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
您當前正在閱讀文本文件。 對於非文本(如 PDF),您應該首先使用binmode
。 並且,永遠不要使用間接對象語法。
my $fh = IO::File->new($fullname, 'r');
$fh->binmode(1);
所以嘗試這樣的事情,改編自梅森書。
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.