« lib/PDF/FromHTML/Template/Container/Conditional.pm
↑ Summary
lib/PDF/FromHTML/Template/Container/Section.pm »
| 1 | package PDF::FromHTML::Template::Container::PageDef; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | BEGIN { | ||
| 6 | use vars qw(@ISA); | ||
| 7 | @ISA = qw(PDF::FromHTML::Template::Container); | ||
| 8 | |||
| 9 | use PDF::FromHTML::Template::Container; | ||
| 10 | |||
| 11 | } | ||
| 12 | use PDF::FromHTML::Template::Constants qw( %Verify ); | ||
| 13 | |||
| 14 | sub new | ||
| 15 | { | ||
| 16 | my $class = shift; | ||
| 17 | my $self = $class->SUPER::new(@_); | ||
| 18 | |||
| 19 | $self->{NOPAGENUMBER} = 0 unless defined $self->{NOPAGENUMBER}; | ||
| 20 | |||
| 21 | $self->{MARGINS} = 0 unless exists $self->{MARGINS}; | ||
| 22 | $self->{$_} = $self->{MARGINS} for grep !exists $self->{$_}, qw(LEFT_MARGIN RIGHT_MARGIN); | ||
| 23 | |||
| 24 | return $self; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub find_margin_heights | ||
| 28 | { | ||
| 29 | my $self = shift; | ||
| 30 | my ($context) = @_; | ||
| 31 | |||
| 32 | my ($header_height, $footer_height) = (undef, undef); | ||
| 33 | |||
| 34 | my $sub; | ||
| 35 | $sub = sub { | ||
| 36 | my $obj = shift; | ||
| 37 | |||
| 38 | $obj->enter_scope($context) unless $obj->isa('PAGEDEF'); | ||
| 39 | |||
| 40 | if ($obj->isa('HEADER')) | ||
| 41 | { | ||
| 42 | die "Cannot have two <header> tags in the same <pagedef>", $/ if defined $header_height; | ||
| 43 | $header_height = $context->get($obj, 'HEADER_HEIGHT'); | ||
| 44 | } | ||
| 45 | elsif ($obj->isa('FOOTER')) | ||
| 46 | { | ||
| 47 | die "Cannot have two <footer> tags in the same <pagedef>", $/ if defined $footer_height; | ||
| 48 | $footer_height = $context->get($obj, 'FOOTER_HEIGHT'); | ||
| 49 | } | ||
| 50 | else | ||
| 51 | { | ||
| 52 | $sub->($_) for grep { $_->isa('CONTAINER') } @{$obj->{ELEMENTS}}; | ||
| 53 | } | ||
| 54 | |||
| 55 | $obj->exit_scope($context, 1) unless $obj->isa('PAGEDEF'); | ||
| 56 | }; | ||
| 57 | |||
| 58 | $sub->($self); | ||
| 59 | |||
| 60 | $header_height ||= 0; | ||
| 61 | $footer_height ||= 0; | ||
| 62 | |||
| 63 | return ($header_height, $footer_height); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub enter_scope | ||
| 67 | { | ||
| 68 | my $self = shift; | ||
| 69 | my ($context) = @_; | ||
| 70 | |||
| 71 | $self->SUPER::enter_scope( $context ); | ||
| 72 | |||
| 73 | my ($pheight, $pwidth) = map { $context->get($self, $_) } qw(PAGE_HEIGHT PAGE_WIDTH); | ||
| 74 | unless (defined $pheight && defined $pwidth) | ||
| 75 | { | ||
| 76 | my $psize = $context->get($self, 'PAGESIZE'); | ||
| 77 | $self->_validate_option('PAGESIZE', \$psize); | ||
| 78 | |||
| 79 | for my $attr (qw(PAGE_HEIGHT PAGE_WIDTH)) | ||
| 80 | { | ||
| 81 | $self->{$attr} = $Verify{PAGESIZE}{$psize}{$attr}; | ||
| 82 | } | ||
| 83 | ($pheight, $pwidth) = @{$self}{qw(PAGE_HEIGHT PAGE_WIDTH)}; | ||
| 84 | } | ||
| 85 | |||
| 86 | # swap dimensions if landscape | ||
| 87 | if ($context->get($self, 'LANDSCAPE')) | ||
| 88 | { | ||
| 89 | ($pheight, $pwidth) = ($pwidth, $pheight); | ||
| 90 | |||
| 91 | @{$self}{qw(PAGE_HEIGHT PAGE_WIDTH)} | ||
| 92 | = @{$self}{qw(_ORIG_WIDTH _ORIG_HEIGHT)} | ||
| 93 | = @{$self}{qw(PAGE_WIDTH PAGE_HEIGHT)} | ||
| 94 | } | ||
| 95 | |||
| 96 | return 1; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub exit_scope | ||
| 100 | { | ||
| 101 | my $self = shift; | ||
| 102 | my ($context) = @_; | ||
| 103 | |||
| 104 | @{$self}{qw(PAGE_HEIGHT PAGE_WIDTH)} = delete @{$self}{qw(_ORIG_HEIGHT _ORIG_WIDTH)} | ||
| 105 | if exists $self->{_ORIG_HEIGHT}; | ||
| 106 | |||
| 107 | return $self->SUPER::exit_scope($context); | ||
| 108 | } | ||
| 109 | |||
| 110 | sub begin_page | ||
| 111 | { | ||
| 112 | my $self = shift; | ||
| 113 | my ($context) = @_; | ||
| 114 | |||
| 115 | $context->{X} = 0; | ||
| 116 | $context->{Y} = $context->get($self, 'START_Y'); | ||
| 117 | |||
| 118 | $context->reset_pagebreak; | ||
| 119 | |||
| 120 | return $self->SUPER::begin_page($context); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub render | ||
| 124 | { | ||
| 125 | my $self = shift; | ||
| 126 | my ($context) = @_; | ||
| 127 | |||
| 128 | my ($header_h, $footer_h) = $self->find_margin_heights($context); | ||
| 129 | |||
| 130 | my ($pheight, $pwidth) = map { $context->get($self, $_) } qw(PAGE_HEIGHT PAGE_WIDTH); | ||
| 131 | |||
| 132 | $self->{START_Y} = $pheight - $header_h; | ||
| 133 | $self->{END_Y} = $footer_h; | ||
| 134 | |||
| 135 | $context->new_page_def; | ||
| 136 | |||
| 137 | my $done = 0; | ||
| 138 | while (!$done) | ||
| 139 | { | ||
| 140 | $self->begin_page($context); | ||
| 141 | $context->{PDF}->begin_page($pwidth, $pheight); | ||
| 142 | |||
| 143 | $done = $self->iterate_over_children($context); | ||
| 144 | |||
| 145 | $context->{PDF}->end_page; | ||
| 146 | $self->end_page($context); | ||
| 147 | |||
| 148 | $context->increment_pagenumber unless $context->get($self, 'NOPAGENUMBER'); | ||
| 149 | } | ||
| 150 | |||
| 151 | return $done; | ||
| 152 | } | ||
| 153 | |||
| 154 | 1; | ||
| 155 | __END__ |