Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Web Form search and open pdf

Reply
Thread Tools

Web Form search and open pdf

 
 
dbmeyers23@yahoo.com
Guest
Posts: n/a
 
      12-08-2004
All,

I'm trying to create a "simple" web-based application that will allow
users to search for pdf files, and when the files exist, the browser
opens the .pdf in the browser. I can get the form to find the .pdf
and open it on a PC, but the filename is still the name of the ".cgi"
script.

I also cannot get my basic version to work on the Macintosh platform.

I've scoured the web for answers, and have pieced together some code
from different sources, but I'm missing two crucial pieces....having
the file show up with the real name (not script.cgi) and getting this
to work on Macintosh OS9 browsers(file is corrupted).

Here's my code:

#!/usr/bin/perl -wT
###########################Find pdf ads based on user submission
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);
use strict;

my $files_location;
my $ID;
my @fileholder;

$files_location = "/usr/local/apache2/pdfads";

$ID = param('pdfname');
if ($ID eq '') {
print "Content-type: text/html\n\n";
print "You must specify a file to download.<br>";
print "<br>";
print "<form name=pdf method=post action=../cgi-bin/download.cgi>";
print "New pdf search<br>";
print "<br>";
print "<table>";
print "<tr><td><b>Ad Number:</b><input type=text maxlength=30
name=pdfname size=20></td></tr>";
print "</table>";
print "<input type=submit name=Submit value=View PDF size=15>";
print "</form>";
exit;
}

