ReportWriter/Pdf/Report.pm

filter: Statementstmt 11.0% 12 / 109 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 10.5% 2 / 19 Podpod 0.0% 0 / 17 totaltotal 8.6% 14 / 162 CC 11 SCAR 28.1
CC
11
Cov
10%
new
56.0
finish_report
12.0
set_font
6.0
CRAP 16.6
« ReportWriter/Pdf.pm ↑ Summary ReportWriter/OOCalc.pm »
1 package Business::ReportWriter::Pdf::Report;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use PDF::API2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 my %DEFAULTS = (
7 PageSize => 'A4',
8 PageOrientation => 'Portrait',
9 Compression => 1,
10 PdfVersion => 3,
11 marginX => 30,
12 marginY => 30,
13 font => "Helvetica",
14 size => 12,
15 );
16  
17 1 my ( $day, $month, $year ) = ( localtime(time) )[ 3 .. 5 ];
18 1 my $DATE = sprintf "%02d/%02d/%04d", ++$month, $day, 1900 + $year;
19  
20 1 my %INFO = (
21 Creator => "None",
22 Producer => "None",
23 CreationDate => $DATE,
24 Title => "Untitled",
25 Subject => "None",
26 Author => "Auto-generated",
27 );
28  
29 1 my @parameterlist = qw(
30 PageSize
31 PageWidth
32 PageHeight
33 PageOrientation
34 Compression
35 PdfVersion
36 );
37  
38 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod undocumented
39 0 my $class = shift;
40 0 my %defaults = @_;
41  
42 0 foreach my $dflt (@parameterlist) {
43 0 ▶ if ( defined( $defaults{$dflt} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $defaults{$dflt}) 0 0 0
44 0 $DEFAULTS{$dflt} = $defaults{$dflt}; # Overridden from user
45 }
46 }
47  
48 # Set the width and height of the page
49 my ( $x1, $y1, $pageWidth, $pageHeight )
50 0 = PDF::API2::Util::page_size( $DEFAULTS{PageSize} );
51  
52 # Swap w and h if landscape
53 0 ▶ if ( lc( $DEFAULTS{PageOrientation} ) =~ /landscape/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc($DEFAULTS{"PageOrientation"}) =~ /landscape/) 0 0 0
54 0 my $tempW = $pageWidth;
55 0 $pageWidth = $pageHeight;
56 0 $pageHeight = $tempW;
57 0 $tempW = undef;
58 }
59  
60 0 my $MARGINX = $DEFAULTS{marginX};
61 0 my $MARGINY = $DEFAULTS{marginY};
62  
63 # May not need alot of these, will review later
64 0 my $self = { #pdf => PDF::API2->new(),
65 hPos => undef,
66 vPos => undef,
67 size => 12, # Default
68 font => undef, # the font object
69 PageWidth => $pageWidth,
70 PageHeight => $pageHeight,
71 Xmargin => $MARGINX,
72 Ymargin => $MARGINY,
73 BodyWidth => $pageWidth - $MARGINX * 2,
74 BodyHeight => $pageHeight - $MARGINY * 2,
75 page => undef, # the current page object
76 page_nbr => 1,
77 align => 'left',
78 linewidth => 1,
79 linespacing => 0,
80 FtrFontName => 'Helvetica-Bold',
81 FtrFontSize => 11,
82 MARGIN_DEBUG => 0,
83 PDF_API2_VERSION => $PDF::API2::VERSION,
84  
85 ########################################################
86 # Cache for font object caching -- used by set_font() ###
87 ########################################################
88 __font_cache => {},
89 };
90  
91 0 ▶ if ( length( $defaults{File} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $defaults{'File'}) { } 0 0 0
92 $self->{pdf} = PDF::API2->open( $defaults{File} )
93 0 ▶ or die "$defaults{File} not found: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"pdf"} = "PDF::API2"->open($defaults{"File"}) or die qq[$defaults{"File"} not found: $!\n] 0 0 0
94  
95 }
96 else {
97 0 $self->{pdf} = PDF::API2->new();
98 }
99  
100 # Default fonts
101 0 $self->{font} = $self->{pdf}->corefont('Helvetica'); # Default font object
102 #$self->{font}->encode('latin1');
103  
104 # Set the users options
105 0 foreach my $key ( keys %defaults ) {
106 0 $self->{$key} = $defaults{$key};
107 }
108  
109 0 bless $self, $class;
110  
111 0 return $self;
112 }
113  
114 0 ▶sub get_pages {
Subroutine get_pages: not called CC=1 SCAR=6.9
Pod undocumented
115 0 my $self = shift;
116  
117 0 return $self->{pdf}->pages;
118 }
119  
120 0 ▶sub new_page {
Subroutine new_page: not called CC=1 SCAR=6.9
Pod undocumented
121 0 my ($self, $no_page_number) = @_;
122  
123 # make a new page
124 0 $self->{page} = $self->{pdf}->page;
125 0 $self->{page}->mediabox( $self->{PageWidth}, $self->{PageHeight} );
126  
127 # Handle the page numbering if this page is to be numbered
128 0 my $total = $self->get_pages;
129 0 push( @{ $self->{no_page_num} }, $no_page_number );
130  
131 0 $self->{page_nbr}++;
132 0 return (0);
133 }
134  
135 0 ▶sub get_pagedimensions {
Subroutine get_pagedimensions: not called CC=1 SCAR=6.9
Pod undocumented
136 0 my $self = shift;
137  
138 0 return ( $self->{PageWidth}, $self->{PageHeight} );
139 }
140  
141 0 ▶sub set_size {
Subroutine set_size: not called CC=1 SCAR=6.9
Pod undocumented
142 0 my ( $self, $size ) = @_;
143  
144 0 $self->{size} = $size;
145 }
146  
147 0 ▶sub set_font {
Subroutine set_font: not called CC=2 SCAR=17.9
Pod undocumented
148 0 my ( $self, $font, $size ) = @_;
149  
150 0 ▶ if ( exists $self->{__font_cache}->{$font} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'__font_cache'}{$font}) { } 0 0 0
151 0 $self->{font} = $self->{__font_cache}->{$font};
152 }
153 else {
154 0 $self->{font} = $self->{pdf}->corefont($font);
155 0 $self->{__font_cache}->{$font} = $self->{font};
156 }
157  
158 0 $self->{fontname} = $font;
159 }
160  
161 0 ▶sub get_add_textpos {
Subroutine get_add_textpos: not called CC=1 SCAR=6.9
Pod undocumented
162 0 my ($self) = @_;
163  
164 0 return ( $self->{hPos}, $self->{vPos} );
165 }
166  
167 0 ▶sub get_stringwidth {
Subroutine get_stringwidth: not called CC=1 SCAR=6.9
Pod undocumented
168 0 my ($self, $string) = @_;
169  
170 0 my $txt = $self->{page}->text;
171 0 $txt->font( $self->{font}, $self->{size} );
172  
173 0 return $txt->advancewidth($string);
174 }
175  
176 0 ▶sub draw_line {
Subroutine draw_line: not called CC=1 SCAR=6.9
Pod undocumented
177 0 my ( $self, $x1, $y1, $x2, $y2 ) = @_;
178  
179 0 my $gfx = $self->{page}->gfx;
180 0 $gfx->move( $x1, $y1 );
181 0 $gfx->linewidth( $self->{linewidth} );
182 0 $gfx->linewidth(.1);
183 0 $gfx->line( $x2, $y2 );
184 0 $gfx->stroke;
185 }
186  
187 0 ▶sub draw_rect {
Subroutine draw_rect: not called CC=1 SCAR=6.9
Pod undocumented
188 0 my ( $self, $x1, $y1, $x2, $y2 ) = @_;
189  
190 0 my $gfx = $self->{page}->gfx;
191 0 $gfx->linewidth( $self->{linewidth} );
192 0 $gfx->rectxy( $x1, $y1, $x2, $y2 );
193 0 $gfx->stroke;
194 }
195  
196 0 ▶sub shade_rect {
Subroutine shade_rect: not called CC=1 SCAR=6.9
Pod undocumented
197 0 my ( $self, $x1, $y1, $x2, $y2, $color ) = @_;
198  
199 0 my $gfx = $self->{page}->gfx;
200  
201 0 $gfx->fillcolor($color);
202 0 $gfx->rectxy( $x1, $y1, $x2, $y2 );
203 0 $gfx->fill;
204 0 $gfx->fillcolor('black');
205 }
206  
207 0 ▶sub set_gfxlinewidth {
Subroutine set_gfxlinewidth: not called CC=1 SCAR=6.9
Pod undocumented
208 0 my ( $self, $width ) = @_;
209  
210 0 $self->{linewidth} = $width;
211 }
212  
213 0 ▶sub add_img_scaled {
Subroutine add_img_scaled: not called CC=1 SCAR=6.9
Pod undocumented
214 0 my ( $self, $file, $x, $y, $scale ) = @_;
215  
216 0 $self->add_img( $file, $x, $y, $scale );
217 }
218  
219 0 ▶sub add_img {
Subroutine add_img: not called CC=1 SCAR=6.9
Pod undocumented
220 0 my ( $self, $file, $x, $y, $scale ) = @_;
221  
222 0 my %type = (
223 jpeg => "jpeg",
224 jpg => "jpeg",
225 tif => "tiff",
226 tiff => "tiff",
227 pnm => "pnm",
228 gif => "gif",
229 png => "png",
230 );
231  
232 0 $file =~ /\.(\w+)$/;
233 0 my $ext = $1;
234  
235 0 my $sub = "image_$type{$ext}";
236 0 my $img = $self->{pdf}->$sub($file);
237 0 my $gfx = $self->{page}->gfx;
238  
239 0 $gfx->image( $img, $x, $y, $scale );
240 }
241  
242 0 ▶sub set_textcolor {
Subroutine set_textcolor: not called CC=1 SCAR=6.9
Pod undocumented
243 0 my ( $self, $color ) = @_;
244  
245 0 $self->{textcolor} = $color;
246 }
247  
248 0 ▶sub add_paragraph {
Subroutine add_paragraph: not called CC=2 SCAR=17.9
Pod undocumented
249 0 my ( $self, $text, $hPos, $vPos, $width, $height, $indent, $lead ) = @_;
250  
251 0 my $txt = $self->{page}->text;
252 0 $txt->font( $self->{font}, $self->{size} );
253  
254 0 ▶ my $textcolor = $self->{textcolor} || 'black';
Condition0%0/2
$self->{'textcolor'} || 'black'
l!l
00
MC/DC0%0/1
$self->{'textcolor'} || 'black'
$self->{'textcolor'}
Truth table0%0/2
A
A= $self->{'textcolor'}
Aresult
11
00
255 0 $txt->fillcolor($textcolor);
256 0 $txt->lead($lead); # Line spacing
257 0 $txt->translate( $hPos, $vPos );
258 0 $txt->paragraph( $text, $width, $height, -align => 'justified' );
259  
260 0 ( $self->{hPos}, $self->{vPos} ) = $txt->textpos;
261 }
262  
263 0 ▶sub finish_report {
Subroutine finish_report: not called CC=3 SCAR=24.8
Pod undocumented
264 0 my ($self, $callback) = @_;
265  
266 0 my $total = $self->{page_nbr} - 1;
267  
268 # Call the callback if one was given to us
269 0 ▶ if ( ref($callback) eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $callback eq 'CODE') { } 0 0 0
elsif (not $callback =~ /none/i) { } 0 0 0
270 0 &$callback( $self, $total );
271  
272 # This will print a footer if no $callback is passed for backwards
273 # compatibility
274 }
275 elsif ( $callback !~ /none/i ) {
276 0 &gen_page_footer( $self, $total, $callback );
277 }
278  
279 0 $self->{pdf}->info(%INFO);
280 0 my $out = $self->{pdf}->stringify;
281  
282 0 return $out;
283 }
284  
285 1 1;
286 __END__
« ReportWriter/Pdf.pm ↑ Summary ReportWriter/OOCalc.pm »