Install koha through the CPAN module
[koha.git] / labels / label-print-pdf.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use CGI;
5 use C4::Labels;
6 use C4::Auth;
7 use C4::Output;
8 use C4::Context;
9 use HTML::Template;
10 use PDF::Reuse;
11 use PDF::Reuse::Barcode;
12 use POSIX;
13 #use C4::Labels;
14 #use Smart::Comments;
15
16 my $htdocs_path = C4::Context->config('intrahtdocs');
17 my $cgi         = new CGI;
18 print $cgi->header( -type => 'application/pdf', -attachment => 'barcode.pdf' );
19
20 my $spine_text = "";
21
22 warn "label-print-pdf ***";
23
24 # get the printing settings
25 my $template    = GetActiveLabelTemplate();
26 my $conf_data   = get_label_options();
27
28 my $batch_id =   $cgi->param('batch_id');
29 my @resultsloop = get_label_items($batch_id);
30
31 #$DB::single = 1;
32
33 my $barcodetype  = $conf_data->{'barcodetype'};
34 my $printingtype = $conf_data->{'printingtype'};
35 my $guidebox     = $conf_data->{'guidebox'};
36 my $start_label  = $conf_data->{'startlabel'};
37 my $fontsize     = $template->{'fontsize'};
38 my $units        = $template->{'units'};
39
40 ### $printingtype;
41
42 =c
43 ################### defaults for testing
44 my $barcodetype  = 'CODE39';
45 my $printingtype = 'BARBIB';
46 my $guidebox     = 1;
47 my $start_label  = 1;
48 my $units        = 'POINTS'
49 =cut
50
51 #my $fontsize = 3;
52
53 #warn "UNITS $units";
54 #warn "fontsize = $fontsize";
55 #warn Dumper $template;
56
57 my $unitvalue = GetUnitsValue($units);
58
59 my $tmpl_code = $template->{'tmpl_code'};
60 my $tmpl_desc = $template->{'tmpl_desc'};
61
62 my $page_height  = ( $template->{'page_height'} * $unitvalue );
63 my $page_width   = ( $template->{'page_width'} * $unitvalue );
64 my $label_height = ( $template->{'label_height'} * $unitvalue );
65 my $label_width  = ( $template->{'label_width'} * $unitvalue );
66 my $spine_width  = ( $template->{'label_width'} * $unitvalue );
67 my $circ_width   = ( $template->{'label_width'} * $unitvalue );
68 my $top_margin   = ( $template->{'topmargin'} * $unitvalue );
69 my $left_margin  = ( $template->{'leftmargin'} * $unitvalue );
70 my $colspace     = ( $template->{'colgap'} * $unitvalue );
71 my $rowspace     = ( $template->{'rowgap'} * $unitvalue );
72
73 my $label_cols = $template->{'cols'};
74 my $label_rows = $template->{'rows'};
75
76 my $text_wrap_cols = GetTextWrapCols( $fontsize, $label_width );
77
78 #warn $label_cols, $label_rows;
79
80 # set the paper size
81 my $lowerLeftX  = 0;
82 my $lowerLeftY  = 0;
83 my $upperRightX = $page_width;
84 my $upperRightY = $page_height;
85
86 prInitVars();
87 $| = 1;
88 prFile();
89
90 prMbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
91
92 # later feature, change the font-type and size?
93 prFont('C');    # Just setting a font
94 prFontSize($fontsize);
95
96 my $margin           = $top_margin;
97 my $left_text_margin = 3;
98 my $str;
99
100 #warn "STARTROW = $startrow\n";
101
102 #my $page_break_count = $startrow;
103 my $codetype; # = 'Code39';
104
105 #do page border
106 #drawbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
107
108 my $item;
109 my ( $i, $i2 );    # loop counters
110
111 # big row loop
112
113 #warn " $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY";
114 #warn "$label_rows, $label_cols\n";
115 #warn "$label_height, $label_width\n";
116 #warn "$page_height, $page_width\n";
117
118 my ( $rowcount, $colcount, $x_pos, $y_pos, $rowtemp, $coltemp );
119
120 if ( $start_label eq 1 ) {
121     $rowcount = 1;
122     $colcount = 1;
123     $x_pos    = $left_margin;
124     $y_pos    = ( $page_height - $top_margin - $label_height );
125 }
126
127 else {
128
129     #eval {
130     $rowcount = ceil( $start_label / $label_cols );
131
132     #} ;
133     #$rowcount = 1 if $@;
134
135     $colcount = ( $start_label - ( ( $rowcount - 1 ) * $label_cols ) );
136
137     $x_pos = $left_margin + ( $label_width * ( $colcount - 1 ) ) +
138       ( $colspace * ( $colcount - 1 ) );
139
140     $y_pos = $page_height - $top_margin - ( $label_height * $rowcount ) -
141       ( $rowspace * ( $rowcount - 1 ) );
142
143 }
144
145 warn "ROW COL $rowcount, $colcount";
146
147 #my $barcodetype; # = 'Code39';
148
149 #
150 #    main foreach loop
151 #
152
153 foreach $item (@resultsloop) {
154 #    warn "$x_pos, $y_pos, $label_width, $label_height";
155     my $barcode = $item->{'barcode'};
156     if ( $printingtype eq 'BAR' ) {
157         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
158         DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $barcode,
159             $barcodetype );
160         CalcNextLabelPos();
161     }
162     elsif ( $printingtype eq 'BARBIB' ) {
163         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
164
165         # reposoitioning barcode up the top of label
166         my $barcode_height = ($label_height / 1.5 );    ## scaling voodoo
167         my $text_height    = $label_height / 2;
168         my $barcode_y      = $y_pos + ( $label_height / 2.5  );   ## scaling voodoo
169
170         DrawBarcode( $x_pos, $barcode_y, $barcode_height, $label_width,
171             $barcode, $barcodetype );
172         DrawSpineText( $y_pos, $text_height, $fontsize, $x_pos,
173             $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
174
175         CalcNextLabelPos();
176
177     }    # correct
178     elsif ( $printingtype eq 'BIBBAR' ) {
179         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
180         my $barcode_height = $label_height / 2;
181         DrawBarcode( $x_pos, $y_pos, $barcode_height, $label_width, $barcode,
182             $barcodetype );
183         DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
184             $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
185
186         CalcNextLabelPos();
187     }
188
189     elsif ( $printingtype eq 'ALT' ) {
190         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
191         DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $barcode,
192             $barcodetype );
193         CalcNextLabelPos();
194         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
195         DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
196             $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
197
198         CalcNextLabelPos();
199     }
200
201
202     elsif ( $printingtype eq 'BIB' ) {
203         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
204         DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
205             $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
206         CalcNextLabelPos();
207     }
208
209
210
211
212
213
214
215
216
217
218
219 }    # end for item loop
220 prEnd();
221
222 #
223 #
224 #
225 #
226 #
227 sub CalcNextLabelPos {
228     if ( $colcount lt $label_cols ) {
229
230         #        warn "new col";
231         $x_pos = ( $x_pos + $label_width + $colspace );
232         $colcount++;
233     }
234
235     else {
236         $x_pos = $left_margin;
237         if ( $rowcount eq $label_rows ) {
238
239             #            warn "new page";
240             prPage();
241             $y_pos    = ( $page_height - $top_margin - $label_height );
242             $rowcount = 1;
243         }
244         else {
245
246             #            warn "new row";
247             $y_pos = ( $y_pos - $rowspace - $label_height );
248             $rowcount++;
249         }
250         $colcount = 1;
251     }
252 }
253