MT2527 : Adds default empty value for branches when batch modifying items
[koha.git] / labels / label-create-pdf.pl
blob607e873a867787f6ad1045333c1af3a858bc0df9
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 use CGI;
8 use C4::Debug;
9 use C4::Creators 1.000000;
10 use C4::Labels 1.000000;
12 my $cgi = new CGI;
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');
21 my $items = undef;
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",
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);
34 sub _calc_next_label_pos {
35 my ($row_count, $col_count, $llx, $lly) = @_;
36 if ($col_count lt $template->get_attr('cols')) {
37 $llx = ($llx + $template->get_attr('label_width') + $template->get_attr('col_gap'));
38 $col_count++;
40 else {
41 $llx = $template->get_attr('left_margin');
42 if ($row_count eq $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;
47 else {
48 $lly = ($lly - $template->get_attr('row_gap') - $template->get_attr('label_height'));
49 $row_count++;
51 $col_count = 1;
53 return ($row_count, $col_count, $llx, $lly);
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);
65 $| = 1;
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');
73 $pdf->Compress(1);
74 $pdf->Mbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
76 my ($row_count, $col_count, $llx, $lly) = $template->get_label_position($start_label);
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;
85 elsif (@item_numbers) {
86 grep {
87 push(@{$items}, {item_number => $_});
88 } @item_numbers;
90 else {
91 $items = $batch->get_attr('items');
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')),
117 my $label_a_text = $label_a->create_label();
118 _print_text($label_a_text);
119 ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
120 my $label_b = C4::Labels::Label->new(
121 batch_id => $batch_id,
122 item_number => $item->{'item_number'},
123 llx => $llx,
124 lly => $lly,
125 width => $template->get_attr('label_width'),
126 height => $template->get_attr('label_height'),
127 top_text_margin => $template->get_attr('top_text_margin'),
128 left_text_margin => $template->get_attr('left_text_margin'),
129 barcode_type => $layout->get_attr('barcode_type'),
130 printing_type => 'BAR',
131 guidebox => $layout->get_attr('guidebox'),
132 font => $layout->get_attr('font'),
133 font_size => $layout->get_attr('font_size'),
134 callnum_split => $layout->get_attr('callnum_split'),
135 justify => $layout->get_attr('text_justify'),
136 format_string => $layout->get_attr('format_string'),
137 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 my $label_b_text = $label_b->create_label();
140 ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
141 next LABEL_ITEMS;
143 else {
145 my $label = C4::Labels::Label->new(
146 batch_id => $batch_id,
147 item_number => $item->{'item_number'},
148 llx => $llx,
149 lly => $lly,
150 width => $template->get_attr('label_width'),
151 height => $template->get_attr('label_height'),
152 top_text_margin => $template->get_attr('top_text_margin'),
153 left_text_margin => $template->get_attr('left_text_margin'),
154 barcode_type => $layout->get_attr('barcode_type'),
155 printing_type => $layout->get_attr('printing_type'),
156 guidebox => $layout->get_attr('guidebox'),
157 font => $layout->get_attr('font'),
158 font_size => $layout->get_attr('font_size'),
159 callnum_split => $layout->get_attr('callnum_split'),
160 justify => $layout->get_attr('text_justify'),
161 format_string => $layout->get_attr('format_string'),
162 text_wrap_cols => $layout->get_text_wrap_cols(label_width => $template->get_attr('label_width'), left_text_margin => $template->get_attr('left_text_margin')),
164 my $label_text = $label->create_label();
165 _print_text($label_text) if $label_text;
166 ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
167 next LABEL_ITEMS;
170 $pdf->End();
172 exit(1);
174 =head1 NAME
176 labels/label-create-pdf.pl - A script for creating a pdf export of labels and label batches in Koha
178 =head1 ABSTRACT
180 This script provides the means of producing a pdf of labels for items either individually, in groups, or in batches from within Koha.
182 =head1 USAGE
184 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
185 parameters and two "multiple" parameters as follows:
187 C<batch_id> A single valid batch id to export.
188 C<template_id> A single valid template id to be applied to the current export. This parameter is manditory.
189 C<layout_id> A single valid layout id to be applied to the current export. This parameter is manditory.
190 C<start_label> The number of the label on which to begin the export. This parameter is optional.
191 C<lable_ids> A single valid label id to export. Multiple label ids may be submitted to export multiple labels.
192 C<item_numbers> A single valid item number to export. Multiple item numbers may be submitted to export multiple items.
194 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.
196 example:
197 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
199 =head1 AUTHOR
201 Chris Nighswonger <cnighswonger AT foundations DOT edu>
203 =head1 COPYRIGHT
205 Copyright 2009 Foundations Bible College.
207 =head1 LICENSE
209 This file is part of Koha.
211 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
212 Foundation; either version 2 of the License, or (at your option) any later version.
214 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,
215 Fifth Floor, Boston, MA 02110-1301 USA.
217 =head1 DISCLAIMER OF WARRANTY
219 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
220 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
222 =cut