if (-e "$files_location/$ID")
{
open(DLFILE, "<$files_location/$ID") || die ("Cannot open PDF ad $ID,
try again.<br>");
@fileholder = <DLFILE>;
close (DLFILE) || Error ('close', 'file');
###print "Content-Type:application/x-download\n";
print "Content-Type:application/pdf\n";
print "Content-Disposition:inline; filename=$ID\n\n";
print @fileholder
}
print "Content-type: text/html\n\n";
print "Cannot find PDF Ad $ID, please try again<br>";
print "<form name=pdf method=post action=../cgi-bin/download.cgi>";
print "<br>";
print "<table>";
print "<tr><td><b>Ad Number:</b><input type=text maxlength=30
name=pdfname size=20></td></tr>";
print "</table>";
print "<input type=submit name=Submit value=View PDF size=15>";
print "</form>";
exit;

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      12-08-2004
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> All,
>
> I'm trying to create a "simple" web-based application that will allow
> users to search for pdf files, and when the files exist, the browser
> opens the .pdf in the browser. I can get the form to find the .pdf
> and open it on a PC, but the filename is still the name of the ".cgi"
> script.
>
> I also cannot get my basic version to work on the Macintosh platform.
>
> I've scoured the web for answers, and have pieced together some code
> from different sources, but I'm missing two crucial pieces....having
> the file show up with the real name (not script.cgi) and getting this
> to work on Macintosh OS9 browsers(file is corrupted).


If you just want the PDF displayed in the User's browser's location bar,
then you should have your CGI program redirect to the PDF, rather than
print the contents of the PDF.

http://search.cpan.org/~lds/CGI.pm-3...RECTION_HEADER

Paul Lalli

 
Reply With Quote
 
 
 
 
Eric Schwartz
Guest
Posts: n/a
 
      12-08-2004
"Paul Lalli" <(E-Mail Removed)> writes:
> If you just want the PDF displayed in the User's browser's location bar,
> then you should have your CGI program redirect to the PDF, rather than
> print the contents of the PDF.


That only works if the PDFs are under the webserver's document root,
and there are many good reasons you may not want to do that. If
that's not the case, then you do need to use your CGI program to print
it to the browser. In which case to set the name, I have always found
setting the filename in the MIME Content-Disposition header to do the
trick. This might look like (untested);

#!/usr/bin/perl
use warnings;
use strict;
use CGI;
use File::Basename;

my $pdfpath = '/path/to/filename.pdf'
open my $pdf, '<', $pdfpath or die "Can't open $pdfpath: $!";

print header(-type => 'application/pdf',
-content_disposition => 'inline; filename='.basename($pdfpath));
print while (<$pdf>);

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
 
Reply With Quote
 
Michael Budash
Guest
Posts: n/a
 
      12-09-2004
In article <(E-Mail Removed)>,
Eric Schwartz <(E-Mail Removed)> wrote:

> "Paul Lalli" <(E-Mail Removed)> writes:
> > If you just want the PDF displayed in the User's browser's location bar,
> > then you should have your CGI program redirect to the PDF, rather than
> > print the contents of the PDF.

>
> That only works if the PDFs are under the webserver's document root,
> and there are many good reasons you may not want to do that. If
> that's not the case, then you do need to use your CGI program to print
> it to the browser. In which case to set the name, I have always found
> setting the filename in the MIME Content-Disposition header to do the
> trick. This might look like (untested);
>
> #!/usr/bin/perl
> use warnings;
> use strict;
> use CGI;
> use File::Basename;
>
> my $pdfpath = '/path/to/filename.pdf'
> open my $pdf, '<', $pdfpath or die "Can't open $pdfpath: $!";
>
> print header(-type => 'application/pdf',
> -content_disposition => 'inline; filename='.basename($pdfpath));
> print while (<$pdf>);
>
> -=Eric


redirect to a script that pushes back the pdf (as the o.p. did), but
pass the pdf name in the path info, thusly:

http://www.domain.com/path/to/script/pdfname

every browser i've tried will then use the pdf name as the file name,
since it's at the end of the url. you can access the pdf name in
$ENV{PATH_INFO}. it will likely have a '/' prefix, so you'll need to
deal with that.

works great for me for years.

--
Michael Budash
 
Reply With Quote
 
Eric Schwartz
Guest
Posts: n/a
 
      12-09-2004
Michael Budash <(E-Mail Removed)> writes:
> redirect to a script that pushes back the pdf (as the o.p. did), but
> pass the pdf name in the path info, thusly:
>
> http://www.domain.com/path/to/script/pdfname


That works too, but you probably don't want to use the full path to
the pdf-- otherwise, you've just opened up a huge security risk. Not
that you advocated that, just pointing it out in case someone reading
your suggestion didn't think of it.

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
 
Reply With Quote
 
Michael Budash
Guest
Posts: n/a
 
      12-09-2004
In article <(E-Mail Removed)>,
Eric Schwartz <(E-Mail Removed)> wrote:

> Michael Budash <(E-Mail Removed)> writes:
> > redirect to a script that pushes back the pdf (as the o.p. did), but
> > pass the pdf name in the path info, thusly:
> >
> > http://www.domain.com/path/to/script/pdfname

>
> That works too, but you probably don't want to use the full path to
> the pdf-- otherwise, you've just opened up a huge security risk. Not
> that you advocated that, just pointing it out in case someone reading
> your suggestion didn't think of it.
>
> -=Eric


absolutely. the only indication of the pdf shold be its name. the script
can handle push its content back from its actual location on the server.

--
Michael Budash
 
Reply With Quote
 
dbmeyers23@yahoo.com
Guest
Posts: n/a
 
      12-10-2004
Thanks everyone, I have an acceptable version of this working.
Michael Budash's suggestion of the redirection solved the problem of
the file showing up with the name as "script.cgi" instead of "xxx.pdf".
This also allows Macs (OS9-OSX browsers) to open the pdf's.....

 
Reply With Quote
 
Michael Budash
Guest
Posts: n/a
 
      12-10-2004
In article <(E-Mail Removed) .com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Thanks everyone, I have an acceptable version of this working.
> Michael Budash's suggestion of the redirection solved the problem of
> the file showing up with the name as "script.cgi" instead of "xxx.pdf".
> This also allows Macs (OS9-OSX browsers) to open the pdf's.....
>


glad to help. like i said, that method has never failed me.

--
Michael Budash
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Postscript to PDF with pdf-tools, pdf-writer, or other Sean Nakasone Ruby 1 04-14-2008 09:13 PM
PDF::Writer, create pdf and insert in other pdf file. Ricardo Pog Ruby 1 03-26-2008 08:24 PM
Open linked PDF-Files from a PDF-File Paul Kuebler ASP .Net 8 08-18-2003 04:42 PM
Re: Open linked PDF-Files from a PDF-File Carl Prothman [MVP] ASP .Net 4 08-12-2003 09:26 AM



Advertisments