3 # Copyright 2006 Katipo Communications.
4 # Some parts Copyright 2009 Foundations Bible College.
6 # This file is part of Koha.
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
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.
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
25 use Sys::Syslog qw(syslog);
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;
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');
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',
53 -attachment => "$pdf_file.pdf",
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);
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'));
68 $llx = $template->get_attr('left_margin');
69 if ($row_count eq $template->get_attr('rows')) {
71 $lly = ($template->get_attr('page_height') - $template->get_attr('top_margin') - $template->get_attr('label_height'));
75 $lly = ($lly - $template->get_attr('row_gap') - $template->get_attr('label_height'));
80 return ($row_count, $col_count, $llx, $lly);
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";
97 my $upperRightX = $template->get_attr('page_width');
98 my $upperRightY = $template->get_attr('page_height');
101 $pdf->Mbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
103 my ($row_count, $col_count, $llx, $lly) = $template->get_label_position($start_label);
106 my $batch_items = $batch->get_attr('items');
109 push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
112 elsif (@item_numbers) {
114 push(@{$items}, {item_number => $_});
118 $items = $batch->get_attr('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')),
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(
148 line_spacer => $line_spacer,
150 _print_text($label_text);
151 ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
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(
164 line_spacer => $line_spacer,
166 _print_text($label_text);
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(
179 line_spacer => $line_spacer,
181 _print_text($label_text);
183 if ($label_type eq 'ALT') {
184 ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
190 width => $barcode_width,
191 y_scale_factor => $barcode_y_scale_factor,
193 ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);