Solved

Perl open PDF to Web browser

Posted on 2014-02-27
1
861 Views
Last Modified: 2014-03-08
Hi guys,

I'm curious how to impliment displaying a PDF to a browser securely and allowing the user to save it locally.

Here's my example.  I am developing a perl web application in conjunction with MySQL to allow parents of students to complete registraion forms on line to print them and to bring into registration.  My program takes standard HTML form data from their input, and put's it into a database, and then at the end I use an HTML template that is passed to PrinceXML which makes it into a PDF and stores the file to a directory outside of the webfolder.  I then use a script to read the the PDF file and output it's contents to the web-browser.  

That part works fine except for one caveat that if they want to save it locally the default "Save as" uses the script name "displayForms.pl" that is in the URL.

 http://registration.yadda.com/cgi-bin/displayForms.pl

Well of course they can save it, but they would need to know to change the extension to be .pdf when saving, or when they try and open it, the extension will be wrong and parent's using windows won't be able to view it and I need a way to fix that.

Because I'm saving the document outside of the web directory for security reasons because I don't want forms publically available,  I can't use a simple web link for parents to download them, how can I both display it securely, and offer them the ability to save it "no-brainer" style with a .pdf extension by default?

Code for displaying PDF to browser
-------------------------------------------------

print "Content-type: application/pdf\n\n";
print "Content-Disposition: inline; filename=out.pdf\n\n";

my $pdfFile = '/path/to/pdf/outside/web/directory/myforms.pdf';

open(my $pdf, '<', $pdfFile);
binmode $pdf;
binmode STDOUT;
my $buffer;
while (read($pdf, $buffer, 1024, 0)) {
   print $buffer;
}
close($pdf);
0
Comment
Question by:NoodlesWIU
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 15

Accepted Solution

by:
Giovanni Heward earned 500 total points
ID: 39891816
The Content-disposition HTTP header generally guides browser behavior (at the discretion of browser.)  In my implementations of PRINCE, I generally create a mode function which allows for either inline or attachment, depending on whether the client is intending to view the PDF within the browser or to download.

Additionally, providing a Content-Length: header can alter the behavior of the browser and encourage it to behave as expected.

Here's an example in PHP, which you can alter for Perl.
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($filename)) . ' GMT');
header('Content-Type: application/pdf');
header('Content-Language: en');
header("Content-Disposition: $mode; filename=\"$year $make $model ($vin) AutoCheck.pdf\"");
header('Content-Length: ' . filesize($filename));
header('Content-MD5: ' . base64_encode(md5_file($filename)));
header('Accept-Ranges: bytes');
@readfile($filename);

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question