Catalyst::View::PDF::API2 - Create PDF files from Catalyst using Template Toolkit templates
Version 0.03
Create a PDF::API2 view:
script/myapp_create.pl view PDF PDF::API2
In MyApp.pm, add a configuration item for the template include path:
__PACKAGE__->config('View::PDF' => { INCLUDE_PATH => __PACKAGE__->path_to('root','pdf_templates') });
In your controller:
$c->stash->{pdf_template} = 'hello_pdf.tt'; $c->forward('View::PDF');
In root/templates/hello_pdf.tt:
[% pdf.corefont('Helvetica-Bold') %]
Catalyst::View::PDF::API2 provides the facility to generate PDF files from a Catalyst application by embedding PDF::API2 commands within a Template Toolkit template.
Within your template you will have access to a pdf object which has methods corresponding to all of PDF::API2's functions.
pdf
For example, to print the text Hello, World at PDF coordinates 100,100, use the following directive in your template:
[% f1 = pdf->corefont('Helvetica') %] [% page = pdf.page %] [% page.mediabox(595,842) %] [% text = page.text %] [% text.textlabel(50,800,$f1,20,'Hello, World',-hspace=>75) %]
Data held in the stash can be printed as follows:
$c->stash->{list} = ['one', 'two', 'three', 'four']; [% y = 500 %] [% FOREACH item IN list %] [% page.textlabel(100,y,$f1,20,item) %] ###### to fix [% y = y - 13 %] [% END %] dav Formatting can be defined using the Template Toolkit format plugin: [% USE format %] [% currency = format('£%.2f') %] [% page.textlabel(100,100,$f1,20,currency(10)) %]
The key benefit of PDF::API2 is the ability to load an existing PDF 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 PDF, then use PDF::API2 to add in details such as the order number and customer address.
[% page.textlabel(123,643,$f1,12,order.number) %] [% page.textlabel(299,643,$f1,12,order.date) %]
Note that the PDF document loaded by pdf.prForm must be in the same directory as your Template Toolkit template.
pdf.prForm
Render the PDF file, places the contents in $c->response->body and sets appropriate HTTP headers.
$c->response->body
You should normally not need to use this method directly, instead forward to the PDF::API2 view from your controller:
$c->forward('View::PDF::API2');
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 PDF filename is not specified, it will be set to the last component of the URL path, appended with '.pdf'. For example, with a URL of http://localhost/view/order/pdf/1234 the PDF filename would be set to 1234.pdf.
Renders the PDF file and returns the raw PDF data.
If you need to capture the PDF data, e.g. to store in a database or for emailing, call render_pdf as follows:
render_pdf
my $pdf = $c->view("PDF::API2")->render_pdf($c);
Ferruccio Zamuner, nonsolosoft@diff.org
Please report any bugs or feature requests to bug-catalyst-view-pdf-api2 at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-View-PDF-API2. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-catalyst-view-pdf-api2 at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Catalyst::View::PDF::API2
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-View-PDF-API2
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Catalyst-View-PDF-API2
CPAN Ratings
http://cpanratings.perl.org/d/Catalyst-View-PDF-API2
Search CPAN
http://search.cpan.org/dist/Catalyst-View-PDF-API2
PDF::API
NonSoLoSoft - http://www.nonsolosoft.com/
Catalyst::View::PDF::Reuse
To every Catalyst developer that has written this framework or its manuals and to any CPAN contributors.
Copyright (C) 2009 NonSoLoSoft
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2 POD Errors
The following errors were encountered while parsing the POD:
Non-ASCII character seen before =encoding in 'format('£%.2f')'. Assuming UTF-8
Unknown directive: =ACKNOWLEDGEMENTS
To install Catalyst::View::PDF::API2, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Catalyst::View::PDF::API2
CPAN shell
perl -MCPAN -e shell install Catalyst::View::PDF::API2
For more information on module installation, please visit the detailed CPAN module installation guide.