[24/40] Adding single/multiple label printing to label export code and interface.
[koha.git] / labels / label-create-pdf.pl
1 #!/usr/bin/perl
2
3 # Copyright 2006 Katipo Communications.
4 # Some parts Copyright 2009 Foundations Bible College.
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use warnings;
23
24 use CGI;
25 use Sys::Syslog qw(syslog);
26 use Data::Dumper;
27
28 use C4::Debug;
29 use C4::Labels::Batch 1.000000;
30 use C4::Labels::Template 1.000000;
31 use C4::Labels::Layout 1.000000;
32 use C4::Labels::PDF 1.000000;
33 use C4::Labels::Label 1.000000;
34
35 =head
36
37 =cut
38
39 my $cgi = new CGI;
40
41 my $batch_id    = $cgi->param('batch_id') if $cgi->param('batch_id');
42 my $template_id = $cgi->param('template_id') || undef;
43 my $layout_id   = $cgi->param('layout_id') || undef;
44 my $start_label = $cgi->param('start_label') || 1;
45 my @label_ids   = $cgi->param('label_id') if $cgi->param('label_id');
46 my @item_numbers  = $cgi->param('item_number') if $cgi->param('item_number');
47
48 my $items = undef;
49
50 my $pdf_file = (@label_ids || @item_numbers ? "label_single_" . scalar(@label_ids || @item_numbers) : "label_batch_$batch_id");
51 print $cgi->header( -type       => 'application/pdf',
52                     -encoding   => 'utf-8',
53                     -attachment => "$pdf_file.pdf",
54                   );
55
56 my $pdf = C4::Labels::PDF->new(InitVars => 0);
57 my $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
58 my $template = C4::Labels::Template->retrieve(template_id => $template_id, profile_id => 1);
59 my $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
60
61 sub _calc_next_label_pos {
62     my ($row_count, $col_count, $llx, $lly) = @_;
63     if ($col_count lt $template->get_attr('cols')) {
64         $llx = ($llx + $template->get_attr('label_width') + $template->get_attr('col_gap'));
65         $col_count++;
66     }
67     else {
68         $llx = $template->get_attr('left_margin');
69         if ($row_count eq $template->get_attr('rows')) {
70             $pdf->Page();
71             $lly = ($template->get_attr('page_height') - $template->get_attr('top_margin') - $template->get_attr('label_height'));
72             $row_count = 1;
73         }
74         else {
75             $lly = ($lly - $template->get_attr('row_gap') - $template->get_attr('label_height'));
76             $row_count++;
77         }
78         $col_count = 1;
79     }
80     return ($row_count, $col_count, $llx, $lly);
81 }
82
83 sub _print_text {
84     my $label_text = shift;
85     foreach my $text_line (@$label_text) {
86         my $pdf_font = $pdf->Font($text_line->{'font'});
87         my $line = "BT /$pdf_font $text_line->{'font_size'} Tf $text_line->{'text_llx'} $text_line->{'text_lly'} Td ($text_line->{'line'}) Tj ET";
88         $pdf->Add($line);
89     }
90 }
91
92 $| = 1;
93
94 # set the paper size
95 my $lowerLeftX  = 0;
96 my $lowerLeftY  = 0;
97 my $upperRightX = $template->get_attr('page_width');
98 my $upperRightY = $template->get_attr('page_height');
99
100 $pdf->Compress(1);
101 $pdf->Mbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
102
103 my ($row_count, $col_count, $llx, $lly) = $template->get_label_position($start_label);
104
105 if (@label_ids) {
106     my $batch_items = $batch->get_attr('items');
107     grep {
108         my $label_id = $_;
109         push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
110     } @label_ids;
111 }
112 elsif (@item_numbers) {
113     grep {
114         push(@{$items}, {item_number => $_});
115     } @item_numbers;
116 }
117 else {
118     $items = $batch->get_attr('items');
119 }
120
121 LABEL_ITEMS:
122 foreach my $item (@{$items}) {
123     my ($barcode_llx, $barcode_lly, $barcode_width, $barcode_y_scale_factor) = 0,0,0,0;
124     my $label = C4::Labels::Label->new(
125                                     batch_id            => $batch_id,
126                                     item_number         => $item->{'item_number'},
127                                     width               => $template->get_attr('label_width'),
128                                     height              => $template->get_attr('label_height'),
129                                     top_text_margin     => $template->get_attr('top_text_margin'),
130                                     left_text_margin    => $template->get_attr('left_text_margin'),
131                                     barcode_type        => $layout->get_attr('barcode_type'),
132                                     printing_type       => $layout->get_attr('printing_type'),
133                                     guidebox            => $layout->get_attr('guidebox'),
134                                     font                => $layout->get_attr('font'),
135                                     font_size           => $layout->get_attr('font_size'),
136                                     callnum_split       => $layout->get_attr('callnum_split'),
137                                     justify             => $layout->get_attr('text_justify'),
138                                     format_string       => $layout->get_attr('format_string'),
139                                     text_wrap_cols      => $layout->get_text_wrap_cols(label_width => $template->get_attr('label_width'), left_text_margin => $template->get_attr('left_text_margin')),
140                                       );
141     my $label_type = $label->get_label_type;
142     if ($label_type eq 'BIB') {
143         my $line_spacer = ($label->get_attr('font_size') * 1);    # number of pixels between text rows (This is actually leading: baseline to baseline minus font size. Recommended starting point is 20% of font size.).
144         my $text_lly = ($lly + ($template->get_attr('label_height') - $template->get_attr('top_text_margin')));
145         my $label_text = $label->draw_label_text(
146                                         llx             => $llx,
147                                         lly             => $text_lly,
148                                         line_spacer     => $line_spacer,
149                                         );
150         _print_text($label_text);
151         ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
152         next LABEL_ITEMS;
153     }
154     elsif ($label_type eq 'BARBIB') {
155         $barcode_llx = $llx + $template->get_attr('left_text_margin');                             # this places the bottom left of the barcode the left text margin distance to right of the the left edge of the label ($llx)
156         $barcode_lly = ($lly + $template->get_attr('label_height')) - $template->get_attr('top_text_margin');        # this places the bottom left of the barcode the top text margin distance below the top of the label ($lly)
157         $barcode_width = 0.8 * $template->get_attr('label_width');                                 # this scales the barcode width to 80% of the label width
158         $barcode_y_scale_factor = 0.01 * $template->get_attr('label_height');                      # this scales the barcode height to 10% of the label height
159         my $line_spacer = ($label->get_attr('font_size') * 1);    # number of pixels between text rows (This is actually leading: baseline to baseline minus font size. Recommended starting point is 20% of font size.).
160         my $text_lly = ($lly + ($template->get_attr('label_height') - $template->get_attr('top_text_margin')));
161         my $label_text = $label->draw_label_text(
162                                         llx             => $llx,
163                                         lly             => $text_lly,
164                                         line_spacer     => $line_spacer,
165                                         );
166         _print_text($label_text);
167     }
168     else {
169         $barcode_llx = $llx + $template->get_attr('left_text_margin');             # this places the bottom left of the barcode the left text margin distance to right of the the left edge of the label ($llx)
170         $barcode_lly = $lly + $template->get_attr('top_text_margin');              # this places the bottom left of the barcode the top text margin distance above the bottom of the label ($lly)
171         $barcode_width = 0.8 * $template->get_attr('label_width');                 # this scales the barcode width to 80% of the label width
172         $barcode_y_scale_factor = 0.01 * $template->get_attr('label_height');      # this scales the barcode height to 10% of the label height
173         if ($label_type eq 'BIBBAR' || $label_type eq 'ALT') {
174             my $line_spacer = ($label->get_attr('font_size') * 1);    # number of pixels between text rows (This is actually leading: baseline to baseline minus font size. Recommended starting point is 20% of font size.).
175             my $text_lly = ($lly + ($template->get_attr('label_height') - $template->get_attr('top_text_margin')));
176             my $label_text = $label->draw_label_text(
177                                             llx             => $llx,
178                                             lly             => $text_lly,
179                                             line_spacer     => $line_spacer,
180                                             );
181             _print_text($label_text);
182         }
183         if ($label_type eq 'ALT') {
184         ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
185         }
186     }
187     $label->barcode(
188                 llx                 => $barcode_llx,
189                 lly                 => $barcode_lly,
190                 width               => $barcode_width,
191                 y_scale_factor      => $barcode_y_scale_factor,
192     );
193     ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
194 }
195
196 $pdf->End();
197
198 exit(1);