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