Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems in downloading pdfs...

Status
Not open for further replies.

cerb

Programmer
Oct 15, 2002
10
GB
Hello guys.
I am writing a simple download script which allows to download from a protected area. This area is readable via script, but not via browser.
The mechanism I wish to use is simple:
- open the file (which can be .doc, .ppt, .eps, .pdf, .zip ie a typical binary document)
- revert the content of the file on a window (litterally "printing" it), as if the file was effectively downloaded by the user.

This mechanism is the only I can think of (not possibe to have a URL redirection, as the area is not accessible via browser as it is protected)

Big problem:
- this mechanism works perfectly for .zip files, for .doc files, for many other binaries, simply using the right mime header. But I cannot get to wrok at all with PDF files.
The think I get from PDF is a simple 2k file with part of the original PDF header.

Any suggestion?
Here is the code I am using ($file contains the full path to the file to be downloaded)

if (-B $file) { #binary file...

$file =~ m|(.*/)(.+)\.([^\.]+)$|;
my $ext = $3; my $mimetype;

#most common mime types
if (uc($ext) ne "PDF") { $mimetype="application/pdf";}
elsif (uc($ext) eq "DOC") { $mimetype="application/msword";}
elsif (uc($ext) eq "PPT") { $mimetype="application/powerpoint";}
elsif (uc($ext) eq "ZIP") { $mimetype="application/x-zip-compressed";}
elsif (uc($ext) eq "EXE") { $mimetype="application/octet-stream";}
elsif (uc($ext) eq "EPS") { $mimetype="application/postscript";}
elsif (uc($ext) eq "PS") { $mimetype="application/postscript";}
elsif (uc($ext) eq "GZ") { $mimetype="application/x-gzip";}
elsif (uc($ext) eq "LATEX") { $mimetype="application/x-latex";}
elsif (uc($ext) eq "TAR") { $mimetype="application/x-tar";}
elsif (uc($ext) eq "EXE") { $mimetype="application/octet-stream";}


open(INFILE, "$file") || die &img_not_found;
binmode(INFILE);
binmode(STDOUT);
print "Content-type: $mimetype\n\n";
while (<INFILE>) {
print $_;
}
close(INFILE);


} #binary mode file
 
[tt]#most common mime types
if (uc($ext) ne &quot;PDF&quot;) { $mimetype=&quot;application/pdf&quot;;} ## <== Shouldn't this be 'eq' rather than 'ne'?
[/tt]

jaa
 
Thanks for the reply.
You are correct in pointing out that. But it is not the problem. Yesterday I did many attempts with different versions of the code and the one I ended up pasting here had parts taken from a different method I tried...

So here is the code that gives problems with PDF. The code is recognising correctly the file extention, start the reading of the PDF and writes something to the output. But whatever the PDF in input, it simply writes 2kb with the header and binary characters. The resulting (downloaded) file is not correct PDF, and does not have anything to do with the original file.

Has anyone any idea, or can anyone test it on a different server (I have been using WINNT IIS server, and also a sambar server with failing in both cases)?
Any other suggestion/code to implemnet a download function for PDF files?

Thanks


if (-B $file) { #binary file...

$file =~ m|(.*/)(.+)\.([^\.]+)$|;
my $ext = $3; my $mimetype;

#most common mime types
if (uc($ext) eq &quot;PDF&quot;) { $mimetype=&quot;application/pdf&quot;;}
elsif (uc($ext) eq &quot;DOC&quot;) { $mimetype=&quot;application/msword&quot;;}
elsif (uc($ext) eq &quot;PPT&quot;) { $mimetype=&quot;application/powerpoint&quot;;}
elsif (uc($ext) eq &quot;ZIP&quot;) { $mimetype=&quot;application/x-zip-compressed&quot;;}
elsif (uc($ext) eq &quot;EXE&quot;) { $mimetype=&quot;application/octet-stream&quot;;}
elsif (uc($ext) eq &quot;EPS&quot;) { $mimetype=&quot;application/postscript&quot;;}
elsif (uc($ext) eq &quot;PS&quot;) { $mimetype=&quot;application/postscript&quot;;}
elsif (uc($ext) eq &quot;GZ&quot;) { $mimetype=&quot;application/x-gzip&quot;;}
elsif (uc($ext) eq &quot;LATEX&quot;) { $mimetype=&quot;application/x-latex&quot;;}
elsif (uc($ext) eq &quot;TAR&quot;) { $mimetype=&quot;application/x-tar&quot;;}
elsif (uc($ext) eq &quot;EXE&quot;) { $mimetype=&quot;application/octet-stream&quot;;}

my $size = -s &quot;$file&quot;;


open(INFILE, &quot;$file&quot;) || die &img_not_found;
binmode(INFILE);
binmode(STDOUT);
print &quot;Content-type: $mimetype\n\n&quot;;
while (<INFILE>) {
print $_;
}
close(INFILE);


} #binary mode file
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top