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