View/Wkhtmltopdf.pm

filter: Statementstmt 41.8% 46 / 110 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 50.0% 11 / 22 Podpod 100% 2 / 2 totaltotal 32.9% 59 / 179 CC 19 SCAR 47.7
CC
19
Cov
32%
render
274.7
process
12.0
__ANON__
2.0
CRAP 118.4
↑ Summary Helper/View/Wkhtmltopdf.pm »
1 package Catalyst::View::Wkhtmltopdf;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # ABSTRACT: Catalyst view to convert HTML (or TT) content to PDF using wkhtmltopdf
5  
6 1 extends 'Catalyst::View';
7  
8 1 ▶use version 0.77;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 our $VERSION = 'v0.6.4';
10  
11 1 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Which qw( which );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use IO::File::WithPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use IPC::Run3 qw( run3 );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 has 'stash_key' => (
22 is => 'rw',
23 isa => 'Str',
24 lazy => 1,
25 0 ▶ default => sub { 'wk' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 1 );
27 has 'tmpdir' => (
28 is => 'rw',
29 isa => 'Str',
30 lazy => 1,
31 0 ▶ default => sub { File::Spec->tmpdir() }
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 1 );
33 has 'command' => (
34 is => 'rw',
35 isa => 'Str',
36 lazy => 1,
37 0 ▶ default => sub { which('wkhtmltopdf') }
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 1 );
39 has 'tt_view' => (
40 is => 'rw',
41 isa => 'Str',
42 lazy => 1,
43 0 ▶ default => sub { 'TT' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 1 );
45 has 'page_size' => (
46 is => 'rw',
47 isa => 'Str',
48 lazy => 1,
49 0 ▶ default => sub { 'a4' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 1 );
51 has 'orientation' => (
52 is => 'rw',
53 isa => 'Str',
54 lazy => 1,
55 0 ▶ default => sub { 'Portrait' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 1 );
57 has 'disposition' => (
58 is => 'rw',
59 isa => 'Str',
60 lazy => 1,
61 0 ▶ default => sub { 'inline' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 1 );
63 has 'filename' => (
64 is => 'rw',
65 isa => 'Str',
66 lazy => 1,
67 0 ▶ default => sub { 'output.pdf' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 1 );
69 has 'allows' => (
70 is => 'rw',
71 isa => 'ArrayRef',
72 lazy => 1,
73 0 ▶ default => sub { [] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 1 );
75  
76 0 ▶sub process {
Subroutine process: not called CC=3 SCAR=24.8
Pod documented
77 0 my ( $self, $c ) = @_;
78  
79 0 my $wk = $c->stash->{ $self->stash_key };
80  
81 0 my $pdfcontent = $self->render($c, $wk);
82  
83 0 ▶ my $disposition = $wk->{disposition} || $self->disposition;
Condition0%0/3
$wk->{'disposition'} || $self->disposition
l!l&&r!l&&!r
000
MC/DC0%0/2
$wk->{'disposition'} || $self->disposition
$wk->{'disposition'} $self->disposition
Truth table0%0/3
A || B
A= $wk->{'disposition'}B= $self->disposition
ABresult
1X1
011
000
84 0 ▶ my $filename = uri_escape_utf8( $wk->{filename} || $self->filename );
Condition0%0/3
$wk->{'filename'} || $self->filename
l!l&&r!l&&!r
000
MC/DC0%0/2
$wk->{'filename'} || $self->filename
$wk->{'filename'} $self->filename
Truth table0%0/3
A || B
A= $wk->{'filename'}B= $self->filename
ABresult
1X1
011
000
85 0 $c->res->header(
86 'Content-Disposition' => "$disposition; filename*=UTF-8''$filename",
87 'Content-type' => 'application/pdf',
88 );
89 0 $c->res->body($pdfcontent);
90 }
91  
92 0 ▶sub render {
Subroutine render: not called CC=17 SCAR=56.2
Pod documented
93 0 my ( $self, $c, $args ) = @_;
94  
95 # Arguments for TT view - if not defined those will be the stash
96 # as per C::V::TT documentation
97 0 ▶ if (!$args->{template_args}) { $args->{template_args} = undef }
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"template_args"}) 0 0 0
98  
99 0 my $html;
100 0 ▶ if ( defined $args->{template} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'template'}) { } 0 0 0
101 0 ▶ $html = $c->view( $self->tt_view )->render( $c, $args->{template} ) or die;
Branch: 1 branch
BranchTrueFalseTotal
$html = $c->view($self->tt_view)->render($c, $args->{"template"}) or die 0 0 0
102 } else {
103 0 $html = $args->{html};
104 }
105 0 ▶ die 'Void-input' if !defined $html;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $html 0 0 0
106  
107 # Create a temporary file
108 1 ▶ use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 0 my $htmlf = File::Temp->new(
110 DIR => $self->tmpdir,
111 SUFFIX => '.html',
112 UNLINK => 1,
113 );
114 0 binmode $htmlf, ':raw';
115 0 my $htmlfn = $htmlf->filename;
116 0 my $pdffn = $htmlfn;
117 0 $pdffn =~ s/\.html/.pdf/;
118  
119 0 print $htmlf $html;
120  
121 0 my $send_filehandle = delete $args->{send_filehandle};
122  
123 0 my @args;
124  
125 0 ▶ if ( defined $args->{page_width} && defined $args->{page_height} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'page_width'} and defined $args->{'page_height'}) { } 0 0 0
Condition0%0/3
defined $args->{"page_width"} and defined $args->{"page_height"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args->{"page_width"} and defined $args->{"page_height"}
defined $args->{"page_width"} defined $args->{"page_height"}
Truth table0%0/3
A and B
A= defined $args->{"page_width"}B= defined $args->{"page_height"}
ABresult
0X0
100
111
126 # Custom page size overrides page_size
127 push @args, '--page-width', B::perlstring( $args->{page_width} ),
128 0 '--page-height', B::perlstring( $args->{page_height} );
129 }
130 else {
131 # Usual page size A4, but labels would need a smaller one
132 0 ▶ push @args, '--page-size', B::perlstring( $args->{page_size} || $self->page_size );
Condition0%0/3
$args->{'page_size'} || $self->page_size
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'page_size'} || $self->page_size
$args->{'page_size'} $self->page_size
Truth table0%0/3
A || B
A= $args->{'page_size'}B= $self->page_size
ABresult
1X1
011
000
133 }
134 0 ▶ push @args, '--orientation', B::perlstring( $args->{orientation} || $self->orientation );
Condition0%0/3
$args->{'orientation'} || $self->orientation
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'orientation'} || $self->orientation
$args->{'orientation'} $self->orientation
Truth table0%0/3
A || B
A= $args->{'orientation'}B= $self->orientation
ABresult
1X1
011
000
135  
136 0 push @args, '--allow', B::perlstring( $self->tmpdir );
137 0 for my $allow ( @{ $self->allows } ) {
138 0 push @args, '--allow', B::perlstring($allow);
139 }
140  
141 0 for my $name ( qw( margin_top margin_left margin_bottom margin_right dpi image_dpi image_quality title ) ) {
142 0 my $arg = $args->{$name};
143 0 ▶ next unless defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 0 0
144 0 ▶ die "${name} cannot contain newlines or control characters" if $arg =~ /[\N{U+00}-\N{U+1f}\#\|;\&]/;
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /[\N{U+00}-\N{U+1f}\#\|;\&]/ 0 0 0
145 0 my $param = $name;
146 0 $param =~ s/_/-/g;
147 0 push @args, "--${param}", B::perlstring($arg)
148 }
149  
150 0 for my $name ( qw( greyscale lowquality quiet no_background no_images disable_javascript print_media_type ) ) {
151 0 ▶ my $arg = $args->{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $arg = $args->{$name} or next 0 0 0
152 0 my $param = $name;
153 0 $param =~ s/_/-/g;
154 0 push @args, "--${param}";
155 }
156  
157 0 push @args, $htmlfn, $pdffn;
158  
159 0 my $input = join(" ", @args);
160  
161 0 my @cmd = split /\s+/, $self->command;
162  
163 0 my %opt = map +( "binmode_std$_" => ":raw" ), "in", "out", "err";
164 0 run3 [ @cmd, '--read-args-from-stdin' ], \ $input, \my $output, \my $err, \%opt;
165  
166 0 ▶ $c->log->debug($err) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
167  
168 0 ▶ if ( $send_filehandle ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($send_filehandle) { } 0 0 0
169 0 return IO::File::WithPath->new( $pdffn, '<:raw' );
170 }
171 else {
172 0 my $pdffc = Path::Class::File->new($pdffn);
173 0 my $pdfcontent = $pdffc->slurp();
174 0 $pdffc->remove();
175 0 return $pdfcontent;
176 }
177 }
178  
179 1 __PACKAGE__->meta->make_immutable();
180  
181 1 1;
182  
183 __END__
↑ Summary Helper/View/Wkhtmltopdf.pm »