Merge commit 'pianohacker-koha/prefs-submit' into master
[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::Labels::Batch 1.000000;
10 use C4::Labels::Template 1.000000;
11 use C4::Labels::Layout 1.000000;
12 use C4::Creators::PDF 1.000000;
13 use C4::Labels::Label 1.000000;
14
15 my $cgi = new CGI;
16
17 my $batch_id    = $cgi->param('batch_id') if $cgi->param('batch_id');
18 my $template_id = $cgi->param('template_id') || undef;
19 my $layout_id   = $cgi->param('layout_id') || undef;
20 my $start_label = $cgi->param('start_label') || 1;
21 my @label_ids   = $cgi->param('label_id') if $cgi->param('label_id');
22 my @item_numbers  = $cgi->param('item_number') if $cgi->param('item_number');
23
24 my $items = undef;
25
26 my $pdf_file = (@label_ids || @item_numbers ? "label_single_" . scalar(@label_ids || @item_numbers) : "label_batch_$batch_id");
27 print $cgi->header( -type       => 'application/pdf',
28                     -encoding   => 'utf-8',
29                     -attachment => "$pdf_file.pdf",
30                   );
31
32 my $pdf = C4::Creators::PDF->new(InitVars => 0);
33 my $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
34 my $template = C4::Labels::Template->retrieve(template_id => $template_id, profile_id => 1);
35 my $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
36
37 sub _calc_next_label_pos {
38     my ($row_count, $col_count, $llx, $lly) = @_;
39     if ($col_count lt $template->get_attr('cols')) {
40         $llx = ($llx + $template->get_attr('label_width') + $template->get_attr('col_gap'));
41         $col_count++;
42     }
43     else {
44         $llx = $template->get_attr('left_margin');
45         if ($row_count eq $template->get_attr('rows')) {
46             $pdf->Page();
47             $lly = ($template->get_attr('page_height') - $template->get_attr('top_margin') - $template->get_attr('label_height'));
48             $row_count = 1;
49         }
50         else {
51             $lly = ($lly - $template->get_attr('row_gap') - $template->get_attr('label_height'));
52             $row_count++;
53         }
54         $col_count = 1;
55     }
56     return ($row_count, $col_count, $llx, $lly);
57 }
58
59 sub _print_text {
60     my $label_text = shift;
61     foreach my $text_line (@$label_text) {
62         my $pdf_font = $pdf->Font($text_line->{'font'});
63         my $line = "BT /$pdf_font $text_line->{'font_size'} Tf $text_line->{'text_llx'} $text_line->{'text_lly'} Td ($text_line->{'line'}) Tj ET";
64         $pdf->Add($line);
65     }
66 }
67
68 $| = 1;
69
70 # set the paper size
71 my $lowerLeftX  = 0;
72 my $lowerLeftY  = 0;
73 my $upperRightX = $template->get_attr('page_width');
74 my $upperRightY = $template->get_attr('page_height');
75
76 $pdf->Compress(1);
77 $pdf->Mbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
78
79 my ($row_count, $col_count, $llx, $lly) = $template->get_label_position($start_label);
80
81 if (@label_ids) {
82     my $batch_items = $batch->get_attr('items');
83     grep {
84         my $label_id = $_;
85         push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
86     } @label_ids;
87 }
88 elsif (@item_numbers) {
89     grep {
90         push(@{$items}, {item_number => $_});
91     } @item_numbers;
92 }
93 else {
94     $items = $batch->get_attr('items');
95 }
96
97 LABEL_ITEMS:
98 foreach my $item (@{$items}) {
99     my ($barcode_llx, $barcode_lly, $barcode_width, $barcode_y_scale_factor) = 0,0,0,0;
100     if ($layout->get_attr('printing_type') eq 'ALT') {  # we process the ALT style printing type here because it is not an atomic printing type 
101         my $label_a = C4::Labels::Label->new(
102                                         batch_id            => $batch_id,
103                                         item_number         => $item->{'item_number'},
104                                         llx                 => $llx,
105                                         lly                 => $lly,
106                                         width               => $template->get_attr('label_width'),
107                                         height              => $template->get_attr('label_height'),
108                                         top_text_margin     => $template->get_attr('top_text_margin'),
109                                         left_text_margin    => $template->get_attr('left_text_margin'),
110                                         barcode_type        => $layout->get_attr('barcode_type'),
111                                         printing_type       => 'BIB',
112                                         guidebox            => $layout->get_attr('guidebox'),
113                                         font                => $layout->get_attr('font'),
114                                         font_size           => $layout->get_attr('font_size'),
115                                         callnum_split       => $layout->get_attr('callnum_split'),
116                                         justify             => $layout->get_attr('text_justify'),
117                                         format_string       => $layout->get_attr('format_string'),
118                                         text_wrap_cols      => $layout->get_text_wrap_cols(label_width => $template->get_attr('label_width'), left_text_margin => $template->get_attr('left_text_margin')),
119                                           );
120         my $label_a_text = $label_a->create_label();
121         _print_text($label_a_text);
122         ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
123         my $label_b = C4::Labels::Label->new(
124                                         batch_id            => $batch_id,
125                                         item_number         => $item->{'item_number'},
126                                         llx                 => $llx,
127                                         lly                 => $lly,
128                                         width               => $template->get_attr('label_width'),
129                                         height              => $template->get_attr('label_height'),
130                                         top_text_margin     => $template->get_attr('top_text_margin'),
131                                         left_text_margin    => $template->get_attr('left_text_margin'),
132                                         barcode_type        => $layout->get_attr('barcode_type'),
133                                         printing_type       => 'BAR',
134                                         guidebox            => $layout->get_attr('guidebox'),
135                                         font                => $layout->get_attr('font'),
136                                         font_size           => $layout->get_attr('font_size'),
137                                         callnum_split       => $layout->get_attr('callnum_split'),
138                                         justify             => $layout->get_attr('text_justify'),
139                                         format_string       => $layout->get_attr('format_string'),
140                                         text_wrap_cols      => $layout->get_text_wrap_cols(label_width => $template->get_attr('label_width'), left_text_margin => $template->get_attr('left_text_margin')),
141                                           );
142         my $label_b_text = $label_b->create_label();
143         ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
144         next LABEL_ITEMS;
145     }
146     else {
147     }
148         my $label = C4::Labels::Label->new(
149                                         batch_id            => $batch_id,
150                                         item_number         => $item->{'item_number'},
151                                         llx                 => $llx,
152                                         lly                 => $lly,
153                                         width               => $template->get_attr('label_width'),
154                                         height              => $template->get_attr('label_height'),
155                                         top_text_margin     => $template->get_attr('top_text_margin'),
156                                         left_text_margin    => $template->get_attr('left_text_margin'),
157                                         barcode_type        => $layout->get_attr('barcode_type'),
158                                         printing_type       => $layout->get_attr('printing_type'),
159                                         guidebox            => $layout->get_attr('guidebox'),
160                                         font                => $layout->get_attr('font'),
161                                         font_size           => $layout->get_attr('font_size'),
162                                         callnum_split       => $layout->get_attr('callnum_split'),
163                                         justify             => $layout->get_attr('text_justify'),
164                                         format_string       => $layout->get_attr('format_string'),
165                                         text_wrap_cols      => $layout->get_text_wrap_cols(label_width => $template->get_attr('label_width'), left_text_margin => $template->get_attr('left_text_margin')),
166                                           );
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., 59 Temple Place,
218 Suite 330, Boston, MA  02111-1307 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