471,896 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,896 software developers and data experts.

Opening a pdf file in browser using perl

Hi all,

I am new to this site and this is my first post. But this issue is of high priority.

I am trying to print pdf file in browser using perl script.

Onclick on hyperlink a child window is opened along with through javascript i have redirected to perl.In perl i am doing something like this,

print "Expires -1\n\n";
print "Content-Disposition: Attachmet; filename="Path of the file";
print "Content-Type: application/pdf\n\n";

open(myfile,"path of file") or die("can't open file");

print "$_";

But the problem when i click on hyperlink, a popup comes and gives option to save or open.If i select "Save" pdf file is saved properly.But click on "Open" is giving an error message like "An error encoutered while openinig the file.File not found".

I am using HTTP 1.1 proxy connections for NTLM user authentication.

Also i am trying to execute this code in solaris box.

Any suggestions on this will be very helpful.

Thanks in advance.
Nov 24 '06 #1
8 15423
1,089 Expert 1GB
As GunnarH stated, your bug is in not setting binmode:

Expand|Select|Wrap|Line Numbers
  1. open(PDF, "path of pdf") or die "could not open PDF [$!]";
  2. binmode PDF;
  3. my $output = do { local $/; <PDF> };
  4. close(PDF);
  6. print "Content-Type: application/pdf\n";
  7. print "Content-Length: " .length($output) . "\n\n";
  8. print $output;
Nov 24 '06 #3
Hi Miller,

When I tried your code, I got the message below: Please help

CGI Error
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:

syntax error at D:\CGI\cgi-bin\samples\DisplayPDFfileonbrowser.pl line 17, near "close"
Execution of D:\CGI\cgi-bin\samples\DisplayPDFfileonbrowser.pl aborted due to compilation errors.

I think, there is something about this statement that can not execute :

my $output = do { local $/; <PDF> }

May 1 '07 #4
4,059 Expert 2GB
it needs a semi-colon on the end:

Expand|Select|Wrap|Line Numbers
  1. my $output = do { local $/; <PDF> };
May 1 '07 #5
1,089 Expert 1GB
Yep. A semi-colon is what it needs. I've edited the code to reflect this.

- Miller
May 1 '07 #6
Thanks guy for quick response. The script ran fine without any error. However, the browser display a whole bunch of binary code content, instead of the regular pdf format. Am I missing something?

May 1 '07 #7
Did you find out how to get the pdf displayed instead of binary code? I would like to know.
Aug 10 '09 #8
3,503 Expert Mod 2GB
@Brent Kerr
Brent, this thread is over 2 1/2 years old. There is no telling if the OP even remembers the issue or is even still surfing Bytes.com. In situations where the thread is this old, its usually better to PM the OP instead of bringing life back into a thread that has been dormant for such a long time.


Aug 10 '09 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by D. Alvarado | last post: by
4 posts views Thread by Vishal | last post: by
16 posts views Thread by idorjee | last post: by
3 posts views Thread by rajiv07 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.