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   341055 use strict;
  5         42  
  5         149  
3 5     5   48 use warnings;
  5         12  
  5         247  
4             our $VERSION = '0.10';
5 5     5   4283 use PDF::API2;
  5         1202883  
  5         206  
6              
7 5     5   63 use base 'Khonsu::File';
  5         8  
  5         2622  
8              
9             sub new {
10 4     4 1 478 my ($pkg, $name, %args) = @_;
11 4         56 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.10
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              
401             Load an external custom plugin.
402              
403             package Khonsu::Test;
404              
405             use parent 'Khonsu::Text';
406              
407             sub add {
408             my ($self, $file, %attributes) = @_;
409             return $self->SUPER::add($file, %attributes);
410             }
411              
412             1;
413              
414             ...
415              
416             Khonsu->load_plugin(qw/+Test/);
417              
418             my $khonsu = Khonsu->new(
419             'PDFName',
420             configure => {
421             test => {
422             ...
423             }
424             }
425             );
426              
427             $khonsu->add_test(...);
428              
429             =cut
430              
431             =head2 new
432              
433             Instantiate a new Khonsu Object and create an empty single page pdf.
434              
435             my $khonsu = Khonsu->new(
436             'PDFName',
437             %ATTRIBUTES,
438             configure => {
439             page_header => {
440             padding => $padding,
441             show_page_num => 'right',
442             page_num_text => 'page {num}',
443             h => $padding,
444             cb => sub {
445             my ($self, $file, %atts) = @_;
446             $self->add(
447             $file,
448             text => 'Ra',
449             align => 'center',
450             %attrs,
451             );
452             }
453             },
454             page_footer => {
455             padding => $padding,
456             show_page_num => 'left',
457             page_num_text => 'page {num}',
458             h => $padding,
459             cb => sub {
460             my ($self, $file, %atts) = @_;
461             $self->add(
462             $file,
463             text => 'Ra',
464             align => 'center',
465             %attrs,
466             );
467             }
468             },
469             toc => {
470             title => 'Table of contents',
471             title_font_args => {
472             size => 50,
473             },
474             title_padding => 10,
475             font_args => {
476             size => 20,
477             },
478             padding => 5,
479             },
480             }
481             );
482              
483             =cut
484              
485             =head2 add_page
486              
487             Add a new page to the pdf.
488              
489             $khonsu->add_page(
490             page_size => 'A4',
491             background => '#fff',
492             columns => 1,
493             column => 1,
494             column_y => 0,
495             is_rotated => 0,
496             header => Khonsu::Page::Header->new(%header),
497             footer => Khonsu::Page::Footer->new(%footer),
498             padding => 20,
499             toc => 0,
500             x => 0,
501             y => 0,
502             h => 842,
503             w => 595,
504             box => Khonsu::Shape::Box->new(%box)
505              
506             );
507              
508             =cut
509              
510             =head2 set_columns
511              
512             Set the number of page columns.
513              
514             $khonsu->set_columns(2);
515              
516             =cut
517              
518             =head2 open_page
519              
520             Open an existing page of the pdf.
521              
522             $khonsu->open_page(1);
523              
524             =cut
525              
526             =head2 add_page_header
527              
528             Add a custom header to the current page and all pages created/added after it.
529              
530             $khonsu->add_page_header(
531             padding => 20,
532             show_page_num => 'right',
533             page_num_text => 'page {num}',
534             h => 20,
535             cb => sub {
536             my ($self, $file, %atts) = @_;
537             $self->add(
538             $file,
539             text => 'Khonsu',
540             align => 'center',
541             %attrs,
542             );
543             }
544             );
545              
546             =cut
547              
548             =head2 add_page_footer
549              
550             Add a custom footer to the current page and all pages created/added after it.
551              
552             $khonsu->add_page_footer(
553             padding => 20,
554             show_page_num => 'left',
555             page_num_text => 'page {num}',
556             h => 20,
557             cb => sub {
558             my ($self, $file, %atts) = @_;
559             $self->add(
560             $file,
561             text => 'Khonsu',
562             align => 'center',
563             %attrs,
564             );
565             }
566             );
567              
568             =cut
569              
570             =head2 remove_page_header_and_footer
571              
572             Remove the page header and footer for the current page.
573              
574             $khonsu->remove_page_header_and_footer();
575              
576             =cut
577              
578             =head2 remove_page_header
579              
580             Remove the page header for the current page.
581              
582             $khonsu->remove_page_header();
583              
584             =cut
585              
586             =head2 remove_page_footer
587              
588             Remove the page footer for the current page.
589              
590             $khonsu->remove_page_footer();
591              
592             =cut
593              
594             =head2 add_toc
595              
596             Add a table of contents to the document.
597              
598             $khonsu->add_toc(
599             title => 'Table of contents',
600             title_font_args => {
601             size => 50,
602             },
603             title_padding => 10,
604             font_args => {
605             size => 20,
606             },
607             padding => 5,
608             x => 20,
609             y => 20,
610             w => $khonsu->page->w - 40,
611             h => $khonsu->page->h - 40
612             );
613              
614             =cut
615              
616             =head2 add_text
617              
618             Add text to the document.
619              
620             $khonsu->add_text(
621             text => 'This is a test ' x 24,
622             x => 20,
623             y => 120,
624             w => 100,
625             h => 120,
626             );
627              
628             =cut
629              
630             =head2 add_h1
631              
632             Add a h1 to the document.
633              
634             $khonsu->add_h1(
635             text => 'This is a h1',
636             );
637              
638             =cut
639              
640             =head2 add_h2
641              
642             Add a h2 to the document.
643              
644             $khonsu->add_h2(
645             text => 'This is a h2',
646             );
647             =cut
648              
649             =head2 add_h3
650              
651             Add a h3 to the document.
652              
653             $khonsu->add_h3(
654             text => 'This is a h3',
655             );
656              
657             =cut
658              
659             =head2 add_h4
660              
661             Add a h4 to the document.
662              
663             $khonsu->add_h4(
664             text => 'This is a h4',
665             );
666             =cut
667              
668             =head2 add_h5
669              
670             Add a h5 to the document.
671              
672             $khonsu->add_h5(
673             text => 'This is a h5',
674             );
675              
676             =cut
677              
678             =head2 add_h6
679              
680             Add a h6 to the document.
681              
682             $khonsu->add_h6(
683             text => 'This is a h6',
684             );
685              
686             =cut
687              
688             =head2 add_image
689              
690             Add a image to the document.
691              
692             $khonsu->add_image(
693             image => 't/test.png',
694             align => 'center'
695             );
696              
697             =cut
698              
699             =head2 add_form
700              
701             Start a pdf form.
702              
703             $khonsu->add_form();
704              
705             =cut
706              
707             =head2 add_input
708              
709             Add a input to the document.
710              
711             $khonsu->add_input(
712             text => 'Input:',
713             pad => '_'
714             );
715              
716             =cut
717              
718             =head2 add_select
719              
720             Add a select to the document.
721              
722             $khonsu->add_input(
723             text => 'Select:',
724             options => [qw/one two three four/],
725             pad => '_'
726             );
727              
728             =cut
729              
730             =head2 add_checkbox
731              
732             Add a checkbox to the document.
733              
734             $khonsu->add_checkbox(
735             text => 'Checkbox:',
736             );
737              
738             =cut
739              
740             =head2 add_line
741              
742             Add a line to the document.
743              
744             $khonsu->add_line(
745             fill_colour => '#000',
746             x => 140,
747             y => 20,
748             ex => 240,
749             ey => 20
750             );
751              
752             =cut
753              
754             =head2 add_box
755              
756             Add a box to the document.
757              
758             $khonsu->add_box(
759             fill_colour => '#000',
760             x => 20,
761             y => 20,
762             w => 100,
763             h => 100
764             );
765              
766             =cut
767              
768             =head2 add_circle
769              
770             Add a circle to the document.
771              
772             $khonsu->add_circle(
773             fill_colour => '#000',
774             x => 260,
775             y => 20,
776             r => 50
777             );
778              
779              
780             =cut
781              
782             =head2 add_pie
783              
784             Add a pie to the document.
785              
786             $khonsu->add_pie(
787             fill_colour => '#000',
788             x => 380,
789             y => 20,
790             r => 50,
791             rx => 360,
792             ry => 40
793             );
794              
795             $khonsu->add_pie(
796             fill_colour => '#fff',
797             x => 380,
798             y => 20,
799             r => 50,
800             rx => 400,
801             ry => 360
802             );
803              
804             =cut
805              
806             =head2 add_ellipse
807              
808             Add a ellipse to the document.
809              
810             $khonsu->add_ellipse(
811             fill_colour => '#000',
812             x => 500,
813             y => 20,
814             w => 30,
815             h => 50
816             );
817              
818             =cut
819              
820             =head2 load_font
821              
822             Load a custom font.
823              
824             $khonsu->load_font(
825             colour => '#000',
826             size => 20,
827             family => 'Times',
828             line_height => 25,
829             );
830              
831             =cut
832              
833             =head1 AUTHOR
834              
835             LNATION, C<< >>
836              
837             =head1 BUGS
838              
839             Please report any bugs or feature requests to C, or through
840             the web interface at L. I will be notified, and then you'll
841             automatically be notified of progress on your bug as I make changes.
842              
843             =head1 SUPPORT
844              
845             You can find documentation for this module with the perldoc command.
846              
847             perldoc Khonsu
848              
849             You can also look for information at:
850              
851             =over 4
852              
853             =item * RT: CPAN's request tracker (report bugs here)
854              
855             L
856              
857             =item * CPAN Ratings
858              
859             L
860              
861             =item * Search CPAN
862              
863             L
864              
865             =back
866              
867              
868             =head1 ACKNOWLEDGEMENTS
869              
870              
871             =head1 LICENSE AND COPYRIGHT
872              
873             This software is Copyright (c) 2023 by LNATION.
874              
875             This is free software, licensed under:
876              
877             The Artistic License 2.0 (GPL Compatible)
878              
879              
880             =cut
881              
882             1; # End of Khonsu