[Melbourne-pm] Delivering PDFs via CGI

Leigh Sharpe lsharpe at pacificwireless.com.au
Mon Jun 19 04:22:38 PDT 2006


This was only a test case, the PDF concerned was all of about 50KB, so I knew I wasn't about to kill the server. Ultimately this will be generating PDF's on the fly, so I really just needed a valid PDF so start with.


Nice to see some activity around here again......

-----Original Message-----
From: Craig Sanders [mailto:cas at taz.net.au]
Sent: Monday, 19 June 2006 7:44 PM
To: Leigh Sharpe
Cc: melbourne-pm
Subject: Re: [Melbourne-pm] Delivering PDFs via CGI


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