Create pdf files from catalyst using template toolkit templates
Create a PDF::Reuse view:
script/myapp_create.pl view PDF::Reuse PDF::Reuse
In MyApp.pm, add a configuration item for the template include path:
_\|_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');
In root/templates/hello_pdf.tt:
[% pdf.prFont('Helvetica-Bold') %] [% pdf.prFontSize(20) %] [% pdf.prText(100,100,'Hello, World!') %]
Catalyst::View::PDF::Reuse provides the facility to generate \s-1PDF\s0 files from a Catalyst application by embedding PDF::Reuse commands within a Template Toolkit template.
Within your template you will have access to a \*(C`pdf\*(C' object which has methods corresponding to all of PDF::Reuse's functions.
For example, to print the text Hello, World at \s-1PDF\s0 coordinates 100,100, use the following directive in your template:
[% pdf.prText(100,100,'Hello, World') %]
Data held in the stash can be printed as follows:
$c->stash->{list} = ['one', 'two', 'three', 'four'];
[% y = 500 %] [% FOREACH item IN list %] [% pdf.prText(100,y,item) %] [% y = y - 13 %] [% END %]
Formatting can be defined using the Template Toolkit format plugin:
[% USE format %] [% currency = format('A\*^X%.2f') %] [% pdf.prText(100,100,currency(10)) %]
The key benefit of PDF::Reuse is the ability to load an existing \s-1PDF\s0 file and use this as the basis for a new document.
For example, to produce receipts or shipping labels you could create a blank receipt in Microsoft Word, convert this to \s-1PDF\s0, then use PDF::Reuse to add in details such as the order number and customer address.
[% pdf.prForm('customer_receipt.pdf') %] [% pdf.prText(123,643,order.number) %] [% pdf.prText(299,643,order.date) %]
Note that the \s-1PDF\s0 document loaded by \*(C`pdf.prForm\*(C' must be in the same directory as your Template Toolkit template.
Render the \s-1PDF\s0 file, places the contents in \*(C`$c->response->body\*(C' and sets appropriate \s-1HTTP\s0 headers.
You should normally not need to use this method directly, instead forward to the PDF::Reuse view from your controller:
$c->forward('View::PDF::Reuse');
The filename and content disposition (inline or attachment) can be controlled by putting the following values in the stash:
$c->stash->{pdf_disposition} = 'attachment'; # Default is 'inline' $c->stash->{pdf_filename} = 'myfile.pdf';
If the \s-1PDF\s0 filename is not specified, it will be set to the last component of the \s-1URL\s0 path, appended with '.pdf'. For example, with a \s-1URL\s0 of <http://localhost/view/order/pdf/1234> the \s-1PDF\s0 filename would be set to 1234.pdf.
Renders the \s-1PDF\s0 file and returns the raw \s-1PDF\s0 data.
If you need to capture the \s-1PDF\s0 data, e.g. to store in a database or for emailing, call \*(C`render_pdf\*(C' as follows:
my $pdf = $c->view("PDF::Reuse")->render_pdf($c);
Jon Allen, [email protected]
Please report any bugs or feature requests to \*(C`bug-catalyst-view-pdf-reuse at rt.cpan.org\*(C', or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-View-PDF-Reuse <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-View-PDF-Reuse>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc Catalyst::View::PDF::Reuse
You can also look for information at:
\s-1RT:\s0 \s-1CPAN\s0's request tracker http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-View-PDF-Reuse <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-View-PDF-Reuse>
AnnoCPAN: Annotated \s-1CPAN\s0 documentation http://annocpan.org/dist/Catalyst-View-PDF-Reuse <http://annocpan.org/dist/Catalyst-View-PDF-Reuse>
\s-1CPAN\s0 Ratings http://cpanratings.perl.org/d/Catalyst-View-PDF-Reuse <http://cpanratings.perl.org/d/Catalyst-View-PDF-Reuse>
Search \s-1CPAN\s0 http://search.cpan.org/dist/Catalyst-View-PDF-Reuse <http://search.cpan.org/dist/Catalyst-View-PDF-Reuse>
PDF::Reuse
Penny's Arcade Open Source - <http://www.pennysarcade.co.uk/opensource>
Copyright (C) 2009 Penny's Arcade Limited
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.