ClearPress Framework

Exporting PDFs

Exporting PDF pages is pretty easy. They can be templated in much the same way as HTML. There are several ways to do it but the one I use involves using TeX as an intermediate format.

Firstly install an appropriate TeX package for your platform. I usually use tetex. Then install the Template::Plugin::Latex plugin which serves as a wrapper around your new tetex installation. Now you're ready to go.

By default the ClearPress view superclass doesn't directly support PDF (although this may change in future) so you need to tell your view superclass a little bit more about how to serve the pages.

sub decor { my $self = shift; my $aspect = $self->aspect; if($aspect eq 'read_pdf') { return 0; } return $self->SUPER::decor(); }

The content-type with which to serve the page needs to be configured in init(). I like to add a useful filter for TeX entity escaping, so this is also set up here:

sub init { my $self = shift; my $aspect = $self->aspect || q[]; $self->add_tt_filter('tex_entity', \&_tex_entity); $self->{content_type} ||= ($aspect =~ /_pdf$/smx) ? 'application/pdf' : q[]; return 1; } sub _tex_entity { my $string = shift; $string =~ s{([\\&_%])}{\\$1}smxg; return $string; }

Next we need to tell our view that it's ok to accept 'read_pdf' requests:

sub read_pdf { return 1; }

but note there's no implementation here. All the real TeX/PDF work is done in the templating. So entity_read_pdf.tt2 looks something like this:

[% USE Latex; FILTER latex("pdf") -%] \documentclass{report} \begin{document} \title{Report for [% model.table | tex_entity %] [% %]} \author{[% requestor.username | tex_entity %]} \maketitle \begin{table} \begin{tabular}{|r|c|} \hline [% FOREACH f IN model.fields -%] [% f | tex_entity %] & [% model.$f | tex_entity %] \\ [% END -%] \hline \end{tabular} \caption{my table which is a very good table} \end{table} \end{document} [% END -%]

And that should be very nearly all there is to it. There is one downside though - you need to learn some TeX. Sorry - can't really help much there. Maybe there's a Template::Plugin::PostScript somewhere... :)