Merge remote-tracking branch 'kc/master' into new/bug_5683
[wip/koha-chris_n.git] / labels / label-create-pdf.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use CGI;
7
8 use C4::Debug;
9 use C4::Creators 1.000000;
10 use C4::Labels 1.000000;
11
12 my $cgi = new CGI;
13
14 my $batch_id    = $cgi->param('batch_id') if $cgi->param('batch_id');
15 my $template_id = $cgi->param('template_id') || undef;
16 my $layout_id   = $cgi->param('layout_id') || undef;
17 my $start_label = $cgi->param('start_label') || 1;
18 my @label_ids   = $cgi->param('label_id') if $cgi->param('label_id');
19 my @item_numbers  = $cgi->param('item_number') if $cgi->param('item_number');
20
21 my $items = undef;
22
23 my $pdf_file = (@label_ids || @item_numbers ? "label_single_" . scalar(@label_ids || @item_numbers) : "label_batch_$batch_id");
24 print $cgi->header( -type       => 'application/pdf',
25                     -encoding   => 'utf-8',
26                     -attachment => "$pdf_file.pdf",
27                   );
28
29 my $pdf = C4::Creators::PDF->new(InitVars => 0);
30 my $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
31 my $template = C4::Labels::Template->retrieve(template_id => $template_id, profile_id => 1);
32 my $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
33
34 sub _calc_next_label_pos {
35     my ($row_count, $col_count, $llx, $lly) = @_;
36     if ($col_count < $template->get_attr('cols')) {
37         $llx = ($llx + $template->get_attr('label_width') + $template->get_attr('col_gap'));
38         $col_count++;
39     }
40     else {
41         $llx = $template->get_attr('left_margin');
42         if ($row_count == $template->get_attr('rows')) {
43             $pdf->Page();
44             $lly = ($template->get_attr('page_height') - $template->get_attr('top_margin') - $template->get_attr('label_height'));
45             $row_count = 1;
46         }
47         else {
48             $lly = ($lly - $template->get_attr('row_gap') - $template->get_attr('label_height'));
49             $row_count++;
50         }
51         $col_count = 1;
52     }
53     return ($row_count, $col_count, $llx, $lly);
54 }
55
56 sub _print_text {
57     my $label_text = shift;
58     foreach my $text_line (@$label_text) {
59         my $pdf_font = $pdf->Font($text_line->{'font'});
60         my $line = "BT /$pdf_font $text_line->{'font_size'} Tf $text_line->{'text_llx'} $text_line->{'text_lly'} Td ($text_line->{'line'}) Tj ET";
61         $pdf->Add($line);
62     }
63 }
64
65 $| = 1;
66
67 # set the paper size
68 my $lowerLeftX  = 0;
69 my $lowerLeftY  = 0;
70 my $upperRightX = $template->get_attr('page_width');
71 my $upperRightY = $template->get_attr('page_height');
72
73 $pdf->Compress(1);
74 $pdf->Mbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
75
76 my ($row_count, $col_count, $llx, $lly) = $template->get_label_position($start_label);
77
78 if (@label_ids) {
79     my $batch_items = $batch->get_attr('items');
80     grep {
81         my $label_id = $_;
82         push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
83     } @label_ids;
84 }
85 elsif (@item_numbers) {
86     grep {
87         push(@{$items}, {item_number => $_});
88     } @item_numbers;
89 }
90 else {
91     $items = $batch->get_attr('items');
92 }
93
94 LABEL_ITEMS:
95 foreach my $item (@{$items}) {
96     my ($barcode_llx, $barcode_lly, $barcode_width, $barcode_y_scale_factor) = 0,0,0,0;
97     if ($layout->get_attr('printing_type') eq 'ALT') {  # we process the ALT style printing type here because it is not an atomic printing type
98         my $label_a = C4::Labels::Label->new(
99                                         batch_id            => $batch_id,
100                                         item_number         => $item->{'item_number'},
101                                         llx                 => $llx,
102                                         lly                 => $lly,
103                                         width               => $template->get_attr('label_width'),
104                                         height              => $template->get_attr('label_height'),
105                                         top_text_margin     => $template->get_attr('top_text_margin'),
106                                         left_text_margin    => $template->get_attr('left_text_margin'),
107                                         barcode_type        => $layout->get_attr('barcode_type'),
108                                         printing_type       => 'BIB',
109                                         guidebox            => $layout->get_attr('guidebox'),
110                                         font                => $layout->get_attr('font'),
111                                         font_size           => $layout->get_attr('font_size'),
112                                         callnum_split       => $layout->get_attr('callnum_split'),
113                                         justify             => $layout->get_attr('text_justify'),
114                                         format_string       => $layout->get_attr('format_string'),
115                                         text_wrap_cols      => $layout->get_text_wrap_cols(label_width => $template->get_attr('label_width'), left_text_margin => $template->get_attr('left_text_margin')),
116                                           );
117         $pdf->Add($label_a->draw_guide_box) if $layout->get_attr('guidebox');
118         my $label_a_text = $label_a->create_label();
119         _print_text($label_a_text);
120         ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
121         my $label_b = C4::Labels::Label->new(
122                                         batch_id            => $batch_id,
123                                         item_number         => $item->{'item_number'},
124                                         llx                 => $llx,
125                                         lly                 => $lly,
126                                         width               => $template->get_attr('label_width'),
127                                         height              => $template->get_attr('label_height'),
128                                         top_text_margin     => $template->get_attr('top_text_margin'),
129                                         left_text_margin    => $template->get_attr('left_text_margin'),
130                                         barcode_type        => $layout->get_attr('barcode_type'),
131                                         printing_type       => 'BAR',
132                                         guidebox            => $layout->get_attr('guidebox'),
133                                         font                => $layout->get_attr('font'),
134                                         font_size           => $layout->get_attr('font_size'),
135                                         callnum_split       => $layout->get_attr('callnum_split'),
136                                         justify             => $layout->get_attr('text_justify'),
137                                         format_string       => $layout->get_attr('format_string'),
138                                         text_wrap_cols      => $layout->get_text_wrap_cols(label_width => $template->get_attr('label_width'), left_text_margin => $template->get_attr('left_text_margin')),
139                                           );
140         $pdf->Add($label_b->draw_guide_box) if $layout->get_attr('guidebox');
141         my $label_b_text = $label_b->create_label();
142         ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
143         next LABEL_ITEMS;
144     }
145     else {
146     }
147         my $label = C4::Labels::Label->new(
148                                         batch_id            => $batch_id,
149                                         item_number         => $item->{'item_number'},
150                                         llx                 => $llx,
151                                         lly                 => $lly,
152                                         width               => $template->get_attr('label_width'),
153                                         height              => $template->get_attr('label_height'),
154                                         top_text_margin     => $template->get_attr('top_text_margin'),
155                                         left_text_margin    => $template->get_attr('left_text_margin'),
156                                         barcode_type        => $layout->get_attr('barcode_type'),
157                                         printing_type       => $layout->get_attr('printing_type'),
158                                         guidebox            => $layout->get_attr('guidebox'),
159                                         font                => $layout->get_attr('font'),
160                                         font_size           => $layout->get_attr('font_size'),
161                                         callnum_split       => $layout->get_attr('callnum_split'),
162                                         justify             => $layout->get_attr('text_justify'),
163                                         format_string       => $layout->get_attr('format_string'),
164                                         text_wrap_cols      => $layout->get_text_wrap_cols(label_width => $template->get_attr('label_width'), left_text_margin => $template->get_attr('left_text_margin')),
165                                           );
166         $pdf->Add($label->draw_guide_box) if $layout->get_attr('guidebox');
167         my $label_text = $label->create_label();
168         _print_text($label_text) if $label_text;
169         ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
170         next LABEL_ITEMS;
171 }
172
173 $pdf->End();
174
175 exit(1);
176
177 =head1 NAME
178
179 labels/label-create-pdf.pl - A script for creating a pdf export of labels and label batches in Koha
180
181 =head1 ABSTRACT
182
183 This script provides the means of producing a pdf of labels for items either individually, in groups, or in batches from within Koha.
184
185 =head1 USAGE
186
187 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
188 parameters and two "multiple" parameters as follows:
189
190     C<batch_id>         A single valid batch id to export.
191     C<template_id>      A single valid template id to be applied to the current export. This parameter is manditory.
192     C<layout_id>        A single valid layout id to be applied to the current export. This parameter is manditory.
193     C<start_label>      The number of the label on which to begin the export. This parameter is optional.
194     C<lable_ids>        A single valid label id to export. Multiple label ids may be submitted to export multiple labels.
195     C<item_numbers>     A single valid item number to export. Multiple item numbers may be submitted to export multiple items.
196
197 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.
198
199     example:
200         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
201
202 =head1 AUTHOR
203
204 Chris Nighswonger <cnighswonger AT foundations DOT edu>
205
206 =head1 COPYRIGHT
207
208 Copyright 2009 Foundations Bible College.
209
210 =head1 LICENSE
211
212 This file is part of Koha.
213
214 Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
215 Foundation; either version 2 of the License, or (at your option) any later version.
216
217 You should have received a copy of the GNU General Public License along with Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
218 Fifth Floor, Boston, MA 02110-1301 USA.
219
220 =head1 DISCLAIMER OF WARRANTY
221
222 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
223 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
224
225 =cut