Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

displaying pdf

by Anonymous Monk
on Oct 21, 2002 at 17:22 UTC ( [id://206882]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am using a perl cgi script to display pdf files to a web browser. The pdf files are /not/ physically on the server, for security concerns. The job of the perl script it mearly authentication and display.

Authentication works fine, but displaying the PDF does not. using ggv there are many error messages, and on windows adobe acrobat just hangs forever when opening from the web browser. If i replace the PDF file with an HTML file (and change the content type back to text/html, it displays the html fine. Am i missing something while displaying the PDFs? (note: the pdf's work fine when opened directly from a local file system ... they just don't work when viewed from the browser or saved to the local file system from the browser). the relevant code follows:
print "Content-Type: application/pdf\n\n"; open(FILE,"$file") or &error(@!); foreach my $line (<FILE>) { print $line; } close(FILE);

Replies are listed 'Best First'.
Re: displaying pdf
by alfie (Pilgrim) on Oct 21, 2002 at 19:44 UTC

    I wouldn't use the lineread mode with <FILE> for that, it might end bad. I am not sure but I guess that is your problem.

    Rather try the following thing, which works perfectly for me with .jpg files:

    while (read FILE, my $data, 1024) { print $data; }

    You can of course vary the length of your read buffer in the while loop to your likes, to optimize memory usage/speed.

    HTH & HAND.
    --
    use signature; signature(" So long\nAlfie");
      May i suggest replacing read with sysread, and 1024 with the value returned from (stat($file))[11]?

      The reason for doing so is that read is already buffered, and you're buffering it a second time... You should avoid it as you probably don't need it, and read directly...
      About the size - "11 blksize preferred block size for file system I/O " - to quote from perldoc -f stat

      open FILE,$file or die "$!"; my $len = (stat($file))[11]; while (sysread FILE, my $data, $len){ syswrite STDOUT, $data };
      Would be my code of choice.
      print is also bufferred I/O, and in the family of read, write, readline, getc, eof, printf seek and tell. It is also bufferred. It may or may not be a good idea to use buffering for writing either. Probably the latter if you're on the same filesystem.

      I think that the most commonly used buffer size in pipes is 4096 bytes, and there's probably a reason for it too.

      -nuffin
      zz zZ Z Z #!perl
Re: displaying pdf
by admiraln (Acolyte) on Oct 21, 2002 at 18:03 UTC
    First off I think that the pdf file should be open and written as a binary file.

    Disclaimer

      This code almost works. The browser kickstarts a download when the page is loaded and asks for a acrobat or a file save.

      If I select Acrobat a warning about a damaged file comes up and is being repaired. A blank pdf file is displayed.

      I will work it some more or some one can spot my error.

      #!perl -w $| = 1; print "Content-Type: application/pdf\n\n"; open(FILE,"dental.pdf") or die("$!"); binmode(FILE); while (sysread(FILE,$buffer,1024)) { print STDOUT $buffer; }

      Disclaimer

        Try binmode STDOUT;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://206882]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2024-04-24 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found