[Catalyst-dev] trouble with a pdf attachment

Jillian Rowe jir2004 at qatar-med.cornell.edu
Tue Nov 6 11:07:11 GMT 2012


Hello all,

I'm trying to send a pdf attachment to the browser.

I found an example online of a text/csv attachment.

sub this_works{
    my($self, $c) = @_; 

    my $csv = "1,5.99\n2,29.99\n3,3.99\n";
    $c->res->content_type('text/comma-separated-values');
    my $filename = 'Important Orders.csv';
    $c->res->header('Content-Disposition', "attachment; filename=$filename");
    $c->res->body($csv);
}

Which gives back a comma separated file as expected.

However, when I try to send back a pdf I get an empty file. 

sub attach_pdf{
  my($self, $c, $fileholder, $file, $name) = @_;

    open(DLFILE, "<$file") or die $c->log->debug("Couldn't open file! $!");
    my @fileholder = <DLFILE>;
    close (DLFILE) or die $c->log->debug("Couldn't close file! $!");

    $c->res->content_type('application/pdf');
    $c->res->header('Content-Disposition', "attachment; filename=$name");
    $c->res->body("@$fileholder");
}

If I try it without the content and the header I get back what the pdf file looks like in vim, so I know its reading the file in. Nearly this exact script works with a perl cgi script, but I haven't figured it out for catalyst yet. 

Any help would be very appreciated.

Best,
Jillian



More information about the Catalyst-dev mailing list