[23/40] Initial work on label export interface.
[koha.git] / labels / label-create-pdf.pl
1 #!/usr/bin/perl
2
3 # Copyright 2006 Katipo Communications.
4 # Some parts Copyright 2009 Foundations Bible College.
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use warnings;
23
24 use CGI;
25 use HTML::Template::Pro;
26 use POSIX qw(ceil);
27 use Data::Dumper;
28 use Sys::Syslog qw(syslog);
29
30 use C4::Labels;
31 use C4::Auth;
32 use C4::Output;
33 use C4::Context;
34 use C4::Members;
35 use C4::Branch;
36 use C4::Debug;
37 use C4::Labels::Batch 1.000000;
38 use C4::Labels::Template 1.000000;
39 use C4::Labels::Layout 1.000000;
40 use C4::Labels::PDF 1.000000;
41 use C4::Labels::Label 1.000000;
42
43 my $cgi = new CGI;
44
45 my $htdocs_path = C4::Context->config('intrahtdocs');
46 my $batch_id    = $cgi->param('batch_id') || $ARGV[0];
47 my $template_id = $cgi->param('template_id') || $ARGV[1];
48 my $layout_id   = $cgi->param('layout_id') || $ARGV[2];
49 my $start_label = $cgi->param('start_label') || $ARGV[3];
50
51 print $cgi->header( -type => 'application/pdf', -attachment => "label_batch_$batch_id.pdf" );
52
53 my $pdf = C4::Labels::PDF->new(InitVars => 0);
54 my $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
55 my $template = C4::Labels::Template->retrieve(template_id => $template_id, profile_id => 1);
56 my $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
57
58 sub _calc_next_label_pos {
59     my ($row_count, $col_count, $llx, $lly) = @_;
60     if ($col_count lt $template->get_attr('cols')) {
61         $llx = ($llx + $template->get_attr('label_width') + $template->get_attr('col_gap'));
62         $col_count++;
63     }
64     else {
65         $llx = $template->get_attr('left_margin');
66         if ($row_count eq $template->get_attr('rows')) {
67             $pdf->Page();
68             $lly = ($template->get_attr('page_height') - $template->get_attr('top_margin') - $template->get_attr('label_height'));
69             $row_count = 1;
70         }
71         else {
72             $lly = ($lly - $template->get_attr('row_gap') - $template->get_attr('label_height'));
73             $row_count++;
74         }
75         $col_count = 1;
76     }
77     return ($row_count, $col_count, $llx, $lly);
78 }
79
80 sub _print_text {
81     my $label_text = shift;
82     foreach my $text_line (@$label_text) {
83         my $pdf_font = $pdf->Font($text_line->{'font'});
84         my $line = "BT /$pdf_font $text_line->{'font_size'} Tf $text_line->{'text_llx'} $text_line->{'text_lly'} Td ($text_line->{'line'}) Tj ET";
85         $pdf->Add($line);
86     }
87 }
88
89 $| = 1;
90
91 # set the paper size
92 my $lowerLeftX  = 0;
93 my $lowerLeftY  = 0;
94 my $upperRightX = $template->get_attr('page_width');
95 my $upperRightY = $template->get_attr('page_height');
96
97 $pdf->Compress(1);
98 $pdf->Mbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
99
100 my ($row_count, $col_count, $llx, $lly) = $template->get_label_position($start_label);
101 LABEL_ITEMS:
102 foreach my $item (@{$batch->get_attr('items')}) {
103     my ($barcode_llx, $barcode_lly, $barcode_width, $barcode_y_scale_factor) = 0,0,0,0;
104     my $label = C4::Labels::Label->new(
105                                     batch_id            => $batch_id,
106                                     item_number         => $item->{'item_number'},
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       => $layout->get_attr('printing_type'),
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_type = $label->get_label_type;
122     if ($label_type eq 'BIB') {
123         my $line_spacer = ($label->get_attr('font_size') * 1);    # number of pixels between text rows (This is actually leading: baseline to baseline minus font size. Recommended starting point is 20% of font size.).
124         my $text_lly = ($lly + ($template->get_attr('label_height') - $template->get_attr('top_text_margin')));
125         my $label_text = $label->draw_label_text(
126                                         llx             => $llx,
127                                         lly             => $text_lly,
128                                         line_spacer     => $line_spacer,
129                                         );
130         _print_text($label_text);
131         ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
132         next LABEL_ITEMS;
133     }
134     elsif ($label_type eq 'BARBIB') {
135         $barcode_llx = $llx + $template->get_attr('left_text_margin');                             # this places the bottom left of the barcode the left text margin distance to right of the the left edge of the label ($llx)
136         $barcode_lly = ($lly + $template->get_attr('label_height')) - $template->get_attr('top_text_margin');        # this places the bottom left of the barcode the top text margin distance below the top of the label ($lly)
137         $barcode_width = 0.8 * $template->get_attr('label_width');                                 # this scales the barcode width to 80% of the label width
138         $barcode_y_scale_factor = 0.01 * $template->get_attr('label_height');                      # this scales the barcode height to 10% of the label height
139         my $line_spacer = ($label->get_attr('font_size') * 1);    # number of pixels between text rows (This is actually leading: baseline to baseline minus font size. Recommended starting point is 20% of font size.).
140         my $text_lly = ($lly + ($template->get_attr('label_height') - $template->get_attr('top_text_margin')));
141         my $label_text = $label->draw_label_text(
142                                         llx             => $llx,
143                                         lly             => $text_lly,
144                                         line_spacer     => $line_spacer,
145                                         );
146         _print_text($label_text);
147     }
148     else {
149         $barcode_llx = $llx + $template->get_attr('left_text_margin');             # this places the bottom left of the barcode the left text margin distance to right of the the left edge of the label ($llx)
150         $barcode_lly = $lly + $template->get_attr('top_text_margin');              # this places the bottom left of the barcode the top text margin distance above the bottom of the label ($lly)
151         $barcode_width = 0.8 * $template->get_attr('label_width');                 # this scales the barcode width to 80% of the label width
152         $barcode_y_scale_factor = 0.01 * $template->get_attr('label_height');      # this scales the barcode height to 10% of the label height
153         if ($label_type eq 'BIBBAR' || $label_type eq 'ALT') {
154             my $line_spacer = ($label->get_attr('font_size') * 1);    # number of pixels between text rows (This is actually leading: baseline to baseline minus font size. Recommended starting point is 20% of font size.).
155             my $text_lly = ($lly + ($template->get_attr('label_height') - $template->get_attr('top_text_margin')));
156             my $label_text = $label->draw_label_text(
157                                             llx             => $llx,
158                                             lly             => $text_lly,
159                                             line_spacer     => $line_spacer,
160                                             );
161             _print_text($label_text);
162         }
163         if ($label_type eq 'ALT') {
164         ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
165         }
166     }
167     $label->barcode(
168                 llx                 => $barcode_llx,
169                 lly                 => $barcode_lly,
170                 width               => $barcode_width,
171                 y_scale_factor      => $barcode_y_scale_factor,
172     );
173     ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
174 }
175
176 $pdf->End();