Code check

K

Kati

I'm trying to get a PDF to download from a web location, instead of making
browsers display it with whatever PDF reader is configured to their
browser. So I tested the following code, placing the PDF file and the below
script in the cgi-bin:

#!/usr/bin/perl
print "Content-Type: application/force-download\n";
print "Content-Disposition: attachment\; filename=myFile.pdf\n";
print "Content-Length: 340\n";
print "Content-Description: PDF document\n\n";
open (myFile, "myFile.pdf");
print <myFile>

The above works, as far as it prompts browsers to download the file, or
to launch in a manually specified application.
However, after it downloaded, the file is corrupted for some reason and
does not open.
Could anyone who knows kindly point out what I might be doing wrong?
 
A

A. Sinan Unur

I'm trying to get a PDF to download from a web location, instead of
making browsers display it with whatever PDF reader is configured to
their browser.

Do not repeatedly post the same message. This is not some chat room.
 
R

Randal L. Schwartz

Kati> I'm trying to get a PDF to download from a web location, instead
Kati> of making browsers display it with whatever PDF reader is
Kati> configured to their browser.

No. Please don't. Send the proper MIME type, and let *me* decide
what to do with it.

Force doesn't work on the web. It only breaks other legitmate things.
 
S

Sherm Pendley

#!/usr/bin/perl
print "Content-Type: application/force-download\n";
print "Content-Disposition: attachment\; filename=myFile.pdf\n";
print "Content-Length: 340\n";
print "Content-Description: PDF document\n\n";
open (myFile, "myFile.pdf");
print <myFile>

The above works, as far as it prompts browsers to download the file, or
to launch in a manually specified application.
However, after it downloaded, the file is corrupted for some reason and
does not open.

First let me say I agree with Randal - what you're doing might be
considered rude, depending on the circumstances. On the other hand, if
you're providing a source of downloadable documents on an internal
network, it might also be perfectly acceptable.

With that said...
Could anyone who knows kindly point out what I might be doing wrong?

Are the downloaded files being truncated? That is, are they smaller than
the source files on the server? If so, read on...

PDF's can contain newlines. The last line in the above code uses <myFile>
in a scalar context, meaning that you're only actually sending everything
up to and including the first newline in the source document.

The simplest way to get around that is to set the input record separator:

$/ = undef;

.... or ...

use English;
$INPUT_RECORD_SEPARATOR = undef;

If your server is Windows, you might also need to set the output file
handle to binary mode with binmode():

binmode STDOUT;

sherm--
 
T

Tassilo v. Parseval

Also sprach Scott Bryce:
I looked at the docs and I wasn't clear on this. I was about to make a
similar comment, but according to the docs:

Because print takes a LIST, anything in the LIST is evaluated in list
context.

So, would not print <myFile> (or print <MYFILE>, depending on whether
you are following conventions) print the entire file?

Yes, it would. The original statement that

print <HANDLE>;

would impose scalar context on the readline() is false.
So I would think that print <myFile> in the OPs script would print the
entire PDF file.

That's what it does.

Tassilo
 

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
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top