By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,176 Members | 984 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,176 IT Pros & Developers. It's quick & easy.

Need help with a simple Perl script

P: n/a
First of all, I am a rank amateur at Perl. Here is my problem: I have a
hundred or more files in a directory on a web server (let's call it
'Library'). Each file is a pdf file and is named 'yyyymmm.pdf' where yyyy
is the year (i.e. 2007) and mmm is the first 3 letters of the month (i.e.
Jan). So a typical file name looks like '2007Jan.pdf'.

I wrote a simple html page using FORM that allows the user to select the
year and the month and then press the SUBMIT button and I want the
respective pdf file returned into the users browser. The problem is, I
don't know how to return a pdf file to the browser.

Here's what I have so far:

#!/usr/local/bin/perl -wT
use strict;
use CGI ':standard';

# declare variables...
my $year;
my $month;
my $pdffile;

# get the parameters...
$year = param('Year');
$month = param('Month');

# construct the relative pathname to the actual PDF file
$pdffile = '../Library/'.$Year.$Month.'.pdf';

print 'Content-type: application/pdf\n\n';

This is where I'm stuck. Can someone push me in the right direction. I
would think it should be trivial. I just don't know how to procede.

Thanks in advance...

Dick

Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Wed, 28 May 2008 22:40:28 -0400, Dick Sutton wrote:
First of all, I am a rank amateur at Perl. Here is my problem: I have a
hundred or more files in a directory on a web server (let's call it
'Library'). Each file is a pdf file and is named 'yyyymmm.pdf' where
yyyy is the year (i.e. 2007) and mmm is the first 3 letters of the month
(i.e. Jan). So a typical file name looks like '2007Jan.pdf'.

I wrote a simple html page using FORM that allows the user to select the
year and the month and then press the SUBMIT button and I want the
respective pdf file returned into the users browser. The problem is, I
don't know how to return a pdf file to the browser.

Here's what I have so far:

#!/usr/local/bin/perl -wT
use strict;
use CGI ':standard';

# declare variables...
my $year;
my $month;
my $pdffile;

# get the parameters...
$year = param('Year');
$month = param('Month');

# construct the relative pathname to the actual PDF file $pdffile =
'../Library/'.$Year.$Month.'.pdf';

print 'Content-type: application/pdf\n\n';

This is where I'm stuck. Can someone push me in the right direction. I
would think it should be trivial. I just don't know how to procede.

Thanks in advance...

Dick
Grab the file content and print it out after your type header...

open(FILE,"<$pdffile");
@file = <FILE>;
close(FILE);

print "Content-Type:application/pdf\n\n";
print @file;

- Netlurker
Jun 27 '08 #2

P: n/a

"Netlurker" <no*****@foo.barwrote in message
news:qZ********************@fe04.news.easynews.com ...
On Wed, 28 May 2008 22:40:28 -0400, Dick Sutton wrote:
>First of all, I am a rank amateur at Perl. Here is my problem: I have a
hundred or more files in a directory on a web server (let's call it
'Library'). Each file is a pdf file and is named 'yyyymmm.pdf' where
yyyy is the year (i.e. 2007) and mmm is the first 3 letters of the month
(i.e. Jan). So a typical file name looks like '2007Jan.pdf'.

I wrote a simple html page using FORM that allows the user to select the
year and the month and then press the SUBMIT button and I want the
respective pdf file returned into the users browser. The problem is, I
don't know how to return a pdf file to the browser.

Here's what I have so far:

#!/usr/local/bin/perl -wT
use strict;
use CGI ':standard';

# declare variables...
my $year;
my $month;
my $pdffile;

# get the parameters...
$year = param('Year');
$month = param('Month');

# construct the relative pathname to the actual PDF file $pdffile =
'../Library/'.$Year.$Month.'.pdf';

print 'Content-type: application/pdf\n\n';

This is where I'm stuck. Can someone push me in the right direction. I
would think it should be trivial. I just don't know how to procede.

Thanks in advance...

Dick

Grab the file content and print it out after your type header...

open(FILE,"<$pdffile");
@file = <FILE>;
close(FILE);

print "Content-Type:application/pdf\n\n";
print @file;

- Netlurker
Thanks, Netlurker, I'll give that a try. Then I'll try to figure out what
each line does. ha-ha. I told you I was a novice Perl scripter.

Dick

Jun 27 '08 #3

P: n/a
>>>>"Netlurker" == Netlurker <no*****@foo.barwrites:

Netlurkerprint "Content-Type:application/pdf\n\n";

You're missing the space after the colon. Don't do that.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<me****@stonehenge.com<URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
** Posted from http://www.teranews.com **
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.