blib/lib/CtrlO/PDF.pm

filter: Statementstmt 86.3% 215 / 249 Branchbran 52.7% 38 / 72 Conditioncond 40.6% 13 / 32 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 90.7% 39 / 43 Podpod 100% 10 / 10 totaltotal 74.7% 317 / 424 CC 52 SCAR 22.9
CC
52
Cov
75%
image
42.0
text
29.7
move_y_position
12.0
CRAP 9.9
↑ Summary
1 package CtrlO::PDF;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Carp qw/croak carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Image::Info qw(image_info image_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use PDF::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use PDF::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 our $VERSION = '0.36';
16  
17 =head1 NAME
18  
19 CtrlO::PDF - high level PDF creator
20  
21 =head1 SYNOPSIS
22  
23 use CtrlO::PDF;
24 use Text::Lorem;
25  
26 my $pdf = CtrlO::PDF->new(
27 logo => "sample/logo.png", # optional
28 logo_scaling => 0.5, # Default
29 width => 595, # Default (A4, portrait mode)
30 height => 842, # Default (A4, portrait mode)
31 orientation => "portrait", # Default
32 margin => 40, # Default, all 4 sides
33 top_padding => 0, # Default
34 header => "My PDF document header", # optional
35 footer => "My PDF document footer", # optional
36 );
37 # width, height page dimensions in points (default A4 paper)
38 # orientation defaults to portrait (taller than wide)
39 # margin in points on all four sides
40 # top padding below header in points
41 # header, footer text line to place at top or bottom
42 # PDFlib actually checked only for '[aA]' or '[bB]', permitting a wide
43 # range of formats to specify the PDF support library
44  
45 # Add a page
46 $pdf->add_page;
47  
48 # Add headings
49 $pdf->heading('This is the main heading');
50 $pdf->heading('This is a sub-heading', size => 12);
51  
52 # Add paragraph text
53 my $lorem = Text::Lorem->new();
54 my $paras = $lorem->paragraphs(30);
55 $pdf->text($paras);
56  
57 # Add a table
58 my $data =[
59 ['Fruit', 'Quantity'], # Table header
60 ['Apples', 120],
61 ['Pears', 90],
62 ['Oranges', 30],
63 ];
64  
65 my $hdr_props = {
66 repeat => 1,
67 justify => 'center',
68 font_size => 8,
69 };
70  
71 $pdf->table(
72 data => $data,
73 header_props => $hdr_props,
74 );
75  
76 my $file = $pdf->content;
77  
78 # output the file
79 open my $pdf_out, '>', 'out.pdf';
80 binmode $pdf_out;
81 print $pdf_out $file;
82 close $pdf_out;
83  
84 # From version 0.34 (and PDF::Builder 3.028) it is possible to produce
85 # internal links in PDFs. For example:
86 $pdf->text(<<'__MARKDOWN', format => 'md1');
87 Go to an [internal link](link-elsewhere) somewhere else in this PDF.
88  
89 ---
90  
91 # System2 Configuration - Overview {#link-elsewhere}
92 __MARKDOWN
93  
94 =head1 DESCRIPTION
95  
96 This module tries to make it easy to create PDFs by providing a high level
97 interface to a number of existing PDF modules. It aims to "do the right thing"
98 by default, allowing minimal coding to create long PDFs. It includes
99 pagination, headings, paragraph text, images and tables. Although there are a
100 number of other modules to create PDFs with a high-level interface, I found
101 that these each lack certain features (e.g. image insertion, paragraph text).
102 This module tries to include each of those features through another existing
103 module. Also, as it is built on PDF::Builder, it provides access to that
104 object, so content can also be added directly using that, thereby providing any
105 powerful features required.
106  
107 B<Updates in v0.20> Note that version 0.20 contains a number breaking changes
108 to improve the default layout and spacing of a page. This better ensures that
109 content added to a page "just works" in terms of its layout, without needing
110 tweaks to its spacing. For example, headers have better spacing above and below
111 by default. This means that PDFs produced with this version will be laid out
112 differently to those produced with earlier versions. In the main, old code
113 should be able to be updated by simply removing any manual spacing fudges (e.g.
114 manual spacing for headers).
115  
116 B<Updates in v0.30> Version 0.30 has some fairly major changes, dropping
117 support of PDF::API2 and requiring use of PDF::Builder version 3.025. The
118 latter contains many updates and powerful new features to create feature-rich
119 PDF documents and means that PDF::TextBlock is no longer required for this
120 module, which uses PDF::Builder's new column() method instead.
121  
122 =head1 CONSTRUCTOR
123  
124 =head2 new
125  
126 The constructor is called C<new>, and accepts a optional hash of options.
127 Valid options are mainly the same as all the methods described below, for those
128 that get and set options. Some methods are read-only and must be set as options
129 to the constructor, others can be set later.
130  
131 =head1 METHODS
132  
133 =head2 pdf
134  
135 Returns the C<PDF::Builder> object used to create the PDF.
136  
137 =cut
138  
139 4 has pdf => (
140 is => 'lazy',
141 );
142  
143 sub _build_pdf
144 5 ▶{ my $self = shift;
Subroutine _build_pdf: called CC=5 SCAR=16.7
145  
146 # Now only supports PDF::Builder
147 5 ▶ croak "Sorry, CtrlO::PDF no longer supports use of PDF::API2"
Branch: 1 branch
BranchTrueFalseTotal
if $self->PDFlib and $self->PDFlib =~ /api2/i 0 5 5
Condition33%1/3
$self->PDFlib and $self->PDFlib =~ /api2/i
!ll&&!rl&&r
050
MC/DC0%0/2
$self->PDFlib and $self->PDFlib =~ /api2/i
$self->PDFlib $self->PDFlib =~ /api2/i
Truth table33%1/3
A and B
A= $self->PDFlibB= $self->PDFlib =~ /api2/i
ABresult
0X0
100
111
148 if $self->PDFlib && $self->PDFlib =~ /api2/i;
149  
150 5 my $rc = eval {
151 5 require PDF::Builder;# 3.025;
152 5 1;
153 };
154 5 ▶ croak "CtrlO::PDF requires PDF::Builder 3.025"
Branch: 1 branch
BranchTrueFalseTotal
unless $rc 0 5 5
155 if !$rc;
156  
157 5 my $pdf = PDF::Builder->new;
158  
159 5 ▶ $pdf->pass_start_state(1, 1, $self->state)
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_state 5 0 5
160 if $self->has_state;
161  
162 5 $pdf->add_font_path('/usr/share/fonts');
163 # Retained for backwards compatibility and moved from being built in the
164 # font() and fontbold() properties
165 5 $pdf->add_font(
166 face => 'liberation-sans',
167 type => 'ttf',
168 style => 'sans-serif',
169 width => 'proportional',
170 file => {
171 'roman' => 'truetype/liberation/LiberationSans-Regular.ttf',
172 'italic' => 'truetype/liberation/LiberationSans-Italic.ttf',
173 'bold' => 'truetype/liberation/LiberationSans-Bold.ttf',
174 'bold-italic' => 'truetype/liberation/LiberationSans-BoldItalic.ttf'
175 },
176 );
177 5 $pdf->font_settings(font => 'liberation-sans');
178  
179 5 $pdf;
180 }
181  
182 =head2 page
183  
184 Returns the current PDF page.
185  
186 =cut
187  
188 # Current page
189 has page => (
190 is => 'rwp',
191 lazy => 1,
192 0 ▶ builder => sub { $_[0]->add_page },
Subroutine _build_page: not called CC=1 SCAR=6.9
193 4 );
194  
195 =head2 add_page
196  
197 Adds a PDF page and returns it.
198  
199 Note that when a PDF page is added (either via this method or automatically)
200 the is_new_page flag records that a new page is in use with no content. See
201 that method for more details.
202  
203 =cut
204  
205 4 has page_count => (
206 is => 'rw',
207 default => 0,
208 );
209  
210 sub add_page
211 7 ▶{ my $self = shift;
Subroutine add_page: called CC=1 SCAR=0.0
Pod documented
212 7 my $page = $self->pdf->page;
213 7 $page->mediabox(0, 0, $self->width, $self->height);
214 7 $self->_set_page($page);
215 7 $self->_set__y($self->_y_start_default); # Reset y cursor
216 # Flag that we have just started a new page. Because text is positioned from
217 # its bottom-left corner, we will need to move the cursor down further to
218 # account for the font size of the text, but we don't know that yet.
219 7 $self->_set_is_new_page(1);
220 7 $self->page_count($self->page_count + 1);
221 7 return $page;
222 }
223  
224 =head2 is_new_page
225  
226 Whether the current page is new with no content. When the heading or text
227 methods are called and this is true, additional top margin is added to account
228 for the height of the text being added. Any other content manually added will
229 not include this margin and will leave the internal new page flag as true.
230  
231 =cut
232  
233 4 has is_new_page => (
234 is => 'rwp',
235 isa => Bool,
236 default => 1,
237 );
238  
239 =head2 clear_new_page
240  
241 Manually clears the is_new_page flag.
242  
243 =cut
244  
245 sub clear_new_page
246 3 ▶{ my $self = shift;
Subroutine clear_new_page: called CC=1 SCAR=0.0
Pod documented
247 3 $self->_set_is_new_page(0);
248 }
249  
250 =head2 orientation
251  
252 Sets or returns the page orientation (portrait or landscape). The default is
253 Portrait (taller than wide).
254  
255 =cut
256  
257 4 has orientation => (
258 is => 'ro',
259 isa => Str,
260 default => 'portrait',
261 );
262  
263 =head2 PDFlib
264  
265 Sets or returns the PDF-building library in use. The choices are "PDF::Builder"
266 and "PDF::API2" (case-insensitive). "PDF::Builder" is the default, indicating
267 that PDF::Builder will be used I<unless> it is not found, in which case
268 PDF::API2 will be used. If neither is found, CtrlO::PDF will fail.
269  
270 =cut
271  
272 4 has PDFlib => (
273 is => 'ro',
274 isa => Str,
275 default => 'PDF::Builder',
276 );
277  
278 =head2 font_size
279  
280 Sets or returns the font size. Default is 10 (points).
281  
282 =cut
283  
284 4 has font_size => (
285 is => 'ro',
286 isa => Int,
287 default => 10,
288 );
289  
290 =head2 width
291  
292 Sets or returns the width. Default is A4.
293  
294 =cut
295  
296 4 has width => (
297 is => 'lazy',
298 isa => Int,
299 );
300  
301 sub _build_width
302 5 ▶{ my $self = shift;
Subroutine _build_width: called CC=2 SCAR=7.2
303 5 ▶ $self->orientation eq 'portrait' ? 595 : 842; # A4 media
Branch: 1 branch
BranchTrueFalseTotal
$self->orientation eq 'portrait' ? : 5 0 5
304 }
305  
306 4 has _width_print => (
307 is => 'lazy',
308 isa => Int,
309 );
310  
311 sub _build__width_print
312 5 ▶{ my $self = shift;
Subroutine _build__width_print: called CC=1 SCAR=0.0
313 5 $self->width - $self->margin * 2;
314 }
315  
316 =head2 height
317  
318 Sets or returns the height. Default is A4.
319  
320 =cut
321  
322 4 has height => (
323 is => 'lazy',
324 isa => Int,
325 );
326  
327 sub _build_height
328 5 ▶{ my $self = shift;
Subroutine _build_height: called CC=2 SCAR=7.2
329 5 ▶ $self->orientation eq 'portrait' ? 842 : 595; # A4 media
Branch: 1 branch
BranchTrueFalseTotal
$self->orientation eq 'portrait' ? : 5 0 5
330 }
331  
332 =head2 margin
333  
334 Sets or returns the page margin. Default 40 pixels.
335  
336 =cut
337  
338 4 has margin => (
339 is => 'ro',
340 isa => Int,
341 default => 40,
342 );
343  
344 =head2 margin_top
345  
346 Sets or returns the top margin. Defaults to the margin + top_padding +
347 room for the header (if defined) + room for the logo (if defined).
348  
349 =cut
350  
351 4 has margin_top => (
352 is => 'lazy',
353 isa => Num,
354 );
355  
356 sub _build_margin_top
357 5 ▶{ my $self = shift;
Subroutine _build_margin_top: called CC=3 SCAR=11.0
358 5 my $size = $self->margin + $self->top_padding;
359 5 ▶ $size += $self->_line_height($self->font_size) if $self->header;
Branch: 1 branch
BranchTrueFalseTotal
if $self->header 2 3 5
360 5 ▶ if ($self->logo)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->logo) 1 4 5
361 {
362 1 $size += $self->logo_height;
363 1 $size += $self->logo_padding;
364 }
365 5 return int $size;
366 };
367  
368 =head2 margin_bottom
369  
370 Sets or returns the bottom margin. Defaults to the margin + room for the
371 footer.
372  
373 =cut
374  
375 4 has margin_bottom => (
376 is => 'lazy',
377 isa => Int,
378 );
379  
380 sub _build_margin_bottom
381 5 ▶{ my $self = shift;
Subroutine _build_margin_bottom: called CC=1 SCAR=0.0
382 # Allow room for the footer as well as space for the text to print
383 5 return $self->margin + 2 * $self->_line_height($self->font_size);
384 };
385  
386 =head2 top_padding
387  
388 Sets or returns the top padding (additional to the margin). Default 0.
389  
390 =cut
391  
392 4 has top_padding => (
393 is => 'ro',
394 isa => Int,
395 default => 0,
396 );
397  
398 =head2 header
399  
400 Sets or returns the header text.
401  
402 =cut
403  
404 4 has header => (
405 is => 'ro',
406 );
407  
408 =head2 footer
409  
410 Sets or returns the footer text. Page numbers are added automatically.
411  
412 =cut
413  
414 4 has footer => (
415 is => 'ro',
416 );
417  
418 =head2 font
419  
420 Sets or returns the font. This is based on PDF::Builder or PDF::API2 ttfont,
421 which returns a TrueType or OpenType font object. By default it assumes the
422 font is available in the exact path
423 C<truetype/liberation/LiberationSans-Regular.ttf>. A future
424 version may make this more flexible.
425  
426 =cut
427  
428 4 has font => (
429 is => 'lazy',
430 );
431  
432 sub _build_font
433 5 ▶{ my $self = shift;
Subroutine _build_font: called CC=1 SCAR=0.0
434 5 $self->pdf->get_font(face => 'liberation-sans', 'italic' => 0, bold => 0);
435 }
436  
437 =head2 fontbold
438  
439 As font, but a bold font.
440  
441 =cut
442  
443 4 has fontbold => (
444 is => 'lazy',
445 );
446  
447 sub _build_fontbold
448 5 ▶{ my $self = shift;
Subroutine _build_fontbold: called CC=1 SCAR=0.0
449 5 $self->pdf->get_font(face => 'liberation-sans', 'italic' => 0, bold => 1);
450 }
451  
452 =head2 logo
453  
454 The path to a logo to include in the top-right corner of every page (optional).
455  
456 =cut
457  
458 4 has logo => (
459 is => 'ro',
460 isa => Str,
461 );
462  
463 =head2 logo_scaling
464  
465 The scaling of the logo. For best results a setting of 0.5 is recommended (the
466 default).
467  
468 =cut
469  
470 4 has logo_scaling => (
471 is => 'ro',
472 default => 0.5,
473 );
474  
475 =head2 logo_padding
476  
477 The padding below the logo before the text. Defaults to 10 pixels.
478  
479 =cut
480  
481 4 has logo_padding => (
482 is => 'ro',
483 default => 10,
484 );
485  
486 4 has logo_height => (
487 is => 'lazy',
488 );
489  
490 sub _build_logo_height
491 1 ▶{ my $self = shift;
Subroutine _build_logo_height: called CC=2 SCAR=7.1
492 1 ▶ return 0 if !$self->_logo_info;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_logo_info 0 1 1
493 1 $self->_logo_info->{height} * $self->logo_scaling;
494 }
495  
496 4 has logo_width => (
497 is => 'lazy',
498 );
499  
500 sub _build_logo_width
501 1 ▶{ my $self = shift;
Subroutine _build_logo_width: called CC=2 SCAR=7.1
502 1 ▶ return 0 if !$self->_logo_info;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_logo_info 0 1 1
503 1 $self->_logo_info->{width} * $self->logo_scaling;
504 }
505  
506 4 has _logo_info => (
507 is => 'lazy',
508 );
509  
510 sub _build__logo_info
511 1 ▶{ my $self = shift;
Subroutine _build__logo_info: called CC=2 SCAR=7.1
512 1 ▶ return if !$self->logo;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logo 0 1 1
513 1 image_info($self->logo);
514 }
515  
516 has _x => (
517 is => 'rwp',
518 lazy => 1,
519 5 ▶ builder => sub { $_[0]->margin },
Subroutine _build__x: called CC=1 SCAR=0.0
520 4 );
521  
522 sub _down
523 15 ▶{ my ($self, $points) = @_;
Subroutine _down: called CC=1 SCAR=0.0
524 15 my $y = $self->_y;
525 15 $self->_set__y($y - $points);
526 }
527  
528 =head2 y_position
529  
530 Returns the current y position on the page. This value updates as the page is
531 written to, and is the location that content will be positioned at the next
532 write. Note that the value is measured from the bottom of the page.
533  
534 =cut
535  
536 sub y_position
537 5 ▶{ my $self = shift;
Subroutine y_position: called CC=1 SCAR=0.0
Pod documented
538 5 $self->_y;
539 }
540  
541 =head2 set_y_position($pixels)
542  
543 Sets the current Y position. See L</y_position>.
544  
545 =cut
546  
547 sub set_y_position
548 1 ▶{ my ($self, $y) = @_;
Subroutine set_y_position: called CC=3 SCAR=12.5
Pod documented
549 1 ▶ $y && $y =~ /^-?[0-9]+(\.[0-9]+)?$/
Branch: 1 branch
BranchTrueFalseTotal
$y and $y =~ /^-?[0-9]+(\.[0-9]+)?$/ or croak("Invalid y value for set_y_position: $y") 1 0 1
Condition33%1/3
$y and $y =~ /^-?[0-9]+(\.[0-9]+)?$/
!ll&&!rl&&r
001
MC/DC0%0/2
$y and $y =~ /^-?[0-9]+(\.[0-9]+)?$/
$y $y =~ /^-?[0-9]+(\.[0-9]+)?$/
Truth table33%1/3
A and B
A= $yB= $y =~ /^-?[0-9]+(\.[0-9]+)?$/
ABresult
0X0
100
111
550 or croak "Invalid y value for set_y_position: $y";
551 1 $self->_set__y($y);
552 }
553  
554 =head2 move_y_position($pixels)
555  
556 Moves the current Y position, relative to its current value. Positive values
557 will move the cursor up the page, negative values down. See L</y_position>.
558  
559 =cut
560  
561 sub move_y_position
562 0 ▶{ my ($self, $y) = @_;
Subroutine move_y_position: not called CC=3 SCAR=24.8
Pod documented
563 0 ▶ $y && $y =~ /^-?[0-9]+(\.[0-9]+)?$/
Branch: 1 branch
BranchTrueFalseTotal
$y and $y =~ /^-?[0-9]+(\.[0-9]+)?$/ or croak("Invalid y value for move_y_position: $y") 0 0 0
Condition0%0/3
$y and $y =~ /^-?[0-9]+(\.[0-9]+)?$/
!ll&&!rl&&r
000
MC/DC0%0/2
$y and $y =~ /^-?[0-9]+(\.[0-9]+)?$/
$y $y =~ /^-?[0-9]+(\.[0-9]+)?$/
Truth table0%0/3
A and B
A= $yB= $y =~ /^-?[0-9]+(\.[0-9]+)?$/
ABresult
0X0
100
111
564 or croak "Invalid y value for move_y_position: $y";
565 0 $self->_set__y($self->_y + $y);
566 }
567  
568 has _y => (
569 is => 'rwp',
570 lazy => 1,
571 1 ▶ builder => sub { $_[0]->_y_start_default },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build__y: called CC=1 SCAR=0.0
572 4 );
573  
574 sub _y_start_default
575 14 ▶{ my $self = shift;
Subroutine _y_start_default: called CC=1 SCAR=0.0
576 14 return $self->height - $self->margin_top;
577 }
578  
579 =head2 heading($text, %options)
580  
581 Add a heading. If called on a new page, will automatically move the cursor down
582 to account for the heading's height (based on the assumption that one pixel
583 equals one point). Options available are:
584  
585 =over
586  
587 =item size I<n>
588  
589 C<n> is the font size in points, B<default 16>
590  
591 =item indent I<n>
592  
593 C<n> is the amount (in points) to indent the text, B<default 0>
594  
595 =item topmargin I<n>
596  
597 C<n> is the amount (in points) of vertical skip for the margin I<above> the
598 heading, B<default:> calculated automatically based on font size
599  
600 =item bottommargin I<n>
601  
602 C<n> is the amount (in points) of vertical skip for the margin I<below> the
603 heading, B<default:> calculated automatically based on the font size
604  
605 =back
606  
607 =cut
608  
609 # Return the line height based on a font size, with optional ratio
610 24 ▶sub _line_height {
Subroutine _line_height: called CC=2 SCAR=6.9
611 24 my ($self, $size, $ratio) = @_;
612 24 ▶ $size * ($ratio || 1.5);
Condition100%2/2
$ratio || 1.5
l!l
915
MC/DC100%1/1
$ratio || 1.5
$ratio
Truth table100%2/2
A
A= $ratio
Aresult
11
00
613 }
614  
615 # Return the spacing above/below a line based on font size and line height
616 0 ▶sub _line_spacing {
Subroutine _line_spacing: not called CC=1 SCAR=6.9
617 0 my ($self, $size) = @_;
618 0 my $spacing = $self->_line_height($size);
619 0 ($spacing - $size) / 2;
620 }
621  
622 sub heading
623 4 ▶{ my ($self, $string, %options) = @_;
Subroutine heading: called CC=8 SCAR=21.7
Pod documented
624  
625 4 my $page = $self->page; # Ensure that page is built and cursor adjusted for first use
626  
627 4 ▶ $page = $self->add_page if $self->_y < 150; # Make sure there is room for following paragraph text
Branch: 1 branch
BranchTrueFalseTotal
if $self->_y < 150 0 4 4
628 4 ▶ my $size = $options{size} || 16;
Condition100%2/2
$options{'size'} || 16
l!l
22
MC/DC100%1/1
$options{'size'} || 16
$options{'size'}
Truth table100%2/2
A
A= $options{'size'}
Aresult
11
00
629  
630 4 ▶ if ($options{topmargin}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options{'topmargin'}) { } 0 4 4
elsif ($self->is_new_page) { } 2 2 4
631 # Always let override take precedence
632 0 ▶ $self->_down($options{topmargin}) if $options{topmargin};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"topmargin"} 0 0 0
633 }
634 elsif ($self->is_new_page)
635 {
636 # If a new page then move down just enough to fit in the font size
637 2 $self->_down($size);
638 2 $self->_set_is_new_page(0);
639 }
640 else {
641 # Default to top margin based on font size, with slightly higher
642 # spacing ratio than normal text
643 2 $self->_down($self->_line_height($size, 1.8));
644 }
645  
646 4 my $text = $page->text;
647 4 my $grfx = $page->gfx;
648 4 ▶ my $x = $self->_x + ($options{indent} || 0),
Condition50%1/2
$options{'indent'} || 0
l!l
04
MC/DC0%0/1
$options{'indent'} || 0
$options{'indent'}
Truth table50%1/2
A
A= $options{'indent'}
Aresult
11
00
649 my $height = $self->_y - $self->margin_bottom;
650 4 $text->font($self->fontbold, $size);
651 4 $text->translate($x, $self->_y);
652 4 $text->text($string);
653  
654 # Unless otherwise defined, add a bottom margin relative to the font size,
655 # but smaller than the top margin
656 my $bottommargin = defined $options{bottommargin}
657 ? $options{bottommargin}
658 4 ▶ : $self->_line_height($size, 0.4);
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'bottommargin'} ? : 0 4 4
659  
660 4 $self->_down($bottommargin);
661 }
662  
663 =head2 text($text, %options)
664  
665 Add paragraph text. This will automatically paginate. Available options are
666 shown below. Any unrecogised options will be passed to C<PDF::Builder>'s Column
667 method.
668  
669 =over
670  
671 =item format I<name>
672  
673 C<name> is the format of the text, in accordance with available formats in
674 C<PDF::Builder>. At the time of writing, supported options are C<none>, C<pre>,
675 C<md1> and C<html>. If unspecified defaults to C<none>.
676  
677 =item size I<n>
678  
679 C<n> is the font size in points, B<default 10>
680  
681 =item indent I<n>
682  
683 C<n> is the amount (in points) to indent the paragraph first line, B<default 0>
684  
685 =item top_padding I<n>
686  
687 C<n> is the amount (in points) of padding above the paragraph, only applied if
688 not at the top of a page. Defaults to half the line height.
689  
690 =item color I<name>
691  
692 C<name> is the string giving the text color, B<default 'black'>
693  
694 =back
695  
696 =cut
697  
698 sub text
699 3 ▶{ my ($self, $string, %options) = @_;
Subroutine text: called CC=14 SCAR=33.9
Pod documented
700  
701 3 ▶ $string or return;
Branch: 1 branch
BranchTrueFalseTotal
$string or return 3 0 3
702  
703 3 ▶ my $size = delete $options{size} || $self->font_size;
Condition33%1/3
delete $options{'size'} || $self->font_size
l!l&&r!l&&!r
030
MC/DC0%0/2
delete $options{'size'} || $self->font_size
delete $options{'size'} $self->font_size
Truth table33%1/3
A || B
A= delete $options{'size'}B= $self->font_size
ABresult
1X1
011
000
704 3 ▶ my $color = delete $options{color} || 'black';
Condition50%1/2
delete $options{'color'} || 'black'
l!l
03
MC/DC0%0/1
delete $options{'color'} || 'black'
delete $options{'color'}
Truth table50%1/2
A
A= delete $options{'color'}
Aresult
11
00
705 3 ▶ my $format = delete $options{format} || 'none';
Condition50%1/2
delete $options{'format'} || 'none'
l!l
03
MC/DC0%0/1
delete $options{'format'} || 'none'
delete $options{'format'}
Truth table50%1/2
A
A= delete $options{'format'}
Aresult
11
00
706  
707 3 my $page = $self->page; # Ensure that page is built and cursor adjusted for first use
708  
709 # Add new page if already at the bottom from previous operation (e.g.
710 # rendering table)
711 3 ▶ $page = $self->add_page
Branch: 1 branch
BranchTrueFalseTotal
if $self->_y - $self->_line_height($size) < $self->margin_bottom 0 3 3
712 if $self->_y - $self->_line_height($size) < $self->margin_bottom;
713  
714 3 my $text = $page->text;
715 3 my $grfx = $page->gfx;
716 3 ▶ my $x = $self->_x + ($options{indent} || 0),
Condition50%1/2
$options{'indent'} || 0
l!l
03
MC/DC0%0/1
$options{'indent'} || 0
$options{'indent'}
Truth table50%1/2
A
A= $options{'indent'}
Aresult
11
00
717 my $height = $self->_y - $self->margin_bottom;
718  
719 3 $text->font($self->font, 10); # Any size, overridden below
720  
721 # Whether this version has relative padding using percentage. If it does
722 # not then fallback to previous behaviour and do not set any styling,
723 # otherwise set the relative padding and the spacing between blocks of text
724 # to be consistent
725 3 my $has_advanced_style = version->parse($PDF::Builder::VERSION) >= 3.028;
726  
727 my $top_padding = defined $options{top_padding}
728 ? $options{top_padding}
729 # Default to a whole line height preceeding
730 3 ▶ : $self->_line_height($size, $has_advanced_style && 2) - $size;
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'top_padding'} ? : 0 3 3
Condition50%1/2
$has_advanced_style && 2
!ll
03
MC/DC0%0/1
$has_advanced_style && 2
$has_advanced_style
Truth table50%1/2
A
A= $has_advanced_style
Aresult
00
11
731  
732 # Only create spacing if below other content
733 3 ▶ if ($self->is_new_page)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_new_page) { } 1 2 3
734 {
735 1 $self->_set_is_new_page(0);
736 }
737 else {
738 2 $self->_down($top_padding);
739 }
740  
741 # As of PDF::Builder 3.028 there is a bug which prevents setting the margin
742 # on "li" elements. Instead, the margin needs to be set on the associated
743 # marker instead (_marker). There is also a separate bug which means that
744 # if the line spacing of the font of the marker is greater than that of the
745 # item text itself, then the spacing of the bullet lines will be too large.
746 # However, with the use of the liberation-sans font, the line spacing is
747 # actually smaller for the marker, hence it has no effect.
748 3 ▶ my $style = $has_advanced_style && '
Condition50%1/2
$has_advanced_style && "\n ol { margin-top: 100%; margin-bottom: 100% }\n ul { margin-top: 100%; margin-bottom: 100% }\n _marker { margin-top: 50%; margin-bottom: 0% }\n p { margin-top: 100%; margin-bottom: 100% }\n "
!ll
03
MC/DC0%0/1
$has_advanced_style && "\n ol { margin-top: 100%; margin-bottom: 100% }\n ul { margin-top: 100%; margin-bottom: 100% }\n _marker { margin-top: 50%; margin-bottom: 0% }\n p { margin-top: 100%; margin-bottom: 100% }\n "
$has_advanced_style
Truth table50%1/2
A
A= $has_advanced_style
Aresult
00
11
749 ol { margin-top: 100%; margin-bottom: 100% }
750 ul { margin-top: 100%; margin-bottom: 100% }
751 _marker { margin-top: 50%; margin-bottom: 0% }
752 p { margin-top: 100%; margin-bottom: 100% }
753 ';
754  
755 3 my ($rc, $next_y, $unused) = $text->column(
756 $page, $text, $grfx, $format, $string,
757 rect => [$x, $self->_y, $self->_width_print, $height],
758 para => [0, $top_padding],
759 state => $self->state,
760 # page is: $pass_count, $max_passes, $ppn, $extfilepath, $fpn, $LR, $bind
761 page => [ 1, 1, $self->page_count, undef, $self->page_count, undef, undef ],
762 font_size => $size,
763 font_info =>'-fm-',
764 style => $style,
765 %options
766 );
767  
768 3 my @last_unused;
769 3 ▶ while ($rc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) 0 3 3
770 # new page
771 0 $page = $self->add_page;
772 0 $height = $self->_y - $self->margin_bottom;
773 0 $text = $page->text;
774 0 $grfx = $page->gfx;
775  
776 0 ($rc, $next_y, $unused) = $text->column($page, $text, $grfx, 'pre', $unused,
777 rect => [$x, $self->_y, $self->_width_print, $height],
778 para => [0, $top_padding],
779 state => $self->state,
780 page => [ 1, 1, $self->page_count, undef, $self->page_count, undef, undef ],
781 font_size => $size,
782 font_info =>'-fm-',
783 style => $style,
784 %options,
785 );
786 0 $self->_set_is_new_page(0);
787 0 ▶ last unless grep length $_->{text}, @$unused;
Branch: 1 branch
BranchTrueFalseTotal
unless grep length($_->{"text"}), @$unused 0 0 0
788  
789 # We need a safety mechanism in case column() does not successfully
790 # print any of the text. In this situation, an infinite loop would
791 # occur, so look for this condition and bail out if so.
792 0 my @this_unused = grep length $_, map $_->{text}, @$unused;
793 0 ▶ croak "Unable to print text to PDF: @this_unused"
Branch: 1 branch
BranchTrueFalseTotal
if join($", @last_unused) eq join($", @this_unused) 0 0 0
794 if "@last_unused" eq "@this_unused";
795 0 @last_unused = @this_unused;
796 }
797  
798 3 $self->_set__y($next_y);
799 }
800  
801 =head2 table(%options)
802  
803 Add a table, based on PDF::Table. Options available are C<data> to pass in the
804 data for the table; all other options are passed to the table method of
805 PDF::Table.
806  
807 =cut
808  
809 sub table
810 3 ▶{ my ($self, %options) = @_;
Subroutine table: called CC=3 SCAR=11.0
Pod documented
811  
812 # Make sure we've got a page if this was the first time we've been called.
813 3 $self->page;
814  
815 # Add a blank line above the table if we'd printed anything previously
816 3 ▶ if ($self->is_new_page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_new_page) 2 1 3
817 2 $self->_down($self->_line_height($self->font_size));
818 }
819  
820 # Work out what sort of table we'd normally produce. Take a copy of the data because
821 # it's passed by reference to PDF::Table, which chews through its arguments.
822 3 my $table = PDF::Table->new;
823 3 my @data = @{ delete $options{data} };
824 3 my %dimensions = (
825 next_h => $self->_y_start_default - $self->margin_bottom,
826 x => $self->_x,
827 w => $self->_width_print,
828 font_size => $self->font_size,
829 padding => 5,
830 y => $self->_y,
831 h => $self->_y - $self->margin_bottom,
832 next_y => $self->_y_start_default,
833 );
834 %options = (
835 %dimensions,
836 h_border_w => 2,
837 v_border_w => 0,
838 border_c => '#dddddd',
839 bg_color_odd => '#f9f9f9',
840 1 ▶ new_page_func => sub { $self->add_page },
Subroutine __ANON__: called CC=1 SCAR=0.0
841 3 font => $self->font,
842 header_props => {
843 font => $self->fontbold,
844 repeat => 1,
845 justify => 'left',
846 font_size => $self->font_size,
847 bg_color => 'white',
848 fg_color => 'black',
849 },
850 %options,
851 );
852  
853 # Would the table fit on the page in its current position? PDF::Table 1.006 crashes if the
854 # header spans two page boundaries, and in any case we want at least the first row of the
855 # table to fit on the page.
856 # Returns: (0) total height of table, (1) height of header, (2) 0, (3) height of first
857 # row, (4) height of second row etc. - note that "first row" means the first non-header
858 # row.
859 3 my @vsizes = $table->table($self->pdf, $self->page, [@data], %options, ink => 0);
860 3 my $first_two_rows_height = $vsizes[1] + $vsizes[3];
861 3 ▶ if ($self->y_position - $first_two_rows_height < $self->margin_bottom) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->y_position - $first_two_rows_height < $self->margin_bottom) 1 2 3
862 1 $self->add_page;
863 1 $options{y} = $self->_y;
864 1 $options{h} = $self->_y - $self->margin_bottom;
865 }
866  
867 3 my ($final_page, $number_of_pages, $final_y) = $table->table(
868 $self->pdf,
869 $self->page,
870 [@data],
871 %options,
872 ink => 1,
873 );
874  
875 # Remember where we got to, and add another blank line below the table. This is definitely
876 # not the start of a new page now.
877 3 $self->_set__y($final_y);
878 3 $self->_down($self->_line_height($self->font_size));
879 3 $self->clear_new_page;
880 }
881  
882 sub _image_type
883 1 ▶{ my $file = shift;
Subroutine _image_type: called CC=2 SCAR=7.0
884 1 my $type = image_type($file);
885 croak "Unable to identify image type for $file: ".$type->{Errno}
886 1 ▶ if $type->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $type->{"error"} 0 1 1
887 1 return $type->{file_type};
888 }
889  
890 =head2 image($file, %options)
891  
892 Add an image. Options available are:
893  
894 =over
895  
896 =item scaling I<n>
897  
898 C<n> is the scaling factor for the image, B<default 0.5> (50%)
899  
900 =item alignment I<name>
901  
902 C<name> is the horizontal alignment, B<default center>
903  
904 =back
905  
906 =cut
907  
908 sub image
909 0 ▶{ my ($self, $file, %options) = @_;
Subroutine image: not called CC=6 SCAR=37.4
Pod documented
910 0 ▶ my $scaling = $options{scaling} || 0.5;
Condition0%0/2
$options{'scaling'} || 0.5
l!l
00
MC/DC0%0/1
$options{'scaling'} || 0.5
$options{'scaling'}
Truth table0%0/2
A
A= $options{'scaling'}
Aresult
11
00
911 0 my $info = image_info($file);
912 0 my $width = $info->{width};
913 0 my $height = $info->{height};
914 0 ▶ my $alignment = $options{alignment} || 'center';
Condition0%0/2
$options{'alignment'} || 'center'
l!l
00
MC/DC0%0/1
$options{'alignment'} || 'center'
$options{'alignment'}
Truth table0%0/2
A
A= $options{'alignment'}
Aresult
11
00
915 0 $height = $height * $scaling;
916 0 $width = $width * $scaling;
917 0 ▶ $self->add_page if $height > $self->_y;
Branch: 1 branch
BranchTrueFalseTotal
if $height > $self->_y 0 0 0
918 0 $self->_down($height);
919 0 ▶ my $x = $alignment eq 'left'
Branch: 2 branches
BranchTrueFalseTotal
$alignment eq 'left' ? : 0 0 0
$alignment eq 'right' ? : 0 0 0
920 ? $self->margin
921 : $alignment eq 'right'
922 ? $self->width - $self->margin - $width
923 : ($self->width / 2) - ($width / 2);
924 0 my $type = lc 'image_'._image_type($file);
925 0 my $image = $self->pdf->$type($file);
926 0 my $gfx = $self->page->gfx;
927 0 $gfx->image($image, $x, $self->_y, $scaling);
928 0 $self->clear_new_page;
929 }
930  
931 =head2 has_state
932  
933 A boolean dictating whether the version of PDF::Builder in use supports state
934 functionality, to retain information across multiple pages.
935  
936 =cut
937  
938 4 has has_state => (
939 is => 'lazy',
940 );
941  
942 sub _build_has_state
943 5 ▶{ my $self = shift;
Subroutine _build_has_state: called CC=1 SCAR=0.0
944 5 version->parse($PDF::Builder::VERSION) >= 3.028;
945 }
946  
947 =head2 state
948  
949 A hashref to configure and store the PDF::Builder state information. By default
950 this is built automatically using C<< PDF::Builder->init_state() >> and includes
951 configuration for the use of id parameters in header tags (enabling internal
952 linking). See the L<PDF::Builder|PDF::Builder::Content::Column_docs/init_state>
953 documentation for more information.
954  
955 =cut
956  
957 4 has state => (
958 is => 'lazy',
959 );
960  
961 sub _build_state
962 5 ▶{ my $self = shift;
Subroutine _build_state: called CC=2 SCAR=7.1
963 5 ▶ $self->has_state or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_state or return undef 5 0 5
964 +{
965 5 PDF::Builder->init_state({
966 # Allow these headings to have an id tag for internal hyperlinking
967 '_reft' => [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ],
968 })
969 }
970 }
971  
972 =head2 content
973  
974 Return the PDF content.
975  
976 =cut
977  
978 sub content
979 3 ▶{ my $self = shift;
Subroutine content: called CC=7 SCAR=19.5
Pod documented
980  
981 3 my $logo;
982 3 ▶ if ($self->logo)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->logo) 1 2 3
983 {
984 1 my $type = lc 'image_'._image_type($self->logo);
985 1 $logo = $self->pdf->$type($self->logo);
986 }
987 3 my $count = $self->pdf->pages;
988 3 foreach my $p (1..$count)
989 {
990 4 my $page = $self->pdf->openpage($p);
991 4 ▶ if ($logo)
Branch: 1 branch
BranchTrueFalseTotal
if ($logo) 1 3 4
992 {
993 1 my $gfx = $page->gfx;
994 1 $gfx->image($logo, $self->width - $self->margin - $self->logo_width, $self->height - $self->margin - $self->logo_height, $self->logo_scaling);
995 }
996 4 my $text = $page->text;
997 4 $text->font($self->font, 10);
998 # Specify the header and footer color, otherwise it takes the color of
999 # the last text block which can lead to unexpected behaviour. TODO:
1000 # Allow this to be defined somehow.
1001 4 $text->fillcolor('black');
1002 4 ▶ if (my $header = $self->header)
Branch: 1 branch
BranchTrueFalseTotal
if (my $header = $self->header) 2 2 4
1003 {
1004 2 $text->translate(int($self->width / 2), $self->height - $self->margin);
1005 2 $text->text_center($header);
1006 }
1007 4 $text->translate($self->width - $self->margin, $self->margin);
1008 4 $text->text_right("Page $p of $count");
1009 4 ▶ if (my $footer = $self->footer)
Branch: 1 branch
BranchTrueFalseTotal
if (my $footer = $self->footer) 4 0 4
1010 {
1011 4 $text->translate($self->margin, $self->margin);
1012 4 $text->text($footer);
1013 }
1014 }
1015  
1016 3 ▶ $self->page->text->pass_end_state(1, 1, $self->pdf, $self->state)
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_state 3 0 3
1017 if $self->has_state;
1018  
1019 3 $self->pdf->stringify;
1020 }
1021  
1022 =head1 LICENSE AND COPYRIGHT
1023  
1024 Copyright 2018-2026 Ctrl O Ltd
1025  
1026 This program is free software; you can redistribute it and/or modify it under
1027 the terms of either: the GNU General Public License (GPL) as published by the
1028 Free Software Foundation; or the Perl Artistic License (PAL).
1029  
1030 See http://dev.perl.org/licenses/ for more information.
1031  
1032 =cut
1033  
1034 4 1;
↑ Summary