Mailing List Archive

trouble with a pdf attachment
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

_______________________________________________
Catalyst-dev mailing list
Catalyst-dev@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst-dev
Re: trouble with a pdf attachment [ In reply to ]
Hi,

> 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! $!");


It should better be:

open( my $dlfile, '<', $file ) or die $c->log->debug( "Couldn't open file! $!" );
binmode $dlfile;


> my @fileholder = <DLFILE>;

It should better be:

my $fileholder;
{ local $/;
$fileholder = <$dlfile>;
}

The code above could be written to look nicer, but it could have been harder to understand.

The line "local $/;" sets the var $/ to undef in the local scope defined between { and } so the next line slurps the entire file content in the scalar variable $fileholder.

You could have also use instead:

use File::Slurp;
my $fileholder = read_file( $file );


> close (DLFILE) or die $c->log->debug("Couldn't close file! $!");


It should better be:

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");

The last line should be:

$c->res->body( $fileholder );
Octavian


_______________________________________________
Catalyst-dev mailing list
Catalyst-dev@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst-dev
RE: trouble with a pdf attachment [ In reply to ]
Thanks! I tried out the File::Slurp just now, and it works perfectly!

Altogether its:

use File::Slurp;
my $fileholder = read_file( $file );
$c->res->content_type('application/pdf');
$c->res->header('Content-Disposition', "attachment; filename=$name");
$c->res->body( $fileholder );

Best,
Jillian
_______________________________________________
Catalyst-dev mailing list
Catalyst-dev@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst-dev
Re: trouble with a pdf attachment [ In reply to ]
On Nov 7, 2012, at 12:20 PM, Jillian Rowe <jir2004@qatar-med.cornell.edu> wrote:

> Thanks! I tried out the File::Slurp just now, and it works perfectly!
>
> Altogether its:
>
> use File::Slurp;
> my $fileholder = read_file( $file );
> $c->res->content_type('application/pdf');
> $c->res->header('Content-Disposition', "attachment; filename=$name");
> $c->res->body( $fileholder );

https://metacpan.org/module/Catalyst::Response#res-body-text-fh-iohandle_object-

You could also pass in a filehandle, or a IO::Handle, and have Catalyst respond with pieces. That way you don't have to read the whole PDF into memory at once.

-a
_______________________________________________
Catalyst-dev mailing list
Catalyst-dev@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst-dev