functions that were in C4::Interface::CGI::Output are now in C4::Output.
[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::Context;
40
41 use PDF::Reuse;
42 use PDF::Reuse::Barcode;
43 use POSIX;
44 use C4::Labels;
45 use Acme::Comment;
46
47
48 my $htdocs_path = C4::Context->config('intrahtdocs');
49 my $cgi         = new CGI;
50 my $spine_text  = "";
51
52 # get the printing settings
53 my $template     = GetActiveLabelTemplate();
54 my $conf_data    = get_label_options();
55 my @resultsloop  = get_label_items();
56 my $barcodetype  = $conf_data->{'barcodetype'};
57 my $printingtype = $conf_data->{'printingtype'};
58 my $guidebox     = $conf_data->{'guidebox'};
59 my $start_label  = $conf_data->{'startlabel'};
60 my $fontsize     = $template->{'fontsize'};
61 my $units        = $template->{'units'};
62
63 warn "UNITS $units";
64 warn "fontsize = $fontsize";
65
66 my $unitvalue = GetUnitsValue($units);
67 warn $unitvalue;
68 warn $units;
69
70 my $tmpl_code = $template->{'tmpl_code'};
71 my $tmpl_desc = $template->{'tmpl_desc'};
72
73 my $page_height  = ( $template->{'page_height'} * $unitvalue );
74 my $page_width   = ( $template->{'page_width'} * $unitvalue );
75 my $label_height = ( $template->{'label_height'} * $unitvalue );
76 my $label_width  = ( $template->{'label_width'} * $unitvalue );
77 my $spine_width  = ( $template->{'label_width'} * $unitvalue );
78 my $circ_width   = ( $template->{'label_width'} * $unitvalue );
79 my $top_margin   = ( $template->{'topmargin'} * $unitvalue );
80 my $left_margin  = ( $template->{'leftmargin'} * $unitvalue );
81 my $colspace     = ( $template->{'colgap'} * $unitvalue );
82 my $rowspace     = ( $template->{'rowgap'} * $unitvalue );
83
84 my $label_cols = $template->{'cols'};
85 my $label_rows = $template->{'rows'};
86
87 my $text_wrap_cols = GetTextWrapCols( $fontsize, $label_width );
88
89 warn $label_cols, $label_rows;
90
91 # set the paper size
92 my $lowerLeftX  = 0;
93 my $lowerLeftY  = 0;
94 my $upperRightX = $page_width;
95 my $upperRightY = $page_height;
96
97 prInitVars();
98 $| = 1;
99 print STDOUT "Content-Type: application/pdf \r\n\r\n";
100 prFile();
101
102 prMbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
103
104 # later feature, change the font-type and size?
105 prFont('C');    # Just setting a font
106 prFontSize($fontsize);
107
108 my $margin           = $top_margin;
109 my $left_text_margin = 3;
110
111 my $str;
112
113 #warn "STARTROW = $startrow\n";
114
115 #my $page_break_count = $startrow;
116 my $codetype = 'Code39';
117
118 #do page border
119 #drawbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
120
121 my $item;
122 my ( $i, $i2 );    # loop counters
123
124 # big row loop
125
126 warn " $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY";
127 warn "$label_rows, $label_cols\n";
128 warn "$label_height, $label_width\n";
129 warn "$page_height, $page_width\n";
130
131 my ( $rowcount, $colcount, $x_pos, $y_pos, $rowtemp, $coltemp );
132
133 if ( $start_label eq 1 ) {
134     $rowcount = 1;
135     $colcount = 1;
136     $x_pos    = $left_margin;
137     $y_pos    = ( $page_height - $top_margin - $label_height );
138 }
139
140 else {
141     $rowcount = ceil( $start_label / $label_cols );
142     $colcount = ( $start_label - ( ( $rowcount - 1 ) * $label_cols ) );
143
144     $x_pos = $left_margin + ( $label_width * ( $colcount - 1 ) ) +
145       ( $colspace * ( $colcount - 1 ) );
146
147     $y_pos = $page_height - $top_margin - ( $label_height * $rowcount ) -
148       ( $rowspace * ( $rowcount - 1 ) );
149
150 }
151
152 warn "ROW COL $rowcount, $colcount";
153
154 #my $barcodetype = 'Code39';
155
156 foreach $item (@resultsloop) {
157
158     warn "-----------------";
159     if ($guidebox) {
160         drawbox( $x_pos, $y_pos, $label_width, $label_height );
161     }
162
163     if ( $printingtype eq 'spine' || $printingtype eq 'both' ) {
164         if ($guidebox) {
165             drawbox( $x_pos, $y_pos, $label_width, $label_height );
166         }
167
168         DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
169             $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
170         CalcNextLabelPos();
171     }
172
173     if ( $printingtype eq 'barcode' || $printingtype eq 'both' ) {
174         if ($guidebox) {
175             drawbox( $x_pos, $y_pos, $label_width, $label_height );
176         }
177
178         DrawBarcode( $x_pos, $y_pos, $label_height, $label_width,
179             $item->{'barcode'}, $barcodetype );
180         CalcNextLabelPos();
181     }
182
183 }    # end for item loop
184 prEnd();
185
186 print $cgi->redirect("/intranet-tmpl/barcodes/new.pdf");
187
188 sub CalcNextLabelPos {
189     if ( $colcount lt $label_cols ) {
190
191         #        warn "new col";
192         $x_pos = ( $x_pos + $label_width + $colspace );
193         $colcount++;
194     }
195
196     else {
197         $x_pos = $left_margin;
198         if ( $rowcount eq $label_rows ) {
199
200             #            warn "new page";
201             prPage();
202             $y_pos    = ( $page_height - $top_margin - $label_height );
203             $rowcount = 1;
204         }
205         else {
206
207             #            warn "new row";
208             $y_pos = ( $y_pos - $rowspace - $label_height );
209             $rowcount++;
210         }
211         $colcount = 1;
212     }
213 }
214