Solved

Perl open PDF to Web browser

Posted on 2014-02-27
1
807 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
1 Comment
 
LVL 14

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 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