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)

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

use base qw/Catalyst::View/;

use HTML::HTMLDoc;

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

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

  my $template = $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 = $c->view('TT')->render($c, $template);

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

  my $doc = HTML::HTMLDoc->new( 'mode' => 'file', 'tmpdir' =>
$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 = $doc->generate_pdf();

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

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

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

  return 1;
}

1;
#============================================

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

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

cheers,

J

On Wed, Oct 21, 2009 at 8:25 AM, Steve Rippl <rip...@woodlandschools.org>wrote:

> 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 => '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 = 1;
>  META title = 'Latex';
> -%]
> [% USE Latex(output='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@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
_______________________________________________
List: Catalyst@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/

Reply via email to