如何使用Perl渲染PDF



是否可以使用PERL在浏览器中呈现pdf?我有一个flash应用程序,它将渲染的pdf二进制文件发送到perl。pdf是从AlivePDF生成的。

#!C:Perlbinperl.exe
##
BEGIN { $ENV{PATH} = ''; delete @ENV{ 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; }
use strict;
use warnings;
no warnings qw (redefine closure);
use CGI;
my $CGI = new CGI();
#name=generated.pdf&method=inline these are passed via the URL and are in the environmental variable QUERY_STRING
my %nv_pairs = map{my @tmp = split(/=/,$_);$tmp[0] => $tmp[1] }split(/&/,$ENV{QUERY_STRING});
my $name = $nv_pairs{name};
my $method = $nv_pairs{method};
#Raw Data is stored in the POST Parameter POSTDATA
my $pdf = $CGI->param('POSTDATA');
print "Content-Type: application/pdfrn";
print "Content-Length: " . length($pdf) . "rn";
print "Content-Disposition :$methodnn";
print $pdf;

问题是,我想实际呈现pdf的样子。我可以保存二进制代码,然后在Adobe Reader中手动打开它,它就能正确渲染。

我想让它在浏览器中渲染,但我不知道如何将其渲染。

当前的输出(浏览器显示的内容)如下所示:

Content-Type: application/pdf
Content-Length: 432785
Content-disposition:inline; filename="test.pdf"
%PDF-1.5
1 0 obj
<</Type /Pages
/Kids [3 0 R 5 0 R]
/Count 2>>
endobj
3 0 obj
<</Type /Page
/Parent 1 0 R
/MediaBox [0 0 612.00 792.00]
/Resources 2 0 R

这只是显示文件的一部分,但我希望这能有所帮助。我不希望代码显示出来,我希望它看起来像图形。如果我下载这个文件,并将扩展名更改为.pdf,它就可以完美地工作。

我没有在请求主体中创建PDF的Flash应用程序,但我根据具有相同响应标头的静态资源的输出进行了验证。CCD_ 1是最关键的一个。这是用Okular KPart在Konqueror中测试过的,我完全希望其他浏览器/插件组合也能工作。

#!/usr/bin/perl -T
# ↑↑↑↑↑
# on Windows you can just write …
#!perl -T
# … instead, using the Unix shebang however does no harm
use strict;
use warnings FATAL => 'all';
use CGI qw();
use IO::File qw();
# delete @ENV{qw(BASH_ENV CDPATH ENV IFS PATH)};
# ↑↑↑↑↑
# Cleaning path is required for taint-checked programs
# that want to run other programs. It does not affect anything here,
# so I commented it out.
my $c = CGI->new;
# untaint data coming from outside
my ($name) = defined $c->url_param('name') ?
    $c->url_param('name') =~ /A ([a-zA-Z_-]{1,40}.pdf) z/msx : ();
my ($method) = defined $c->url_param('method') ?
    $c->url_param('method') =~ /A (attachment|inline) z/msx : ();
die 'invalid parameters' unless $name or $method;
# FIXME: untaint blindly because I don't know how to validate PDF
my ($pdf) = $c->param('POSTDATA') =~ /(.*)/msx;
STDOUT->binmode(':raw');
STDOUT->print($c->header(
    -Content_Type        => 'application/pdf',
    -Content_Length      => length($pdf),
    -Content_Disposition => qq($method; filename="$name"),
));
STDOUT->print($pdf);

请注意,您正在混合使用GET和POST参数。了解如何编写安全的CGI程序。

您需要添加以下HTTP标头

print "Content-Transfer-Encoding: binaryn";

以下是我阅读pdf文件并显示它的方法:

use strict;
use warnings;
my $file = "discover.pdf"; # a pdf I happen to have
my $pdf;
open (my $fh, $file);
binmode $fh; # set the file handle to binary mode
while (<$fh>){ $pdf .= $_; } # read it all into a string;
close ($fh);
showPdf($pdf); # call the display function
sub showPdf {
        my $pdf = shift;
        my $file = shift || "new.pdf"; # if no name is given use this
        my $method = shift || "Content-disposition:inline; filename='$file'"; # default method
        my $size = length($pdf);
        print "Content-Type: application/pdfn";
        print "Content-Length: $sizen";
        print "$methodn";
        print "Content-Transfer-Encoding: binarynn"; # blank line to separate headers
        print $pdf;
}

相同的功能可以添加到原始代码中,并且应该像这样工作:

#!C:Perlbinperl.exe
##
BEGIN { $ENV{PATH} = ''; delete @ENV{ 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; }
use strict;
use warnings;
no warnings qw (redefine closure);
use CGI;
my $CGI = new CGI();
#name=generated.pdf&method=inline these are passed via the URL and are in the environmental variable QUERY_STRING
my %nv_pairs = map{my @tmp = split(/=/,$_);$tmp[0] => $tmp[1] }split(/&/,$ENV{QUERY_STRING});
my $name = $nv_pairs{name};
my $method = $nv_pairs{method};
#Raw Data is stored in the POST Parameter POSTDATA
my $pdf = $CGI->param('POSTDATA');
showPdf($pdf, $name, $method);
sub showPdf {
    my $pdf = shift;
    my $file = shift || "new.pdf"; # if no name is given use this
    my $method = shift || "Content-disposition:inline; filename='$file'"; # default method
    my $size = length($pdf);
    print "Content-Type: application/pdfn";
    print "Content-Length: $sizen";
    print "$methodn";
    print "Content-Transfer-Encoding: binarynn"; # blank line to separate headers
    print $pdf;
}

相关内容

  • 没有找到相关文章

最新更新