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 HTML::Template::Pro;
28 use Sys::Syslog qw(syslog);
37 use C4::Labels::Batch 1.000000;
38 use C4::Labels::Template 1.000000;
39 use C4::Labels::Layout 1.000000;
40 use C4::Labels::PDF 1.000000;
41 use C4::Labels::Label 1.000000;
45 my $htdocs_path = C4::Context->config('intrahtdocs');
46 my $batch_id = $cgi->param('batch_id') || $ARGV[0];
47 my $template_id = $cgi->param('template_id') || $ARGV[1];
48 my $layout_id = $cgi->param('layout_id') || $ARGV[2];
49 my $start_label = $cgi->param('start_label') || $ARGV[3];
51 print $cgi->header( -type => 'application/pdf', -attachment => "label_batch_$batch_id.pdf" );
53 my $pdf = C4::Labels::PDF->new(InitVars => 0);
54 my $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
55 my $template = C4::Labels::Template->retrieve(template_id => $template_id, profile_id => 1);
56 my $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
58 sub _calc_next_label_pos {
59 my ($row_count, $col_count, $llx, $lly) = @_;
60 if ($col_count lt $template->get_attr('cols')) {
61 $llx = ($llx + $template->get_attr('label_width') + $template->get_attr('col_gap'));
65 $llx = $template->get_attr('left_margin');
66 if ($row_count eq $template->get_attr('rows')) {
68 $lly = ($template->get_attr('page_height') - $template->get_attr('top_margin') - $template->get_attr('label_height'));
72 $lly = ($lly - $template->get_attr('row_gap') - $template->get_attr('label_height'));
77 return ($row_count, $col_count, $llx, $lly);
81 my $label_text = shift;
82 foreach my $text_line (@$label_text) {
83 my $pdf_font = $pdf->Font($text_line->{'font'});
84 my $line = "BT /$pdf_font $text_line->{'font_size'} Tf $text_line->{'text_llx'} $text_line->{'text_lly'} Td ($text_line->{'line'}) Tj ET";
94 my $upperRightX = $template->get_attr('page_width');
95 my $upperRightY = $template->get_attr('page_height');
98 $pdf->Mbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
100 my ($row_count, $col_count, $llx, $lly) = $template->get_label_position($start_label);
102 foreach my $item (@{$batch->get_attr('items')}) {
103 my ($barcode_llx, $barcode_lly, $barcode_width, $barcode_y_scale_factor) = 0,0,0,0;
104 my $label = C4::Labels::Label->new(
105 batch_id => $batch_id,
106 item_number => $item->{'item_number'},
107 width => $template->get_attr('label_width'),
108 height => $template->get_attr('label_height'),
109 top_text_margin => $template->get_attr('top_text_margin'),
110 left_text_margin => $template->get_attr('left_text_margin'),
111 barcode_type => $layout->get_attr('barcode_type'),
112 printing_type => $layout->get_attr('printing_type'),
113 guidebox => $layout->get_attr('guidebox'),
114 font => $layout->get_attr('font'),
115 font_size => $layout->get_attr('font_size'),
116 callnum_split => $layout->get_attr('callnum_split'),
117 justify => $layout->get_attr('text_justify'),
118 format_string => $layout->get_attr('format_string'),
119 text_wrap_cols => $layout->get_text_wrap_cols(label_width => $template->get_attr('label_width'), left_text_margin => $template->get_attr('left_text_margin')),
121 my $label_type = $label->get_label_type;
122 if ($label_type eq 'BIB') {
123 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.).
124 my $text_lly = ($lly + ($template->get_attr('label_height') - $template->get_attr('top_text_margin')));
125 my $label_text = $label->draw_label_text(
128 line_spacer => $line_spacer,
130 _print_text($label_text);
131 ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
134 elsif ($label_type eq 'BARBIB') {
135 $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)
136 $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)
137 $barcode_width = 0.8 * $template->get_attr('label_width'); # this scales the barcode width to 80% of the label width
138 $barcode_y_scale_factor = 0.01 * $template->get_attr('label_height'); # this scales the barcode height to 10% of the label height
139 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.).
140 my $text_lly = ($lly + ($template->get_attr('label_height') - $template->get_attr('top_text_margin')));
141 my $label_text = $label->draw_label_text(
144 line_spacer => $line_spacer,
146 _print_text($label_text);
149 $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)
150 $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)
151 $barcode_width = 0.8 * $template->get_attr('label_width'); # this scales the barcode width to 80% of the label width
152 $barcode_y_scale_factor = 0.01 * $template->get_attr('label_height'); # this scales the barcode height to 10% of the label height
153 if ($label_type eq 'BIBBAR' || $label_type eq 'ALT') {
154 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.).
155 my $text_lly = ($lly + ($template->get_attr('label_height') - $template->get_attr('top_text_margin')));
156 my $label_text = $label->draw_label_text(
159 line_spacer => $line_spacer,
161 _print_text($label_text);
163 if ($label_type eq 'ALT') {
164 ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
170 width => $barcode_width,
171 y_scale_factor => $barcode_y_scale_factor,
173 ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);