PDA

View Full Version : Send Server Side PDF to Desktop


tom123
08-13-2007, 03:05 PM
Hi All

I'm trying to send an pdf file to a users computer i.e They click on a link and a "Save As" box allows them save the file to their computer.

I currently have the following code:


my $pdf_file_name = "/tmp/order1.pdf";

# Open file and put contents in temporary filehandle
open(READ_FILE,"<$pdf_file_name") || Error('open'.'file'); #or die "Could not open $pdf_file_name for reading: $!\n";
my @file_holder = <READ_FILE>;
close(READ_FILE) || Error('close','file'); #or die "Could not close $pdf_file_name: $!\n";

# Send file to screen for user to save to their desktop
print "Content-Type:application/x-download\n";
print "Content-Disposition:attachment;filename=$pdf_file_name\n\n";
print @file_holder;

Problem is the save as option dosent apperar.
Insted a load of garbage is written to the Response header (In firebug)

Any help greatly appreciated.

KevinADC
08-13-2007, 07:01 PM
have you tried it with any other browser?

tom123
08-14-2007, 11:42 AM
Yes. Netscape 9, Same result.

tom123
08-14-2007, 11:50 AM
Also, I should have stated that the PDF is crteated dynamically before being send to desktop.

# First create a HTML output
my $template = Template->new();
my $template_file = 'templates/admin_entries_pdf.tt';
my $vars = { 'company' => &getEntryDetails( $db, $report_details{'category'}, $entry ),
'category' => $category_info[1],
'entry' => $entry,
'question' => @res
};

my $message;
$template->process($template_file, $vars, \$message) || die $template->error();

# Generate the PDF file from the HTML output
my $htmldoc = new HTML::HTMLDoc();
$htmldoc->set_html_content($message);
$htmldoc->set_logoimage('../images/award2007.gif');
$htmldoc->set_header('t', '.', 'l');
$htmldoc->set_permissions('all');
my $pdf = $htmldoc->generate_pdf();

$pdf->to_file($pdf_file_name);

# Change permissions
chmod 0777, $pdf_file_name;

KevinADC
08-14-2007, 07:01 PM
there must also be more code before that as there are variables used in the code that are not defined. I'm guessing your script is dieing here:

die $template->error();

and trying to print anerror message.

This looks wrong (but maybe not):

my $message;
$template->process($template_file, $vars, \$message) || die $template->error();

$message is declared but undefined and then used as a reference in the process() method. Is that what you really want to do?

tom123
08-14-2007, 07:06 PM
I checked the server and downloaded the PDF throught FTP so the file is been created fine :-(

KevinADC
08-14-2007, 08:55 PM
last ditch effort, try using binmode:

open(READ_FILE,"<$pdf_file_name") || Error('open'.'file'); #or die "Could not open $pdf_file_name for reading: $!\n";
binmode READ_FILE;
my @file_holder = <READ_FILE>;
close(READ_FILE) || Error('close','file'); #or die "Could not close $pdf_file_name: $!\n";

tom123
08-15-2007, 12:48 PM
Figured it out after many head banging hours.
I was missing "enctype="multipart/form-data" in the html form declaration.

Additionally the following lines were messing it up too:
print "Content-type: text/html\n\n";
print Data::Dumper->Dump([\%report_details],['*ARGV']);

Thanks for you help Kevin.