[Melbourne-pm] Delivering PDFs via CGI

Craig Sanders cas at taz.net.au
Mon Jun 19 02:44:29 PDT 2006


On Mon, Jun 19, 2006 at 04:49:33PM +1000, Leigh Sharpe wrote:
>  my $size = -s "$filename";
>  read PDFFILE,$data,$size || die"$!\n";
>  close PDFFILE;
>  print $data;

this doesnt answer your question (others have done that), but why slurp
the entire pdf file into memory before sending it? that could use up
enormous amounts of RAM, depending on the size of the file. instead, do
somehing like this:

replace the last 4 lines of your script above with the following:


$|=1; # unbuffer output - generally a good idea to do this in any CGI
      # to avoid buffering delays which can annoy users.
while <PDFFILE> {
	print
} ;
close(PDFFILE);


also, there's a difference between single-quote (') and double-quote
(") characters. use single-quotes for fixed strings with no variable
interpolation (and no escaped chars like \n) - e.g where you define my
$filename, that's just a constant and doesnt need var. interp.. use
double-quotes where you need them.

there's a performance improvement for using
single quotes when you dont need interpolation....minor, but it adds up
if you're doing it inside a loop. and it's a good habit to develop.


craig

-- 
craig sanders <cas at taz.net.au>           (part time cyborg)


More information about the Melbourne-pm mailing list