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