[Catalyst] generating and redirecting to pdfs

Jason Galea lists at eightdegrees.com.au
Tue Oct 20 23:42:18 GMT 2009


Hi Steve,

not sure if you can gleen anything from this but I recently set myself up to
produce PDFs from basic HTML produced by TT.
I created a view that processes a TT template then converts to PDF using
HTML::Doc and shoves it as a string straight to $c->response->body.

I'm pretty happy with the results and ease of use.. (automatically handles
multi page output amoung other things)

#=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
package Lecstor7::View::PDF;

use base qw/Catalyst::View/;

use HTML::HTMLDoc;

__PACKAGE__->config->{template_extension} =3D '.tt';
__PACKAGE__->config->{tmp_dir} =3D '/tmp';
__PACKAGE__->config->{page_size} =3D 'a4';
__PACKAGE__->config->{header} =3D ['t', '.', '/'];
__PACKAGE__->config->{footer} =3D ['t', '.', '/'];

sub process {
  my ( $self, $c ) =3D @_;

  my $template =3D $c->stash->{template}
    ||  $c->action . $self->config->{template_extension};

  unless (defined $template) {
    $c->log->debug('No template specified for rendering') if $c->debug;
    return 0;
  }

  my $output =3D $c->view('TT')->render($c, $template);

  if (UNIVERSAL::isa($output, 'Template::Exception')) {
    my $error =3D qq/Couldn't render template "$output"/;
    $c->log->error($error);
    $c->error($error);
    return 0;
  }

  my $doc =3D HTML::HTMLDoc->new( 'mode' =3D> 'file', 'tmpdir' =3D>
$self->config->{tmp_dir} );
  $doc->set_page_size($self->config->{page_size});
  $doc->set_header(@{$self->config->{header}});
  $doc->set_footer(@{$self->config->{footer}});
  $doc->set_html_content($output);

  my $pdf =3D $doc->generate_pdf();

  unless ( $c->response->content_type ) {
    $c->response->content_type('text/pdf; charset=3Dutf-8');
  }

  $c->res->header( 'Content-Disposition' =3D>
'attachment;filename=3D'.$c->stash->{pdf_filename} );

  $c->response->body($pdf->to_string());

  return 1;
}

1;
#=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

in my Controller I can then do.. (simplified version of my actual
controller)

sub packing_slip : Local{
  my ($self, $c) =3D @_;
  $c->stash(
    pdf_filename =3D> 'packingslip.pdf'
  );
  $c->forward( $c->view('PDF') );
}

cheers,

J

On Wed, Oct 21, 2009 at 8:25 AM, Steve Rippl <rippls at woodlandschools.org>wr=
ote:

> Hi,
>
> I'm using TT for my View templates, and I'm experimenting with
> Template::Plugin::Latex for generating pdf reports.  Now I'm generating
> pdfs (which is nice!) but not redirecting to them at the end, so I
> suspect I'm using the wrong approach.
>
> Right now I've added OUTPUT_PATH =3D> 'root/tmp' to lib/WsdSis/View/TT.pm
> and when I hit the /report/test it using the following template (without
> a wrapper)
>
> [%
>  META no_wrapper =3D 1;
>  META title =3D 'Latex';
> -%]
> [% USE Latex(output=3D'example.pdf') -%]
> [% FILTER latex %]
> \documentclass[letterpaper]{article}
> \begin{document}
> ...LaTeX document...
> \end{document}
> [% END -%]
>
> which produces root/tmp/example.pdf but leaves me looking at a blank
> page on the screen.  I realize my view is supposed to provide the final
> view and I've redirected that way to the pdf file, is there a quick way
> to redirect to that or do I need a different approach (similar to the
> Catalyst::View::Email::Template example in the book?).
>
> Any pointers would be much appreciated...
>
> Thanks,
> Steve
>
>
> --
> Steve Rippl
> Technology Director
> Woodland Public Schools
> 360 225 9451 x326
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>



-- =

Jason Galea
Web Developer

Ph 07 40556926
Mob 04 12345 534
www.eightdegrees.com.au
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20091021/4f43e=
914/attachment.htm


More information about the Catalyst mailing list