Catalyst/View/PDF/Reuse.pm

filter: Statementstmt 38.9% 23 / 59 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 77.7% 7 / 9 Podpod 100% 2 / 2 totaltotal 36.3% 32 / 88 CC 10 SCAR 31.2
CC
10
Cov
31%
process
42.0
render_pdf
30.0
BEGIN
1.0
CRAP 22.7
↑ Summary Template/Plugin/Catalyst/View/PDF/Reuse/Barcode.pm »
1 package Catalyst::View::PDF::Reuse;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use parent 'Catalyst::View::TT';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Spec::Functions qw/catfile rel2abs/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Temp qw/tmpnam/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use PDF::Reuse;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.05';
12  
13 =encoding utf8
14  
15 =head1 NAME
16  
17 Catalyst::View::PDF::Reuse - Create PDF files from Catalyst using Template Toolkit templates
18  
19 =cut
20  
21 =head1 SYNOPSIS
22  
23 Create a PDF::Reuse view:
24  
25 script/myapp_create.pl view PDF::Reuse PDF::Reuse
26  
27 In MyApp.pm, add a configuration item for the template include path:
28  
29 __PACKAGE__->config('View::PDF::Reuse' => {
30 INCLUDE_PATH => __PACKAGE__->path_to('root','templates')
31 });
32  
33 In your controller:
34  
35 $c->stash->{pdf_template} = 'hello_pdf.tt';
36 $c->forward('View::PDF::Reuse');
37  
38 In F<root/templates/hello_pdf.tt>:
39  
40 [% pdf.prFont('Helvetica-Bold') %]
41 [% pdf.prFontSize(20) %]
42 [% pdf.prText(100,100,'Hello, World!') %]
43  
44  
45 =head1 DESCRIPTION
46  
47 Catalyst::View::PDF::Reuse provides the facility to generate PDF files from
48 a Catalyst application by embedding L<PDF::Reuse> commands within a Template
49 Toolkit template.
50  
51 =head2 Template Syntax
52  
53 Within your template you will have access to a C<pdf> object which has
54 methods corresponding to all of L<PDF::Reuse>'s functions.
55  
56 For example, to print the text I<Hello, World> at PDF coordinates 100,100,
57 use the following directive in your template:
58  
59 [% pdf.prText(100,100,'Hello, World') %]
60  
61 Data held in the stash can be printed as follows:
62  
63 $c->stash->{list} = ['one', 'two', 'three', 'four'];
64  
65 [% y = 500 %]
66 [% FOREACH item IN list %]
67 [% pdf.prText(100,y,item) %]
68 [% y = y - 13 %]
69 [% END %]
70  
71 Formatting can be defined using the Template Toolkit format plugin:
72  
73 [% USE format %]
74 [% currency = format('£%.2f') %]
75 [% pdf.prText(100,100,currency(10)) %]
76  
77 =head2 Using existing PDF documents
78  
79 The key benefit of L<PDF::Reuse> is the ability to load an existing PDF
80 file and use this as the basis for a new document.
81  
82 For example, to produce receipts or shipping labels you could create a
83 blank receipt in Microsoft Word, convert this to PDF, then use PDF::Reuse
84 to add in details such as the order number and customer address.
85  
86 [% pdf.prForm('customer_receipt.pdf') %]
87 [% pdf.prText(123,643,order.number) %]
88 [% pdf.prText(299,643,order.date) %]
89  
90 Note that the PDF document loaded by C<pdf.prForm> must be in the same
91 directory as your Template Toolkit template.
92  
93 =head1 FUNCTIONS
94  
95 =head2 process
96  
97 Render the PDF file, places the contents in C<< $c->response->body >> and
98 sets appropriate HTTP headers.
99  
100 You should normally not need to use this method directly, instead forward
101 to the PDF::Reuse view from your controller:
102  
103 $c->forward('View::PDF::Reuse');
104  
105 The filename and content disposition (inline or attachment) can be controlled
106 by putting the following values in the stash:
107  
108 $c->stash->{pdf_disposition} = 'attachment'; # Default is 'inline'
109 $c->stash->{pdf_filename} = 'myfile.pdf';
110  
111 If the PDF filename is not specified, it will be set to the last component
112 of the URL path, appended with '.pdf'. For example, with a URL of
113 L<http://localhost/view/order/pdf/1234> the PDF filename would be set to
114 F<1234.pdf>.
115  
116 =cut
117  
118 0 ▶sub process {
Subroutine process: not called CC=6 SCAR=37.4
Pod documented
119 0 my ($self, $c) = @_;
120  
121 0 my $output = $self->render_pdf($c);
122 0 ▶ if (UNIVERSAL::isa($output, 'Template::Exception')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($output, "Template::Exception")) 0 0 0
123 0 my $error = qq/Couldn't render template "$output"/;
124 0 $c->log->error($error);
125 0 $c->error($error);
126 0 return 0;
127 }
128  
129 0 ▶ my $pdf_filename = $c->stash->{pdf_filename} || (split '/',$c->req->path)[-1] || 'index.pdf';
Condition0%0/5
$c->stash->{'pdf_filename'} || (split(m[/], $c->req->path, 0))[-1] || 'index.pdf'
l!l
00
$c->stash->{"pdf_filename"} or (split(m[/], $c->req->path, 0))[-1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->stash->{'pdf_filename'} || (split(m[/], $c->req->path, 0))[-1] || 'index.pdf'
$c->stash->{"pdf_filename"} (split(m[/], $c->req->path, 0))[-1]
Truth table0%0/3
A or B
A= $c->stash->{"pdf_filename"}B= (split(m[/], $c->req->path, 0))[-1]
ABresult
1X1
011
000
130 0 ▶ $pdf_filename .= '.pdf' unless ($pdf_filename =~ /\.pdf$/i);
Branch: 1 branch
BranchTrueFalseTotal
unless $pdf_filename =~ /\.pdf$/i 0 0 0
131
132 0 ▶ my $pdf_disposition = $c->stash->{pdf_disposition} || 'inline';
Condition0%0/2
$c->stash->{'pdf_disposition'} || 'inline'
l!l
00
MC/DC0%0/1
$c->stash->{'pdf_disposition'} || 'inline'
$c->stash->{'pdf_disposition'}
Truth table0%0/2
A
A= $c->stash->{'pdf_disposition'}
Aresult
11
00
133
134 0 $c->response->content_type('application/pdf');
135 0 $c->response->headers->header("Content-Disposition" => qq{$pdf_disposition; filename="$pdf_filename"});
136
137 0 $c->response->body($output);
138 }
139  
140  
141 =head2 render_pdf
142  
143 Renders the PDF file and returns the raw PDF data.
144  
145 If you need to capture the PDF data, e.g. to store in a database or for
146 emailing, call C<render_pdf> as follows:
147  
148 my $pdf = $c->view("PDF::Reuse")->render_pdf($c);
149  
150 =cut
151  
152 0 ▶sub render_pdf {
Subroutine render_pdf: not called CC=5 SCAR=34.0
Pod documented
153 0 my ($self, $c) = @_;
154
155 0 my $template = <<'EOT';
156 [% USE pdf = Catalyst::View::PDF::Reuse %]
157 [% USE barcode = Catalyst::View::PDF::Reuse::Barcode %]
158 [% PROCESS $pdf_template %]
159 EOT
160  
161 0 my $tempfile = tmpnam();
162 0 prInitVars();
163 0 prFile($tempfile);
164
165 # Convert any relative include paths to absolute paths
166 0 foreach my $path (@{$self->config->{INCLUDE_PATH}}) {
167 0 $path = rel2abs($path);
168 }
169  
170 0 my $output;
171 0 SEARCH: foreach my $path (@{$self->config->{INCLUDE_PATH}}) {
172 0 ▶ if (-e catfile($path,$c->stash->{pdf_template})) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e catfile($path, $c->stash->{"pdf_template"})) 0 0 0
173 0 local $CWD = $path;
174 0 $output = $self->render($c,\$template);
175 0 last SEARCH;
176 }
177 }
178
179 0 prEnd();
180  
181 0 my $pdf;
182 0 local $/ = undef;
183 0 open PDF,'<',$tempfile;
184 0 $pdf = (<PDF>);
185 0 close PDF;
186 0 unlink $tempfile;
187  
188 0 ▶ return (UNIVERSAL::isa($output, 'Template::Exception')) ? $output : $pdf;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($output, 'Template::Exception') ? : 0 0 0
189 }
190  
191 =head1 AUTHOR
192  
193 Jon Allen, L<jj@jonallen.info>
194  
195  
196 =head1 BUGS
197  
198 Please report any bugs or feature requests to C<bug-catalyst-view-pdf-reuse at rt.cpan.org>, or through
199 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-View-PDF-Reuse>. I will be notified, and then you'll
200 automatically be notified of progress on your bug as I make changes.
201  
202  
203 =head1 SUPPORT
204  
205 You can find documentation for this module with the perldoc command.
206  
207 perldoc Catalyst::View::PDF::Reuse
208  
209 You can also look for information at:
210  
211 =over 4
212  
213 =item * RT: CPAN's request tracker
214  
215 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-View-PDF-Reuse>
216  
217 =item * AnnoCPAN: Annotated CPAN documentation
218  
219 L<http://annocpan.org/dist/Catalyst-View-PDF-Reuse>
220  
221 =item * CPAN Ratings
222  
223 L<http://cpanratings.perl.org/d/Catalyst-View-PDF-Reuse>
224  
225 =item * Search CPAN
226  
227 L<http://search.cpan.org/dist/Catalyst-View-PDF-Reuse>
228  
229 =back
230  
231  
232 =head1 SEE ALSO
233  
234 L<PDF::Reuse>
235  
236 Penny's Arcade Open Source - L<http://www.pennysarcade.co.uk/opensource>
237  
238  
239 =head1 COPYRIGHT & LICENSE
240  
241 Copyright (C) 2009 Penny's Arcade Limited
242  
243 This program is free software; you can redistribute it and/or modify it
244 under the same terms as Perl itself.
245  
246 =cut
247  
248 1 1; # End of Catalyst::View::PDF::Reuse
↑ Summary Template/Plugin/Catalyst/View/PDF/Reuse/Barcode.pm »