How to display a pdf file using CGI

Discussion in 'Perl Misc' started by perlUSER, Apr 14, 2006.

  1. perlUSER

    perlUSER Guest

    Hello all;
    Learned a lot from this group. Thanks to everyone who answered my
    questions.

    Yet another question.
    I am creating web pages to display a table lisitng reports. When a user
    clicks the report name corresponding report displayed in the browser.
    When the report is displayed the url is the location of the file name.
    I am wondering whether perl/CGI can read a pdf file and display it in
    the browser. This is to hide the report location on the server.

    I was searching for hints for the past couple of days and my search
    didn't yield anything.

    I would appreciate if someone posts a script if they have done this in
    the past.
    Please advise the best way to achieve this.

    Best regards,
    Sri.
     
    perlUSER, Apr 14, 2006
    #1
    1. Advertising

  2. perlUSER

    Matt Garrish Guest

    "perlUSER" <> wrote in message
    news:...
    > Hello all;
    > Learned a lot from this group. Thanks to everyone who answered my
    > questions.
    >
    > Yet another question.
    > I am creating web pages to display a table lisitng reports. When a user
    > clicks the report name corresponding report displayed in the browser.
    > When the report is displayed the url is the location of the file name.
    > I am wondering whether perl/CGI can read a pdf file and display it in
    > the browser. This is to hide the report location on the server.
    >


    The same you'd read any other file and print it out to a browser:

    print "Content-type: application/pdf\n\n";

    my $pdfFile = '/test.pdf';

    open(my $pdf, '<', $pdfFile);
    binmode $pdf;
    binmode STDOUT;
    my $buffer;
    while (read($pdf, $buffer, 1024, 0)) {
    print $buffer;
    }
    close($pdf);

    Error checking and handling left to you.

    Matt
     
    Matt Garrish, Apr 14, 2006
    #2
    1. Advertising

  3. perlUSER

    perlUSER Guest

    Here is my code runs fine in the console but can't get it to work in
    the browser;

    #!/usr/local/bin/perl

    my $pdfFile =
    "C:\\reports\\monthly\\06_04_08\\009NJS01_060315_0515.pdf";

    print "Content-type: application/pdf\n\n";


    open(my $pdf, '<', $pdfFile);
    binmode $pdf;
    binmode STDOUT;
    my $buffer;
    while (read($pdf, $buffer, 1024, 0)) {
    print $buffer;


    }


    close($pdf);

    ==================
    Any suggestions where I am going wrong?

    Thanks in advance.

    Sri
     
    perlUSER, Apr 15, 2006
    #3
  4. perlUSER wrote:
    > Here is my code runs fine in the console but can't get it to work in
    > the browser;


    Your Question is Asked Frequently: perldoc -q 500
    "My CGI script runs from the command line but not the browser."

    > ==================
    > Any suggestions where I am going wrong?


    Not following the advise in the FAQ?

    jue
     
    Jürgen Exner, Apr 15, 2006
    #4
  5. perlUSER <> wrote:

    > open(my $pdf, '<', $pdfFile);


    > Any suggestions where I am going wrong?



    Not checking the return value from open().


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 15, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SG
    Replies:
    6
    Views:
    30,692
    ricuzzo
    Jan 15, 2008
  2. Ricardo Pog
    Replies:
    1
    Views:
    468
    Austin Ziegler
    Mar 26, 2008
  3. Sean Nakasone
    Replies:
    1
    Views:
    411
    Farrel Lifson
    Apr 14, 2008
  4. Jimbo
    Replies:
    2
    Views:
    230
    Jimbo
    Jan 26, 2006
Loading...

Share This Page