Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Perl open PDF to Web browser

Posted on 2014-02-27
1
Medium Priority
?
935 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 2000 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 to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Six Sigma Control Plans

705 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