File Coverage

blib/lib/Khonsu.pm
Criterion Covered Total %
statement 14 15 93.3
branch n/a
condition n/a
subroutine 5 6 83.3
pod 1 2 50.0
total 20 23 86.9


line stmt bran cond sub pod time code
1             package Khonsu;
2 5     5   335438 use strict;
  5         45  
  5         157  
3 5     5   38 use warnings;
  5         9  
  5         236  
4             our $VERSION = '0.11';
5 5     5   3907 use PDF::API2;
  5         1175577  
  5         190  
6              
7 5     5   65 use base 'Khonsu::File';
  5         14  
  5         2394  
8              
9             sub new {
10 4     4 1 464 my ($pkg, $name, %args) = @_;
11 4         50 return $pkg->SUPER::new(
12             file_name => $name,
13             pages => [],
14             page_size => 'A4',
15             page_args => {},
16             pdf => PDF::API2->new( -file => sprintf("%s.pdf", $name) ),
17             %args
18             );
19             }
20              
21 0     0 0   sub open { ... }
22              
23             1;
24              
25             =head1 NAME
26              
27             Khonsu - PDF Generation!
28              
29             =head1 VERSION
30              
31             Version 0.11
32              
33             =cut
34              
35             =head1 SYNOPSIS
36              
37             my @words = ('Aker', 'Anubis', 'Hapi', 'Khepri', 'Maahes', 'Thoth', 'Bastet', 'Hatmehit', 'Tefnut', 'Menhit', 'Imentet');
38              
39             my $generate_text = sub {
40             my $length = shift;
41             return join " ", map { $words[int(rand(scalar @words))] } 1 .. $length;
42             };
43              
44             use Khonsu;
45              
46             Khonsu->load_plugin(qw/+Syntax/);
47              
48             my $padding = 20;
49             my $page_padding = $padding * 2;
50             my $khonsu = Khonsu->new(
51             'Ra',
52             configure => {
53             page_header => {
54             padding => $padding,
55             show_page_num => 'right',
56             page_num_text => 'page {num}',
57             h => $padding,
58             cb => sub {
59             my ($self, $file, %atts) = @_;
60             $self->add(
61             $file,
62             text => 'Ra',
63             align => 'center',
64             %attrs,
65             );
66             }
67             },
68             page_footer => {
69             padding => $padding,
70             show_page_num => 'left',
71             page_num_text => 'page {num}',
72             h => $padding,
73             cb => sub {
74             my ($self, $file, %atts) = @_;
75             $self->add(
76             $file,
77             text => 'Ra',
78             align => 'center',
79             %attrs,
80             );
81             }
82             },
83             toc => {
84             title => 'Table of contents',
85             title_font_args => {
86             size => 50,
87             },
88             title_padding => 10,
89             font_args => {
90             size => 20,
91             },
92             padding => 5,
93             },
94             h1 => {
95             font => { colour => '#0EE' }
96             }
97             }
98             );
99              
100             $khonsu->add_image(
101             image => 't/test.png',
102             x => $padding,
103             y => $padding,
104             w => $khonsu->page->w - $page_padding,
105             h => $khonsu->page->h - $page_padding,
106             )->add_page;
107              
108             $khonsu->add_toc();
109              
110             $khonsu->set_columns(2);
111              
112             for (0 .. 100) {
113             $khonsu->add_h1(
114             text => $generate_text->(3),
115             toc => 1,
116             )->add_text(
117             text => $generate_text->(2000),
118             indent => 4,
119             font => {
120             colour => '#fff'
121             },
122             );
123             }
124              
125             $khonsu->set_columns(1);
126              
127             $khonsu->add_h1(
128             text => 'A simple form',
129             toc => 1
130             );
131              
132             $khonsu->add_input(
133             text => 'Name:'
134             );
135              
136             $khonsu->add_select(
137             text => 'Colour:',
138             options => [qw/red yellow green/]
139             );
140              
141             $khonsu->add_checkbox(
142             text => 'Checkbox:'
143             );
144              
145             $khonsu->add_syntax(
146             text => $perl_string
147             );
148              
149             $khonsu->save();
150              
151             =head1 ATTRIBUTES
152              
153             =cut
154              
155             =head2 file_name
156              
157             Get and set the PDF file name.
158              
159             $khonsu->file_name();
160             $khonsu->file_name('PDFName');
161              
162             =cut
163              
164             =head2 pdf
165              
166             Get and set the PDF::API2 object.
167              
168             $khonsu->pdf();
169             $khonsu->pdf(PDF::API2->new( -file => sprintf("%s.pdf", $khonsu->file_name()) ));
170              
171             =cut
172              
173             =head2 pages
174              
175             Get and set the pdf pages Objects.
176              
177             $khonsu->pages();
178             $khonsu->pages([Khonsu::Page->new(
179             page_size =>'A4',
180             num => 1,
181             )->add($khonsu)];
182              
183             =cut
184              
185             =head2 page
186              
187             Get and set the current page.
188              
189             $khonsu->page();
190             $khonsu->page($khonsu->pages()->[0]);
191              
192             =head2 page_args
193              
194             Get and set the default page_args used when creating/adding a new page.
195              
196             $khonsu->page_args();
197             $khonsu->page_args({ background => '#fff' });
198              
199             =cut
200              
201             =head2 page_offset
202              
203             Get and set the page_offset used while generating the table of contents.
204              
205             $khonsu->page_offset();
206             $khonsu->page_offset(5);
207              
208             =cut
209              
210             =head2 onsave_cbs
211              
212             Get and set onsave callbacks.
213              
214             $khonsu->onsave_cbs();
215             $khonsu->onsave_cbs([
216             'text',
217             'add',
218             {
219             text => 'On save callback',
220             }
221             ]);
222              
223             =cut
224              
225             =head2 line
226              
227             Get and set the Khonsu::Shape::Line object.
228              
229             $khonsu->line();
230             $khonsu->line(Khonsu::Shape::Line->new(%line));
231              
232             =cut
233              
234             =head2 box
235              
236             Get and set the Khonsu::Shape::Box object.
237              
238             $khonsu->box();
239             $khonsu->box(Khonsu::Shape::Box->new(%box));
240              
241             =cut
242              
243             =head2 circle
244              
245             Get and set the Khonsu::Shape::Circle object.
246              
247             $khonsu->circle();
248             $khonsu->circle(Khonsu::Shape::Circle->new(%circle));
249              
250             =cut
251              
252             =head2 pie
253              
254             Get and set the Khonsu::Shape::Pie object.
255              
256             $khonsu->pie();
257             $khonsu->pie(Khonsu::Shape::Pie->new(%pie));
258              
259             =cut
260              
261             =head2 ellipse
262              
263             Get and set the Khonsu::Shape::Ellipse object.
264              
265             $khonsu->ellipse();
266             $khonsu->ellipse(Khonsu::Shape::Ellipse->new(%ellipse));
267              
268             =cut
269              
270             =head2 font
271              
272             Get and set the Khonsu::Font object.
273              
274             $khonsu->font();
275             $khonsu->font(Khonsu::Font->new(%font));
276              
277             =cut
278              
279             =head2 text
280              
281             Get and set the Khonsu::Text object.
282              
283             $khonsu->text();
284             $khonsu->text(Khonsu::Text->new(%text));
285              
286             =cut
287              
288             =head2 h1
289              
290             Get and set the Khonsu::Text::H1 object.
291              
292             $khonsu->h1();
293             $khonsu->h1(Khonsu::Text::H1->new(%h1));
294              
295             =cut
296              
297             =head2 h2
298              
299             Get and set the Khonsu::Text::H2 object.
300              
301             $khonsu->h2();
302             $khonsu->h2(Khonsu::Text::H2->new(%h2));
303             =cut
304              
305             =head2 h3
306              
307             Get and set the Khonsu::Text::H3 object.
308              
309             $khonsu->h3();
310             $khonsu->h3(Khonsu::Text::H3->new(%h3));
311              
312             =cut
313              
314             =head2 h4
315              
316             Get and set the Khonsu::Text::H4 object.
317              
318             $khonsu->h4();
319             $khonsu->h4(Khonsu::Text::H4->new(%h4));
320              
321             =cut
322              
323             =head2 h5
324              
325             Get and set the Khonsu::Text::H5 object.
326              
327             $khonsu->h5();
328             $khonsu->h5(Khonsu::Text::H5->new(%h5));
329              
330             =cut
331              
332             =head2 h6
333              
334             Get and set the Khonsu::Text::H6 object.
335              
336             $khonsu->h6();
337             $khonsu->h6(Khonsu::Text::H6->new(%h6));
338             =cut
339              
340             =head2 image
341              
342             Get and set the Khonsu::Image object.
343              
344             $khonsu->image();
345             $khonsu->image(Khonsu::Image->new(%image));
346              
347             =cut
348              
349             =head2 toc
350              
351             Get and set the Khonsu::TOC object.
352              
353             $khonsu->toc();
354             $khonsu->toc(Khonsu::TOC->new(%toc));
355              
356             =cut
357              
358             =head2 form
359              
360             Get and set the Khonsu::Form object.
361              
362             $khonsu->form();
363             $khonsu->form(Khonsu::Form->new(%form));
364              
365             =cut
366              
367             =head2 input
368              
369             Get and set the Khonsu::Form::Field::Input object.
370              
371             $khonsu->input();
372             $khonsu->input(Khonsu::Form::Field::Input->new(%input));
373              
374             =cut
375              
376             =head2 select
377              
378             Get and set the Khonsu::Form::Field::Select object.
379              
380             $khonsu->select();
381             $khonsu->select(Khonsu::Form::Field::Select->new(%select));
382              
383             =cut
384              
385             =head2 checkbox
386              
387             Get and set the Khonsu::Form::Field::Checkbox object.
388              
389             $khonsu->checkbox();
390             $khonsu->checkbox(Khonsu::Form::Field::Checkbox->new(%checkbox));
391              
392             =cut
393              
394             =head1 METHODS
395              
396             =cut
397              
398             =head2 load_plugin
399              
400             Load an external custom plugin.
401              
402             package Khonsu::Test;
403              
404             use parent 'Khonsu::Text';
405              
406             sub add {
407             my ($self, $file, %attributes) = @_;
408             return $self->SUPER::add($file, %attributes);
409             }
410              
411             1;
412              
413             ...
414              
415             Khonsu->load_plugin(qw/+Test/);
416              
417             my $khonsu = Khonsu->new(
418             'PDFName',
419             configure => {
420             test => {
421             ...
422             }
423             }
424             );
425              
426             $khonsu->add_test(...);
427              
428             =cut
429              
430             =head2 new
431              
432             Instantiate a new Khonsu Object and create an empty single page pdf.
433              
434             my $khonsu = Khonsu->new(
435             'PDFName',
436             %ATTRIBUTES,
437             configure => {
438             page_header => {
439             padding => $padding,
440             show_page_num => 'right',
441             page_num_text => 'page {num}',
442             h => $padding,
443             cb => sub {
444             my ($self, $file, %atts) = @_;
445             $self->add(
446             $file,
447             text => 'Ra',
448             align => 'center',
449             %attrs,
450             );
451             }
452             },
453             page_footer => {
454             padding => $padding,
455             show_page_num => 'left',
456             page_num_text => 'page {num}',
457             h => $padding,
458             cb => sub {
459             my ($self, $file, %atts) = @_;
460             $self->add(
461             $file,
462             text => 'Ra',
463             align => 'center',
464             %attrs,
465             );
466             }
467             },
468             toc => {
469             title => 'Table of contents',
470             title_font_args => {
471             size => 50,
472             },
473             title_padding => 10,
474             font_args => {
475             size => 20,
476             },
477             padding => 5,
478             },
479             }
480             );
481              
482             =cut
483              
484             =head2 add_page
485              
486             Add a new page to the pdf.
487              
488             $khonsu->add_page(
489             page_size => 'A4',
490             background => '#fff',
491             columns => 1,
492             column => 1,
493             column_y => 0,
494             is_rotated => 0,
495             header => Khonsu::Page::Header->new(%header),
496             footer => Khonsu::Page::Footer->new(%footer),
497             padding => 20,
498             toc => 0,
499             x => 0,
500             y => 0,
501             h => 842,
502             w => 595,
503             box => Khonsu::Shape::Box->new(%box)
504              
505             );
506              
507             =cut
508              
509             =head2 set_columns
510              
511             Set the number of page columns.
512              
513             $khonsu->set_columns(2);
514              
515             =cut
516              
517             =head2 open_page
518              
519             Open an existing page of the pdf.
520              
521             $khonsu->open_page(1);
522              
523             =cut
524              
525             =head2 add_page_header
526              
527             Add a custom header to the current page and all pages created/added after it.
528              
529             $khonsu->add_page_header(
530             padding => 20,
531             show_page_num => 'right',
532             page_num_text => 'page {num}',
533             h => 20,
534             cb => sub {
535             my ($self, $file, %atts) = @_;
536             $self->add(
537             $file,
538             text => 'Khonsu',
539             align => 'center',
540             %attrs,
541             );
542             }
543             );
544              
545             =cut
546              
547             =head2 add_page_footer
548              
549             Add a custom footer to the current page and all pages created/added after it.
550              
551             $khonsu->add_page_footer(
552             padding => 20,
553             show_page_num => 'left',
554             page_num_text => 'page {num}',
555             h => 20,
556             cb => sub {
557             my ($self, $file, %atts) = @_;
558             $self->add(
559             $file,
560             text => 'Khonsu',
561             align => 'center',
562             %attrs,
563             );
564             }
565             );
566              
567             =cut
568              
569             =head2 remove_page_header_and_footer
570              
571             Remove the page header and footer for the current page.
572              
573             $khonsu->remove_page_header_and_footer();
574              
575             =cut
576              
577             =head2 remove_page_header
578              
579             Remove the page header for the current page.
580              
581             $khonsu->remove_page_header();
582              
583             =cut
584              
585             =head2 remove_page_footer
586              
587             Remove the page footer for the current page.
588              
589             $khonsu->remove_page_footer();
590              
591             =cut
592              
593             =head2 add_toc
594              
595             Add a table of contents to the document.
596              
597             $khonsu->add_toc(
598             title => 'Table of contents',
599             title_font_args => {
600             size => 50,
601             },
602             title_padding => 10,
603             font_args => {
604             size => 20,
605             },
606             padding => 5,
607             x => 20,
608             y => 20,
609             w => $khonsu->page->w - 40,
610             h => $khonsu->page->h - 40
611             );
612              
613             =cut
614              
615             =head2 add_text
616              
617             Add text to the document.
618              
619             $khonsu->add_text(
620             text => 'This is a test ' x 24,
621             x => 20,
622             y => 120,
623             w => 100,
624             h => 120,
625             );
626              
627             =cut
628              
629             =head2 add_h1
630              
631             Add a h1 to the document.
632              
633             $khonsu->add_h1(
634             text => 'This is a h1',
635             );
636              
637             =cut
638              
639             =head2 add_h2
640              
641             Add a h2 to the document.
642              
643             $khonsu->add_h2(
644             text => 'This is a h2',
645             );
646             =cut
647              
648             =head2 add_h3
649              
650             Add a h3 to the document.
651              
652             $khonsu->add_h3(
653             text => 'This is a h3',
654             );
655              
656             =cut
657              
658             =head2 add_h4
659              
660             Add a h4 to the document.
661              
662             $khonsu->add_h4(
663             text => 'This is a h4',
664             );
665             =cut
666              
667             =head2 add_h5
668              
669             Add a h5 to the document.
670              
671             $khonsu->add_h5(
672             text => 'This is a h5',
673             );
674              
675             =cut
676              
677             =head2 add_h6
678              
679             Add a h6 to the document.
680              
681             $khonsu->add_h6(
682             text => 'This is a h6',
683             );
684              
685             =cut
686              
687             =head2 add_image
688              
689             Add a image to the document.
690              
691             $khonsu->add_image(
692             image => 't/test.png',
693             align => 'center'
694             );
695              
696             =cut
697              
698             =head2 add_form
699              
700             Start a pdf form.
701              
702             $khonsu->add_form();
703              
704             =cut
705              
706             =head2 add_input
707              
708             Add a input to the document.
709              
710             $khonsu->add_input(
711             text => 'Input:',
712             pad => '_'
713             );
714              
715             =cut
716              
717             =head2 add_select
718              
719             Add a select to the document.
720              
721             $khonsu->add_input(
722             text => 'Select:',
723             options => [qw/one two three four/],
724             pad => '_'
725             );
726              
727             =cut
728              
729             =head2 add_checkbox
730              
731             Add a checkbox to the document.
732              
733             $khonsu->add_checkbox(
734             text => 'Checkbox:',
735             );
736              
737             =cut
738              
739             =head2 add_line
740              
741             Add a line to the document.
742              
743             $khonsu->add_line(
744             fill_colour => '#000',
745             x => 140,
746             y => 20,
747             ex => 240,
748             ey => 20
749             );
750              
751             =cut
752              
753             =head2 add_box
754              
755             Add a box to the document.
756              
757             $khonsu->add_box(
758             fill_colour => '#000',
759             x => 20,
760             y => 20,
761             w => 100,
762             h => 100
763             );
764              
765             =cut
766              
767             =head2 add_circle
768              
769             Add a circle to the document.
770              
771             $khonsu->add_circle(
772             fill_colour => '#000',
773             x => 260,
774             y => 20,
775             r => 50
776             );
777              
778              
779             =cut
780              
781             =head2 add_pie
782              
783             Add a pie to the document.
784              
785             $khonsu->add_pie(
786             fill_colour => '#000',
787             x => 380,
788             y => 20,
789             r => 50,
790             rx => 360,
791             ry => 40
792             );
793              
794             $khonsu->add_pie(
795             fill_colour => '#fff',
796             x => 380,
797             y => 20,
798             r => 50,
799             rx => 400,
800             ry => 360
801             );
802              
803             =cut
804              
805             =head2 add_ellipse
806              
807             Add a ellipse to the document.
808              
809             $khonsu->add_ellipse(
810             fill_colour => '#000',
811             x => 500,
812             y => 20,
813             w => 30,
814             h => 50
815             );
816              
817             =cut
818              
819             =head2 load_font
820              
821             Load a custom font.
822              
823             $khonsu->load_font(
824             colour => '#000',
825             size => 20,
826             family => 'Times',
827             line_height => 25,
828             );
829              
830             =cut
831              
832             =head1 AUTHOR
833              
834             LNATION, C<< >>
835              
836             =head1 BUGS
837              
838             Please report any bugs or feature requests to C, or through
839             the web interface at L. I will be notified, and then you'll
840             automatically be notified of progress on your bug as I make changes.
841              
842             =head1 SUPPORT
843              
844             You can find documentation for this module with the perldoc command.
845              
846             perldoc Khonsu
847              
848             You can also look for information at:
849              
850             =over 4
851              
852             =item * RT: CPAN's request tracker (report bugs here)
853              
854             L
855              
856             =item * CPAN Ratings
857              
858             L
859              
860             =item * Search CPAN
861              
862             L
863              
864             =back
865              
866              
867             =head1 ACKNOWLEDGEMENTS
868              
869              
870             =head1 LICENSE AND COPYRIGHT
871              
872             This software is Copyright (c) 2023 by LNATION.
873              
874             This is free software, licensed under:
875              
876             The Artistic License 2.0 (GPL Compatible)
877              
878              
879             =cut
880              
881             1; # End of Khonsu