3 # Copyright Chris Nighswonger 2009
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use C4::Auth qw( get_template_and_user );
30 my ( undef, $loggedinuser, $cookie ) = get_template_and_user({
31 template_name => "labels/label-home.tt",
34 flagsrequired => { tools => 'label_creator' },
40 $batch_id = $cgi->param('batch_id') if $cgi->param('batch_id');
41 my $template_id = $cgi->param('template_id') || undef;
42 my $layout_id = $cgi->param('layout_id') || undef;
43 my $start_label = $cgi->param('start_label') || 1;
44 @label_ids = $cgi->multi_param('label_id') if $cgi->param('label_id');
45 @item_numbers = $cgi->multi_param('item_number') if $cgi->param('item_number');
46 my $from = $cgi->param('from') || undef;
47 my $to = $cgi->param('to') || undef;
48 my $barcode_length = $cgi->param('barcode_length');
52 my $pdf_file = (@label_ids || @item_numbers ? "label_single_" . scalar(@label_ids || @item_numbers) : "label_batch_$batch_id");
53 print $cgi->header( -type => 'application/pdf',
55 -attachment => "$pdf_file.pdf",
58 our $pdf = C4::Creators::PDF->new(InitVars => 0);
59 my $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
60 our $template = C4::Labels::Template->retrieve(template_id => $template_id, profile_id => 1);
61 my $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
63 sub _calc_next_label_pos {
64 my ($row_count, $col_count, $llx, $lly) = @_;
65 if ($col_count < $template->get_attr('cols')) {
66 $llx = ($llx + $template->get_attr('label_width') + $template->get_attr('col_gap'));
70 $llx = $template->get_attr('left_margin');
71 if ($row_count == $template->get_attr('rows')) {
73 $lly = ($template->get_attr('page_height') - $template->get_attr('top_margin') - $template->get_attr('label_height'));
77 $lly = ($lly - $template->get_attr('row_gap') - $template->get_attr('label_height'));
82 return ($row_count, $col_count, $llx, $lly);
86 my $label_text = shift;
87 foreach my $text_line (@$label_text) {
88 $pdf->Font($text_line->{'font'});
89 $pdf->FontSize( $text_line->{'font_size'} );
90 $pdf->Text( $text_line->{'text_llx'}, $text_line->{'text_lly'}, $text_line->{'line'} );
99 my $upperRightX = $template->get_attr('page_width');
100 my $upperRightY = $template->get_attr('page_height');
103 $pdf->Mbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
105 my ($row_count, $col_count, $llx, $lly) = $template->get_label_position($start_label);
108 my $batch_items = $batch->get_attr('items');
111 push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
114 elsif (@item_numbers) {
116 push(@{$items}, {item_number => $_});
119 elsif ($from and $to) {
120 for my $i ( $from .. $to ) {
121 my $padding = '0' x ( $barcode_length - length($i) );
122 push @$items, { item_number => $padding . $i };
126 $items = $batch->get_attr('items');
130 foreach my $item (@{$items}) {
131 my ($barcode_llx, $barcode_lly, $barcode_width, $barcode_y_scale_factor) = 0,0,0,0;
132 if ($layout->get_attr('printing_type') eq 'ALT') { # we process the ALT style printing type here because it is not an atomic printing type
133 my $label_a = C4::Labels::Label->new(
134 batch_id => $batch_id,
135 item_number => $item->{'item_number'},
138 width => $template->get_attr('label_width'),
139 height => $template->get_attr('label_height'),
140 top_text_margin => $template->get_attr('top_text_margin'),
141 left_text_margin => $template->get_attr('left_text_margin'),
142 barcode_type => $layout->get_attr('barcode_type'),
143 printing_type => 'BIB',
144 guidebox => $layout->get_attr('guidebox'),
145 oblique_title => $layout->get_attr('oblique_title'),
146 font => $layout->get_attr('font'),
147 font_size => $layout->get_attr('font_size'),
148 scale_width => $layout->get_attr('scale_width'),
149 scale_height => $layout->get_attr('scale_height'),
150 callnum_split => $layout->get_attr('callnum_split'),
151 justify => $layout->get_attr('text_justify'),
152 format_string => $layout->get_attr('format_string'),
153 text_wrap_cols => $layout->get_text_wrap_cols(
154 label_width => $template->get_attr('label_width'),
155 left_text_margin => $template->get_attr('left_text_margin')
158 $pdf->Add($label_a->draw_guide_box) if $layout->get_attr('guidebox');
159 my $label_a_text = $label_a->create_label();
160 _print_text($label_a_text);
161 ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
162 my $label_b = C4::Labels::Label->new(
163 batch_id => $batch_id,
164 item_number => $item->{'item_number'},
167 width => $template->get_attr('label_width'),
168 height => $template->get_attr('label_height'),
169 top_text_margin => $template->get_attr('top_text_margin'),
170 left_text_margin => $template->get_attr('left_text_margin'),
171 barcode_type => $layout->get_attr('barcode_type'),
172 printing_type => 'BAR',
173 guidebox => $layout->get_attr('guidebox'),
174 oblique_title => $layout->get_attr('oblique_title'),
175 font => $layout->get_attr('font'),
176 font_size => $layout->get_attr('font_size'),
177 scale_width => $layout->get_attr('scale_width'),
178 scale_height => $layout->get_attr('scale_height'),
179 callnum_split => $layout->get_attr('callnum_split'),
180 justify => $layout->get_attr('text_justify'),
181 format_string => $layout->get_attr('format_string'),
182 text_wrap_cols => $layout->get_text_wrap_cols(
183 label_width => $template->get_attr('label_width'),
184 left_text_margin => $template->get_attr('left_text_margin')
187 $pdf->Add($label_b->draw_guide_box) if $layout->get_attr('guidebox');
188 my $label_b_text = $label_b->create_label();
189 ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
194 my $label = C4::Labels::Label->new(
195 batch_id => $batch_id,
196 item_number => $item->{'item_number'},
199 width => $template->get_attr('label_width'),
200 height => $template->get_attr('label_height'),
201 top_text_margin => $template->get_attr('top_text_margin'),
202 left_text_margin => $template->get_attr('left_text_margin'),
203 barcode_type => $layout->get_attr('barcode_type'),
204 printing_type => $layout->get_attr('printing_type'),
205 guidebox => $layout->get_attr('guidebox'),
206 oblique_title => $layout->get_attr('oblique_title'),
207 font => $layout->get_attr('font'),
208 font_size => $layout->get_attr('font_size'),
209 scale_width => $layout->get_attr('scale_width'),
210 scale_height => $layout->get_attr('scale_height'),
211 callnum_split => $layout->get_attr('callnum_split'),
212 justify => $layout->get_attr('text_justify'),
213 format_string => $layout->get_attr('format_string'),
214 text_wrap_cols => $layout->get_text_wrap_cols(
215 label_width => $template->get_attr('label_width'),
216 left_text_margin => $template->get_attr('left_text_margin')
219 $pdf->Add($label->draw_guide_box) if $layout->get_attr('guidebox');
220 $label->{'barcode'} = $item->{'item_number'} if ($from and $to);
221 my $label_text = $label->create_label();
222 _print_text($label_text) if $label_text;
223 ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
233 labels/label-create-pdf.pl - A script for creating a pdf export of labels and label batches in Koha
237 This script provides the means of producing a pdf of labels for items either individually, in groups, or in batches from within Koha.
241 This script is intended to be called as a cgi script although it could be easily modified to accept command line parameters. The script accepts four single
242 parameters and two "multiple" parameters as follows:
244 C<batch_id> A single valid batch id to export.
245 C<template_id> A single valid template id to be applied to the current export. This parameter is manditory.
246 C<layout_id> A single valid layout id to be applied to the current export. This parameter is manditory.
247 C<start_label> The number of the label on which to begin the export. This parameter is optional.
248 C<lable_ids> A single valid label id to export. Multiple label ids may be submitted to export multiple labels.
249 C<item_numbers> A single valid item number to export. Multiple item numbers may be submitted to export multiple items.
251 B<NOTE:> One of the C<batch_id>, C<label_ids>, or C<item_number> parameters is manditory. However, do not pass a combination of them or bad things might result.
254 http://staff-client.kohadev.org/cgi-bin/koha/labels/label-create-pdf.pl?batch_id=1&template_id=1&layout_id=5&start_label=1
258 Chris Nighswonger <cnighswonger AT foundations DOT edu>
262 Copyright 2009 Foundations Bible College.
266 This file is part of Koha.
268 Koha is free software; you can redistribute it and/or modify it
269 under the terms of the GNU General Public License as published by
270 the Free Software Foundation; either version 3 of the License, or
271 (at your option) any later version.
273 Koha is distributed in the hope that it will be useful, but
274 WITHOUT ANY WARRANTY; without even the implied warranty of
275 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
276 GNU General Public License for more details.
278 You should have received a copy of the GNU General Public License
279 along with Koha; if not, see <http://www.gnu.org/licenses>.
281 =head1 DISCLAIMER OF WARRANTY
283 Koha is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
284 A PARTICULAR PURPOSE. See the GNU General Public License for more details.