blib/lib/Catalyst/View/ChromePDF.pm

filter: Statementstmt 53.2% 57 / 107 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 68.1% 15 / 22 Podpod 100% 2 / 2 totaltotal 35.0% 74 / 211 CC 23 SCAR 34.7
CC
23
Cov
35%
render
90.0
_build_pdf_options
56.0
process
20.0
CRAP 32.2
↑ Summary
1 package Catalyst::View::ChromePDF;
2  
3 1 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 extends 'Catalyst::View';
7  
8 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Heap::Fibonacci (); # needed by IO::Async via WWW::Mechanize::Chrome
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IO::File::WithPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use MooseX::Aliases;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Types::Common qw( Enum HashRef InstanceOf NonEmptySimpleStr PositiveOrZeroInt StrMatch );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use URI::Escape qw( uri_escape_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use WWW::Mechanize::Chrome;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # RECOMMEND PREREQ: Catalyst::View::TT
20 # RECOMMEND PREREQ: Type::Tiny::XS
21  
22 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 our $VERSION = 'v0.1.6';
27  
28 # ABSTRACT: convert HTML (or TT) content to PDF using Chrome
29  
30  
31 1 has tmpdir => (
32 is => 'ro',
33 isa => NonEmptySimpleStr,
34 lazy_build => 1,
35 builder => '_build_tmpdir',
36 );
37  
38 0 ▶sub _build_tmpdir($self) {
Subroutine _build_tmpdir: not called CC=1 SCAR=6.9
39 0 return File::Spec->tmpdir;
40 }
41  
42  
43 1 has tt_view => (
44 is => 'ro',
45 isa => StrMatch[ qr/^[A-Z]\w*(::\w+)*$/a ],
46 default => 'TT',
47 );
48  
49  
50 1 has stash_key => (
51 is => 'ro',
52 isa => NonEmptySimpleStr,
53 default => 'pdf',
54 );
55  
56  
57 has chrome_args => (
58 is => 'ro',
59 isa => HashRef,
60 1 ▶ default => sub($self) { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
61 1 );
62  
63  
64 1 my $PageSizes = Enum [ keys %WWW::Mechanize::Chrome::PaperFormats ];
65  
66 1 has format => (
67 is => 'ro',
68 isa => $PageSizes,
69 alias => 'page_size',
70 default => 'a4',
71 );
72  
73  
74 1 my $Orientations = Enum [qw( portrait landscape )];
75  
76 1 has orientation => (
77 is => 'ro',
78 isa => $Orientations,
79 default => 'portrait',
80 );
81  
82  
83 1 my $Dispositions = Enum[ qw( inline attachment ) ];
84  
85 1 has 'disposition' => (
86 is => 'rw',
87 isa => $Dispositions,
88 default => 'inline',
89 );
90  
91  
92 1 has 'filename' => (
93 is => 'rw',
94 isa => NonEmptySimpleStr,
95 default => 'output.pdf',
96 );
97  
98  
99 1 has mech => (
100 is => 'ro',
101 isa => InstanceOf ['WWW::Mechanize::Chrome'],
102 builder => '_build_mech',
103 lazy_build => 1,
104 );
105  
106 0 ▶sub _build_mech($self) {
Subroutine _build_mech: not called CC=3 SCAR=24.8
107 0 my %args = $self->chrome_args->%*;
108 0 ▶ $args{headless} //= 1;
Condition0%0/2
$args{"headless"} //= 1
l!l
00
MC/DC0%0/1
$args{"headless"} //= 1
$args{"headless"}
Truth table0%0/2
A
A= $args{"headless"}
Aresult
11
00
109 0 ▶ $args{separate_session} //= 1;
Condition0%0/2
$args{"separate_session"} //= 1
l!l
00
MC/DC0%0/1
$args{"separate_session"} //= 1
$args{"separate_session"}
Truth table0%0/2
A
A= $args{"separate_session"}
Aresult
11
00
110 0 return WWW::Mechanize::Chrome->new(%args);
111 }
112  
113  
114 0 ▶sub process( $self, $c ) {
Subroutine process: not called CC=4 SCAR=30.0
Pod documented
115  
116 0 my $args = $c->stash->{ $self->stash_key };
117  
118 0 ▶ $c->res->body( $self->render( $c, $args // { } ) );
Condition0%0/2
$args // {}
l!l
00
MC/DC0%0/1
$args // {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
119  
120 0 ▶ my $disposition = $Dispositions->assert_return( $args->{disposition} // $self->disposition );
Condition0%0/3
$args->{"disposition"} // $self->disposition
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"disposition"} // $self->disposition
$args->{"disposition"} $self->disposition
Truth table0%0/3
A // B
A= $args->{"disposition"}B= $self->disposition
ABresult
1X1
011
000
121 0 ▶ my $filename = uri_escape_utf8( $args->{filename} // $self->filename );
Condition0%0/3
$args->{"filename"} // $self->filename
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"filename"} // $self->filename
$args->{"filename"} $self->filename
Truth table0%0/3
A // B
A= $args->{"filename"}B= $self->filename
ABresult
1X1
011
000
122  
123 0 $c->res->header(
124 "Content-Disposition" => "${disposition}; filename*=UTF-8''${filename}",
125 "Content-Type" => "application/pdf",
126 );
127  
128 0 return 1;
129 }
130  
131  
132 0 ▶sub render( $self, $c, $args ) {
Subroutine render: not called CC=9 SCAR=45.0
Pod documented
133  
134 0 my $html;
135 0 ▶ my $template = $args->{template} // $c->stash->{template};
Condition0%0/3
$args->{"template"} // $c->stash->{"template"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"template"} // $c->stash->{"template"}
$args->{"template"} $c->stash->{"template"}
Truth table0%0/3
A // B
A= $args->{"template"}B= $c->stash->{"template"}
ABresult
1X1
011
000
136 0 ▶ if ( defined $template ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $template) { } 0 0 0
137 0 $html = $c->view( $self->tt_view )->render( $c, $template );
138 }
139 else {
140 0 ▶ $html = $args->{html} // $c->res->body;
Condition0%0/3
$args->{"html"} // $c->res->body
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"html"} // $c->res->body
$args->{"html"} $c->res->body
Truth table0%0/3
A // B
A= $args->{"html"}B= $c->res->body
ABresult
1X1
011
000
141 0 ▶ if ( blessed($html) && $html->isa("IO::File") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($html) and $html->isa("IO::File")) 0 0 0
Condition0%0/3
builtin::blessed($html) and $html->isa("IO::File")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($html) and $html->isa("IO::File")
builtin::blessed($html) $html->isa("IO::File")
Truth table0%0/3
A and B
A= builtin::blessed($html)B= $html->isa("IO::File")
ABresult
0X0
100
111
142 0 die "Filehandles are not supported";
143 }
144 }
145 0 ▶ die 'Void-input' unless defined $html;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $html 0 0 0
146  
147 0 my $file = Path::Tiny->tempfile(
148 DIR => $self->tmpdir,
149 SUFFIX => ".html",
150 UNLINK => 1,
151 );
152  
153 0 $c->log->debug("Saving the HTML to ${file}");
154 0 $file->spew_raw($html);
155  
156 0 ▶ my $wait = PositiveOrZeroInt->assert_return( $args->{wait} // 0 );
Condition0%0/2
$args->{"wait"} // 0
l!l
00
MC/DC0%0/1
$args->{"wait"} // 0
$args->{"wait"}
Truth table0%0/2
A
A= $args->{"wait"}
Aresult
11
00
157  
158 0 ▶ my $mech = $args->{mech} // $self->mech;
Condition0%0/3
$args->{"mech"} // $self->mech
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"mech"} // $self->mech
$args->{"mech"} $self->mech
Truth table0%0/3
A // B
A= $args->{"mech"}B= $self->mech
ABresult
1X1
011
000
159  
160 0 ▶ return try {
Subroutine __ANON__: not called CC=4 SCAR=30.0
161 0 my $res = $mech->get_local( $file->stringify );
162  
163 0 ▶ $mech->infinite_scroll($wait) if $wait;
Branch: 1 branch
BranchTrueFalseTotal
if $wait 0 0 0
164  
165 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
166  
167 0 my $out = Path::Tiny->tempfile(
168 DIR => $self->tmpdir,
169 SUFFIX => ".pdf",
170 UNLINK => 1,
171 );
172  
173 0 my $ret;
174  
175 0 my %opts = $self->_build_pdf_options( $c, $args );
176  
177 0 ▶ if ( $args->{send_filehandle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'send_filehandle'}) { } 0 0 0
178  
179 0 $c->log->debug("Saving the PDF to ${out}");
180  
181 0 $mech->content_as_pdf( %opts, filename => $out->stringify );
182 0 $ret = IO::File::WithPath->new( $out, '<:raw' );
183  
184 }
185 else {
186  
187 0 $ret = $mech->content_as_pdf(%opts);
188  
189 }
190  
191 0 return $ret;
192  
193 }
194 else {
195  
196 0 die "Unknown error: HTTP " . $res->code;
197  
198 }
199  
200  
201 }
202 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
203  
204 0 my $e = "$_";
205 0 $c->log->error($e);
206 0 $c->error($e);
207  
208 0 return 0;
209 0 };
210  
211 }
212  
213  
214 0 ▶sub _build_pdf_options( $self, $, $args ) {
Subroutine _build_pdf_options: not called CC=7 SCAR=40.3
215  
216 0 ▶ my $size = $PageSizes->assert_return( $args->{page_size} // $args->{format} // $self->format );
Condition0%0/6
($args->{"page_size"} // $args->{"format"}) // $self->format
l!l&&r!l&&!r
000
$args->{"page_size"} // $args->{"format"}
l!l&&r!l&&!r
000
MC/DC0%0/3
($args->{"page_size"} // $args->{"format"}) // $self->format
$args->{"page_size"} $args->{"format"} $self->format
Truth table0%0/4
A // B // C
A= $args->{"page_size"}B= $args->{"format"}C= $self->format
ABCresult
1XX1
01X1
0011
0000
217 0 my ( $width, $height ) = map { $WWW::Mechanize::Chrome::PaperFormats{$size}{$_} } qw( width height );
218  
219 0 ▶ my $orientation = $Orientations->assert_return( $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
220 0 ▶ if ( $orientation eq "landscape" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orientation eq "landscape") 0 0 0
221 0 ( $width, $height ) = ( $height, $width );
222 }
223  
224 my %opts = (
225 paperWidth => $args->{paper_width} // $width,
226 0 ▶ paperHeight => $args->{paper_height} // $height,
Condition0%0/6
$args->{"paper_width"} // $width
l!l&&r!l&&!r
000
$args->{"paper_height"} // $height
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"paper_width"} // $width
$args->{"paper_width"} $width
MC/DC0%0/2
$args->{"paper_height"} // $height
$args->{"paper_height"} $height
Truth table0%0/3
A // B
A= $args->{"paper_width"}B= $width
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $args->{"paper_height"}B= $height
ABresult
1X1
011
000
227 );
228  
229 0 return %opts;
230 }
231  
232  
233 1 __PACKAGE__->meta->make_immutable();
234  
235 __END__
↑ Summary