Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 966
  • Last Modified:

Perl open PDF to Web browser

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
NoodlesWIU
Asked:
NoodlesWIU
1 Solution
 
Giovanni HewardCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now