Solved

Perl open PDF to Web browser

Posted on 2014-02-27
1
778 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now