English 中文(简体)
• 如何使用Perl向国防军提款
原标题:How to Render a PDF using Perl

能否在使用PERL的浏览器中投出一个 pdf? 我的闪电申请是寄送已久的pdf双通到永久的。 pdf来自AlivePDF。

#!C:Perlinperl.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/pdf
";
print "Content-Length: " . length($pdf) . "
";
print "Content-Disposition :$method

";
print $pdf;

The problem is that I want to actually render what a pdf will look like. I can save that binary code and open it manually in Adobe Reader and it renders properly.

我愿意在浏览器上发言,但我不知道如何做到这一点。

Currently the output (what the browser displays), looks like this:

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,它就非常有效。

问题回答

I do not have the Flash app that creates the PDF in the request body, but I verified it against the output of a static resource with the same response headers. Content-Disposition is the crucial one. This was tested in Konqueror with the Okular KPart and works, I fully expect other browsers/plug-in combinations to also work.

#!/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);

了解你是mixing GET and POST参数。 • 学习如何撰写安全科学、技术和创新方案。

您需要在以下的吉大港山区负责人中加入

print "Content-Transfer-Encoding: binary
";

The following is working for me to read a pdf file and display it:

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/pdf
";
        print "Content-Length: $size
";
        print "$method
";
        print "Content-Transfer-Encoding: binary

"; # blank line to separate headers

        print $pdf;

}

原来的法典中也可增加同样的职能,并应当这样做:

#!C:Perlinperl.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/pdf
";
    print "Content-Length: $size
";
    print "$method
";
    print "Content-Transfer-Encoding: binary

"; # blank line to separate headers

    print $pdf;

}




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签