Merge branch 'master' into topic/cross_download
[sgn.git] / lib / PDF / LabelPage.pm
bloba81597ddb5c79291ef40e866fc0ba25f84dfbab2
2 package PDF::LabelPage;
4 use Moose;
5 use Data::Dumper;
7 has 'pdf' => ( isa => 'PDF::Create',
8 is => 'rw',
9 );
11 has 'labels' => ( isa => 'ArrayRef',
12 is => 'rw',
13 default => sub { [] },
16 has 'rows' => ( isa => 'Int',
17 is => 'rw',
20 has 'cols' => ( isa => 'Int',
21 is => 'rw',
24 has 'top_margin' => (isa=>'Int',
25 is => 'rw',
28 has 'bottom_margin' => (isa=>'Int',
29 is => 'rw',
32 has 'left_margin' => (isa => 'Int',
33 is => 'rw',
36 has 'right_margin' => (isa => 'Int',
37 is => 'rw',
40 has 'page_format' => (isa => 'Str',
41 is => 'rw',
42 default => 'Letter',
45 sub add_label {
46 my $self = shift;
47 my $label = shift;
49 my $labels = $self->labels();
50 push @$labels, $label;
51 $self->labels($labels);
53 print STDERR "Now we have ".(scalar(@{$self->labels()}))." labels in the page object.\n";
56 sub render {
57 my $self = shift;
59 my $page = $self->pdf()->new_page(Mediabox=>$self->pdf->get_page_size($self->page_format));
61 my ($page_width, $page_height) = @{$self->pdf->get_page_size($self->page_format)}[2,3];
63 my $label_height = int( ($page_height - $self->top_margin - $self->bottom_margin) / $self->rows());
65 my $label_width = int( ($page_width - $self->left_margin - $self->right_margin) / $self->cols());
66 my $final_barcode_width = ($page_width - $self->right_margin - $self->left_margin) / $self->cols;
68 my @images =@{ $self->labels() };
70 print STDERR "Rendering page with @images labels on it\n";
71 foreach my $row (1..$self->rows()) {
72 foreach my $col (1..$self->cols()) {
73 my $label_boundary = $page_height - (($row-1) * $label_height) - $self->top_margin;
74 $page->line($page_width -100, $label_boundary, $page_width, $label_boundary);
75 my $index = ($row -1) * $self->cols() + $col -1;
76 my $image = $images[$index];
77 #print STDERR "RENDER: IMAGE: ".Data::Dumper::Dumper($image)."\n\n";
80 if (!defined($image)) { next; }
82 my $scalex = $final_barcode_width / $image->{width};
83 my $scaley = $label_height / $image->{height};
85 my $ypos = $label_boundary - int( ($label_height - $image->{height} * $scaley) /2);
86 my $xpos = $label_width * ($col -1);
88 print STDERR "Printing label: row $row; col $col = index $index X: $xpos. Y: $ypos\n";
90 if ($scalex < $scaley) { $scaley = $scalex; }
91 else { $scalex = $scaley; }
93 #foreach my $label_count (1..$self->cols) {
94 $page->image(image=>$image, xpos=>$xpos, ypos=>$ypos, xalign=>0, yalign=>2, xscale=>$scalex, yscale=>$scaley);
102 sub need_more_labels {
103 my $self = shift;
105 if (scalar(@{$self->labels()}) < ($self->rows * $self->cols)) {
106 return 1;
108 else {
109 return 0;