Bug 26176: (QA follow-up) Branch -> Library
[koha.git] / labels / label-create-pdf.pl
1 #!/usr/bin/perl
2
3 # Copyright Chris Nighswonger 2009
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20
21 use Modern::Perl;
22
23 use CGI qw ( -utf8 );
24 use C4::Auth qw( get_template_and_user );
25 use C4::Creators;
26 use C4::Labels;
27
28 my $cgi = CGI->new;
29
30 my ( undef, $loggedinuser, $cookie ) = get_template_and_user({
31                                                                      template_name   => "labels/label-home.tt",
32                                                                      query           => $cgi,
33                                                                      type            => "intranet",
34                                                                      flagsrequired   => { tools => 'label_creator' },
35                                                                      });
36
37 my $batch_id;
38 my @label_ids;
39 my @item_numbers;
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');
49
50 my $items = undef;
51
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',
54                     -encoding   => 'utf-8',
55                     -attachment => "$pdf_file.pdf",
56                   );
57
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);
62
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'));
67         $col_count++;
68     }
69     else {
70         $llx = $template->get_attr('left_margin');
71         if ($row_count == $template->get_attr('rows')) {
72             $pdf->Page();
73             $lly = ($template->get_attr('page_height') - $template->get_attr('top_margin') - $template->get_attr('label_height'));
74             $row_count = 1;
75         }
76         else {
77             $lly = ($lly - $template->get_attr('row_gap') - $template->get_attr('label_height'));
78             $row_count++;
79         }
80         $col_count = 1;
81     }
82     return ($row_count, $col_count, $llx, $lly);
83 }
84
85 sub _print_text {
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'} );
91     }
92 }
93
94 $| = 1;
95
96 # set the paper size
97 my $lowerLeftX  = 0;
98 my $lowerLeftY  = 0;
99 my $upperRightX = $template->get_attr('page_width');
100 my $upperRightY = $template->get_attr('page_height');
101
102 $pdf->Compress(1);
103 $pdf->Mbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
104
105 my ($row_count, $col_count, $llx, $lly) = $template->get_label_position($start_label);
106
107 if (@label_ids) {
108     my $batch_items = $batch->get_attr('items');
109     grep {
110         my $label_id = $_;
111         push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
112     } @label_ids;
113 }
114 elsif (@item_numbers) {
115     grep {
116         push(@{$items}, {item_number => $_});
117     } @item_numbers;
118 }
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 };
123     }
124 }
125 else {
126     $items = $batch->get_attr('items');
127 }
128
129 LABEL_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'},
136             llx              => $llx,
137             lly              => $lly,
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')
156             ),
157         );
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'},
165             llx              => $llx,
166             lly              => $lly,
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')
185             ),
186         );
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);
190         next LABEL_ITEMS;
191     }
192     else {
193     }
194         my $label = C4::Labels::Label->new(
195             batch_id         => $batch_id,
196             item_number      => $item->{'item_number'},
197             llx              => $llx,
198             lly              => $lly,
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')
217             ),
218         );
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);
224         next LABEL_ITEMS;
225 }
226
227 $pdf->End();
228
229 __END__
230
231 =head1 NAME
232
233 labels/label-create-pdf.pl - A script for creating a pdf export of labels and label batches in Koha
234
235 =head1 ABSTRACT
236
237 This script provides the means of producing a pdf of labels for items either individually, in groups, or in batches from within Koha.
238
239 =head1 USAGE
240
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:
243
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.
250
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.
252
253     example:
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
255
256 =head1 AUTHOR
257
258 Chris Nighswonger <cnighswonger AT foundations DOT edu>
259
260 =head1 COPYRIGHT
261
262 Copyright 2009 Foundations Bible College.
263
264 =head1 LICENSE
265
266 This file is part of Koha.
267
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.
272
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.
277
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>.
280
281 =head1 DISCLAIMER OF WARRANTY
282
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.
285
286 =cut