Perl/CGI and PDF problem

B

byrapaneni

Hello all;
I have gathered information from this group and several other groups to
write a script to display a pdf document in the browser window. Here is
my script;
==================================================================
#!/bin/perl

use CGI qw:)standard *table);

$q = new CGI;

my $buffer = undef;
my $pdfFile = 'test.pdf';
my $mimeType = 'text/html';


if (! -e $pdfFile){
&die_nice("Cannot find file to open - ". $pdfFile);
}

if (! -r $pdfFile){
&die_nice("Unable to read file - ". $pdfFile);
}

if ((-e $pdfFile) && (-r $pdfFile)){
open(pdfFile, '<', $pdfFile) || &die_nice("Cannot open - ".$pdfFile);
$mimeType = 'application/pdf';
}

print $q->header(-expires=>'now', -type=>$mimeType);

binmode pdfFile;
binmode STDOUT;

while (read(pdfFile, $buffer, 4096, 0)) {
print $buffer;
}

close(pdfFile);

sub die_nice{
my $error = shift;
print $q->header(-expires=>'now', -type=>$mimeType);

print "<dl>";
print "<dt>Some error occurred:</dt>";
print "<dd><strong>$error</strong></dd>";
print "</dl>";

exit;
}
==================================================================

Case 1:
This scipt returns the pdf document to the browser correctly.
This script doesn't return anything If I change the file name to
something else and execute it.
But this script returns the error message if executed from a new
browser window.

Case 2:
If I execute this scrit from the web browser with incorrect file name,
I can see the proper error message. Now, if I change the file name back
to it's original name and execute the script in the same window I end
with raw data.

I think the browser saves the content-type when the script invoked for
the first time and saves it. Could some one please help me fix this
problem? Thanks in advance.

Regards,
Sri.
 
K

Keith Keller

["Newsgroups:" header set to comp.lang.perl.misc.]

==================================================================
#!/bin/perl

Is this really where your perl is?
use CGI qw:)standard *table);

Missing: use strict; use warnings;
$q = new CGI;

Fortunately, you only need to my this to get your script working under
use strict:

my $q = new CGI;

[remainder of code snipped]

You should also not use & on subroutine calls unless you know why you
need it. Search perldoc perlsub for more details.
Case 1:
This scipt returns the pdf document to the browser correctly.
This script doesn't return anything If I change the file name to
something else and execute it.
But this script returns the error message if executed from a new
browser window.

Case 2:
If I execute this scrit from the web browser with incorrect file name,
I can see the proper error message. Now, if I change the file name back
to it's original name and execute the script in the same window I end
with raw data.

I think the browser saves the content-type when the script invoked for
the first time and saves it. Could some one please help me fix this
problem? Thanks in advance.

If you think the browser saves the content-type, then you have a browser
problem, which Perl can not help you solve. Firefox 1.5 on linux
displays suitable output no matter what: if the file doesn't exist, I
see your error message, and if the file exists, I'm asked to save a PDF.
So it seems clear that the problem is with the browser, not Perl (and
*certainly* not with your PDF, so crossposting to a PDF newsgroup was
even less appropriate than posting here).

--keith
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top