You are on page 1of 9

Creating PDF documents with

Catalyst
Jon Allen

www.pennysarcade.co.uk/opensource
Creating PDF files with Perl
•  Lots of good modules on CPAN
•  PDF::API2
–  Very powerful
–  Lots of options
–  But…
–  Low-level interface
•  Difficult to create complex documents
–  Documentation could be better

Creating PDF documents with Catalyst


www.pennysarcade.co.uk/opensource
PDF::Reuse
•  Very simple interface
•  Can add text and graphics to an existing
PDF file
use PDF::Reuse;

prFile(’output.pdf’);
prForm(’template.pdf’);
prText(100,500,’Hello, World!’);
prEnd;

Creating PDF documents with Catalyst


www.pennysarcade.co.uk/opensource
Problems with PDF::Reuse
•  Only outputs to file or STDOUT
–  No function to return the output as a scalar
•  Global procedural interface – not OO – hard
to work on multiple files
•  Document layout still needs to be hard-
coded
–  i.e. a separate Catalyst view is required for each
PDF file

Creating PDF documents with Catalyst


www.pennysarcade.co.uk/opensource
Catalyst::View::PDF::Reuse
•  Generic view for PDF::Reuse documents
•  Addresses limitations of PDF::Reuse
–  Handles temporary file creation and output
–  Sets content-type header
–  Sets sensible filename (based on last URL
component)
–  Document layout commands embedded into
Template Toolkit template

Creating PDF documents with Catalyst


www.pennysarcade.co.uk/opensource
Using C:V:P:R
•  Create a view:
script/myapp_create.pl view PDF::Reuse PDF::Reuse

•  Config:
__PACKAGE__−>config('View::PDF::Reuse' => {
INCLUDE_PATH =>
__PACKAGE__−>path_to('root','templates’)
});

•  In your controller:
$c−>stash−>{pdf_template} = 'hello_pdf.tt';
$c−>forward('View::PDF::Reuse');

Creating PDF documents with Catalyst


www.pennysarcade.co.uk/opensource
Template structure
•  C:V:P:R adds a ‘pdf’ object to TT with all
PDF::Reuse functions available as methods
hello_pdf.tt
[% pdf.prForm(’template.pdf’) %]
[% pdf.prText(100,500,’Hello, World!’) %]

•  All standard Template Toolkit functions can


be used

Creating PDF documents with Catalyst


www.pennysarcade.co.uk/opensource
Advanced templates
[% USE format %]
[% currency = format('£%.2f') %]

[% pdf.prForm('customer-receipt.pdf') %]
[% pdf.prText(100,500,order.number) %]

[% y = 450 %]
[% FOREACH item IN list %]
[% pdf.prText(100,y,item.name %]
[% pdf.prText(400,y,currency(item.price)) %]
[% y = y – 12 %]
[% END %]

Creating PDF documents with Catalyst


www.pennysarcade.co.uk/opensource
Try it out!
•  Catalyst::View::PDF::Reuse can be
downloaded from
http://www.pennysarcade.co.uk/opensource

•  Thank you for listening!


–  Any questions?

Creating PDF documents with Catalyst


www.pennysarcade.co.uk/opensource

You might also like