blib/lib/MooseX/PDF.pm

filter: Statementstmt 96.0% 24 / 25 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 91.6% 33 / 36 CC 3 SCAR 5.8
CC
3
Cov
90%
create_pdf
3.1
BEGIN
1.0
BEGIN
1.0
CRAP 1.8
↑ Summary
1 package MooseX::PDF;
2  
3 6 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use IO::CaptureOutput qw(capture qxx qxy);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 our $VERSION = '0.01';
10  
11 =head1 NAME
12  
13 MooseX::PDF - Create PDF files with Moose using Template Toolkit templates
14  
15 =head1 DESCRIPTION
16  
17 MooseX::PDF provides functionality to create PDF files using Template Toolkit
18 templates. Given a template with PDF::Reuse directives, this module will
19 process the template and return the raw PDF contents.
20  
21 This scalar can then be written to file, or output via a streamed process,
22 such as a web server.
23  
24 =head1 SYNOPSIS
25  
26 In your moose file do something like:
27  
28 with 'MooseX::PDF';
29  
30 $self->inc_path('/path/to/my/templates/folder/');
31 my $vars = {
32 scalar => $test,
33 array => \@array,
34 hash => \%hash
35 };
36  
37 my $raw_pdf = $self->create_pdf('template_file',$vars);
38  
39 =head1 ATTRIBUTES
40  
41 =over 4
42  
43 =item $self->inc_path()
44  
45 Used to set the INCLUDE_PATH for TT
46  
47 =back
48  
49 =head1 SUBROUTINES/METHODS
50  
51 =head2 create_pdf
52  
53 Used to generate the raw pdf
54  
55 =cut
56  
57 6 has 'inc_path' => (
58 is => 'rw',
59 isa => 'Str',
60 required => 1,
61 predicate => 'has_inc_path'
62 );
63  
64 1 ▶sub create_pdf {
Subroutine create_pdf: called CC=3 SCAR=11.3
Pod documented
65 1 my ($self, $pdf_template, $vars) = @_;
66  
67 1 ▶ die 'Error: TT INCLUDE_PATH has not been specified!'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_inc_path 0 1 1
68 unless( $self->has_inc_path );
69  
70 1 my $tt = Template->new({
71 INCLUDE_PATH => $self->inc_path,
72 ENCODING => 'utf8',
73 PLUGIN_BASE => 'Template::Plugin',
74 LOAD_PERL => 1,
75 });
76  
77 1 my $template = <<"EOT";
78 [% USE pdf = PDF::Reuse %]
79 [% PROCESS $pdf_template %]
80 EOT
81  
82 1 my ($output, $stdout, $stderr);
83  
84 1 local $CWD = $self->inc_path;
85  
86 #capture the output of TT and stick it into $stdout
87 1 ▶ capture { $tt->process(\$template,$vars,\$output) } \$stdout;
Subroutine __ANON__: called CC=1 SCAR=0.0
88  
89 1 ▶ die 'There is an error with the template: ' . $tt->error
Branch: 1 branch
BranchTrueFalseTotal
unless $stdout 1 0 1
90 if !$stdout;
91  
92 0 return $stdout;
93 }
94  
95 6 1;
96  
97 __END__
↑ Summary