rel_3_0 moved to HEAD
[koha.git] / barcodes / label-print-pdf.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 # $Id$
19
20 =head1 label-print-pdf.pl
21
22  this script is really divided into 2 differenvt section,
23
24  the first section creates, and defines the new PDF file the barcodes
25  using PDF::Reuse::Barcode, then saves the file to disk.
26
27  the second section then opens the pdf file off disk, and places the spline label
28  text in the left-most column of the page. then save the file again.
29
30  the reason for this goofyness, it that i couldnt find a single perl package that handled both barcodes and decent text placement.
31
32 =cut
33
34 use strict;
35 use CGI;
36 use C4::Labels;
37 use C4::Auth;
38 use C4::Output;
39 use C4::Interface::CGI::Output;
40 use C4::Context;
41
42 use PDF::Reuse;
43 use PDF::Reuse::Barcode;
44 use POSIX;
45 use C4::Labels;
46 use Acme::Comment;
47
48
49 my $htdocs_path = C4::Context->config('intrahtdocs');
50 my $cgi         = new CGI;
51 my $spine_text  = "";
52
53 # get the printing settings
54 my $template     = GetActiveLabelTemplate();
55 my $conf_data    = get_label_options();
56 my @resultsloop  = get_label_items();
57 my $barcodetype  = $conf_data->{'barcodetype'};
58 my $printingtype = $conf_data->{'printingtype'};
59 my $guidebox     = $conf_data->{'guidebox'};
60 my $start_label  = $conf_data->{'startlabel'};
61 my $fontsize     = $template->{'fontsize'};
62 my $units        = $template->{'units'};
63
64 warn "UNITS $units";
65 warn "fontsize = $fontsize";
66
67 my $unitvalue = GetUnitsValue($units);
68 warn $unitvalue;
69 warn $units;
70
71 my $tmpl_code = $template->{'tmpl_code'};
72 my $tmpl_desc = $template->{'tmpl_desc'};
73
74 my $page_height  = ( $template->{'page_height'} * $unitvalue );
75 my $page_width   = ( $template->{'page_width'} * $unitvalue );
76 my $label_height = ( $template->{'label_height'} * $unitvalue );
77 my $label_width  = ( $template->{'label_width'} * $unitvalue );
78 my $spine_width  = ( $template->{'label_width'} * $unitvalue );
79 my $circ_width   = ( $template->{'label_width'} * $unitvalue );
80 my $top_margin   = ( $template->{'topmargin'} * $unitvalue );
81 my $left_margin  = ( $template->{'leftmargin'} * $unitvalue );
82 my $colspace     = ( $template->{'colgap'} * $unitvalue );
83 my $rowspace     = ( $template->{'rowgap'} * $unitvalue );
84
85 my $label_cols = $template->{'cols'};
86 my $label_rows = $template->{'rows'};
87
88 my $text_wrap_cols = GetTextWrapCols( $fontsize, $label_width );
89
90 warn $label_cols, $label_rows;
91
92 # set the paper size
93 my $lowerLeftX  = 0;
94 my $lowerLeftY  = 0;
95 my $upperRightX = $page_width;
96 my $upperRightY = $page_height;
97
98 prInitVars();
99 $| = 1;
100 print STDOUT "Content-Type: application/pdf \r\n\r\n";
101 prFile();
102
103 prMbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
104
105 # later feature, change the font-type and size?
106 prFont('C');    # Just setting a font
107 prFontSize($fontsize);
108
109 my $margin           = $top_margin;
110 my $left_text_margin = 3;
111
112 my $str;
113
114 #warn "STARTROW = $startrow\n";
115
116 #my $page_break_count = $startrow;
117 my $codetype = 'Code39';
118
119 #do page border
120 #drawbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
121
122 my $item;
123 my ( $i, $i2 );    # loop counters
124
125 # big row loop
126
127 warn " $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY";
128 warn "$label_rows, $label_cols\n";
129 warn "$label_height, $label_width\n";
130 warn "$page_height, $page_width\n";
131
132 my ( $rowcount, $colcount, $x_pos, $y_pos, $rowtemp, $coltemp );
133
134 if ( $start_label eq 1 ) {
135     $rowcount = 1;
136     $colcount = 1;
137     $x_pos    = $left_margin;
138     $y_pos    = ( $page_height - $top_margin - $label_height );
139 }
140
141 else {
142     $rowcount = ceil( $start_label / $label_cols );
143     $colcount = ( $start_label - ( ( $rowcount - 1 ) * $label_cols ) );
144
145     $x_pos = $left_margin + ( $label_width * ( $colcount - 1 ) ) +
146       ( $colspace * ( $colcount - 1 ) );
147
148     $y_pos = $page_height - $top_margin - ( $label_height * $rowcount ) -
149       ( $rowspace * ( $rowcount - 1 ) );
150
151 }
152
153 warn "ROW COL $rowcount, $colcount";
154
155 #my $barcodetype = 'Code39';
156
157 foreach $item (@resultsloop) {
158
159     warn "-----------------";
160     if ($guidebox) {
161         drawbox( $x_pos, $y_pos, $label_width, $label_height );
162     }
163
164     if ( $printingtype eq 'spine' || $printingtype eq 'both' ) {
165         if ($guidebox) {
166             drawbox( $x_pos, $y_pos, $label_width, $label_height );
167         }
168
169         DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
170             $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
171         CalcNextLabelPos();
172     }
173
174     if ( $printingtype eq 'barcode' || $printingtype eq 'both' ) {
175         if ($guidebox) {
176             drawbox( $x_pos, $y_pos, $label_width, $label_height );
177         }
178
179         DrawBarcode( $x_pos, $y_pos, $label_height, $label_width,
180             $item->{'barcode'}, $barcodetype );
181         CalcNextLabelPos();
182     }
183
184 }    # end for item loop
185 prEnd();
186
187 print $cgi->redirect("/intranet-tmpl/barcodes/new.pdf");
188
189 sub CalcNextLabelPos {
190     if ( $colcount lt $label_cols ) {
191
192         #        warn "new col";
193         $x_pos = ( $x_pos + $label_width + $colspace );
194         $colcount++;
195     }
196
197     else {
198         $x_pos = $left_margin;
199         if ( $rowcount eq $label_rows ) {
200
201             #            warn "new page";
202             prPage();
203             $y_pos    = ( $page_height - $top_margin - $label_height );
204             $rowcount = 1;
205         }
206         else {
207
208             #            warn "new row";
209             $y_pos = ( $y_pos - $rowspace - $label_height );
210             $rowcount++;
211         }
212         $colcount = 1;
213     }
214 }
215