Removing unused script
[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::Pro;
10 use PDF::Reuse;
11 use PDF::Reuse::Barcode;
12 use POSIX;
13 #use C4::Labels;
14 #use Smart::Comments;
15
16 my $DEBUG = 0;
17 my $DEBUG_LPT = 0;
18
19 my $htdocs_path = C4::Context->config('intrahtdocs');
20 my $cgi         = new CGI;
21 print $cgi->header( -type => 'application/pdf', -attachment => 'barcode.pdf' );
22
23 my $spine_text = "";
24
25 #warn "label-print-pdf ***";
26
27 # get the printing settings
28 my $template    = GetActiveLabelTemplate();
29 my $conf_data   = get_label_options();
30 my $profile = GetAssociatedProfile($template->{'tmpl_id'});
31
32 my $batch_id =   $cgi->param('batch_id');
33 my @resultsloop = get_label_items($batch_id);
34
35 #$DB::single = 1;
36
37 my $barcodetype  = $conf_data->{'barcodetype'};
38 my $printingtype = $conf_data->{'printingtype'};
39 my $guidebox     = $conf_data->{'guidebox'};
40 my $start_label  = $conf_data->{'startlabel'};
41 if ($cgi->param('startlabel')) {
42         $start_label = $cgi->param('startlabel');       # A bit of a hack to allow setting the starting label from the address bar... -fbcit
43     }
44 warn "Starting on label #$start_label" if $DEBUG;
45 my $fontsize     = $template->{'fontsize'};
46 my $units        = $template->{'units'};
47
48 ### $printingtype;
49
50 =c
51 ################### defaults for testing
52 my $barcodetype  = 'CODE39';
53 my $printingtype = 'BARBIB';
54 my $guidebox     = 1;
55 my $start_label  = 1;
56 my $units        = 'POINTS'
57 =cut
58
59 #my $fontsize = 3;
60
61 #warn "UNITS $units";
62 #warn "fontsize = $fontsize";
63 #warn Dumper $template;
64
65 my $unitvalue = GetUnitsValue($units);
66
67 warn "Template units: $units which converts to $unitvalue PostScript Points" if $DEBUG;
68
69 my $tmpl_code = $template->{'tmpl_code'};
70 my $tmpl_desc = $template->{'tmpl_desc'};
71
72 my $page_height  = ( $template->{'page_height'} * $unitvalue );
73 my $page_width   = ( $template->{'page_width'} * $unitvalue );
74 my $label_height = ( $template->{'label_height'} * $unitvalue );
75 my $label_width  = ( $template->{'label_width'} * $unitvalue );
76 my $spine_width  = ( $template->{'label_width'} * $unitvalue );
77 my $circ_width   = ( $template->{'label_width'} * $unitvalue );
78 my $top_margin   = ( $template->{'topmargin'} * $unitvalue );
79 my $left_margin  = ( $template->{'leftmargin'} * $unitvalue );
80 my $colspace     = ( $template->{'colgap'} * $unitvalue );
81 my $rowspace     = ( $template->{'rowgap'} * $unitvalue );
82
83 warn "Converted dimensions are:" if $DEBUG;
84 warn "pghth=$page_height, pgwth=$page_width, lblhth=$label_height, lblwth=$label_width, spinwth=$spine_width, circwth=$circ_width, tpmar=$top_margin, lmar=$left_margin, colsp=$colspace, rowsp=$rowspace" if $DEBUG;
85
86 my $label_cols = $template->{'cols'};
87 my $label_rows = $template->{'rows'};
88
89 my $text_wrap_cols = GetTextWrapCols( $fontsize, $label_width );
90
91
92 #warn $label_cols, $label_rows;
93
94 # set the paper size
95 my $lowerLeftX  = 0;
96 my $lowerLeftY  = 0;
97 my $upperRightX = $page_width;
98 my $upperRightY = $page_height;
99
100 prInitVars();
101 $| = 1;
102 prFile();
103
104 prMbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
105
106 # later feature, change the font-type and size?
107 prFont('C');    # Just setting a font
108 prFontSize($fontsize);
109
110 my $margin           = $top_margin;
111 my $left_text_margin = 3;
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 # draw margin box for alignment page
123 drawbox( ($left_margin), ($top_margin), ($page_width-(2*$left_margin)), ($page_height-(2*$top_margin)) ) if $DEBUG_LPT;
124
125 # Adjustments for image position and creep -fbcit
126 # NOTE: *All* of these factor in to image position and creep. Keep this in mind when makeing adjustments.
127 # Suggested proceedure: Adjust margins until both top and left margins are correct. Then adjust the label
128 # height and width to correct label creep across and down page. Units are PostScript Points (72 per inch).
129
130 warn "Active profile: " . ($profile->{'prof_id'}?$profile->{'prof_id'}:"None") if $DEBUG;
131
132 if ( $DEBUG ) {
133     warn "-------------------------INITIAL VALUES-----------------------------";
134     warn "top margin = $top_margin points\n";
135     warn "left margin = $left_margin points\n";
136     warn "label height = $label_height points\n";
137     warn "label width = $label_width points\n";
138 }
139
140 if ( $profile->{'prof_id'} ) {
141     $top_margin = $top_margin + $profile->{'offset_vert'};    #  controls vertical offset
142     $label_height = $label_height + $profile->{'creep_vert'};    # controls vertical creep
143     $left_margin = $left_margin + $profile->{'offset_horz'};    # controls horizontal offset
144     $label_width = $label_width + $profile->{'creep_horz'};    # controls horizontal creep
145 }
146
147 if ( $DEBUG && $profile->{'prof_id'} ) {
148     warn "-------------------------ADJUSTED VALUES-----------------------------";
149     warn "top margin = $top_margin points\n";
150     warn "left margin = $left_margin points\n";
151     warn "label height = $label_height points\n";
152     warn "label width = $label_width points\n";
153 } elsif ( $DEBUG ) {
154     warn "No profile associated so no adjustment applied.";
155 }
156
157 my $item;
158 my ( $i, $i2 );    # loop counters
159
160 # big row loop
161
162 #warn " $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY";
163 #warn "$label_rows, $label_cols\n";
164 #warn "$label_height, $label_width\n";
165 #warn "$page_height, $page_width\n";
166
167 my ( $rowcount, $colcount, $x_pos, $y_pos, $rowtemp, $coltemp );
168
169 if ( $start_label eq 1 ) {
170     $rowcount = 1;
171     $colcount = 1;
172     $x_pos    = $left_margin;
173     $y_pos    = ( $page_height - $top_margin - $label_height );
174 }
175
176 else {
177
178     #eval {
179     $rowcount = ceil( $start_label / $label_cols );
180
181     #} ;
182     #$rowcount = 1 if $@;
183
184     $colcount = ( $start_label - ( ( $rowcount - 1 ) * $label_cols ) );
185
186     $x_pos = $left_margin + ( $label_width * ( $colcount - 1 ) ) +
187       ( $colspace * ( $colcount - 1 ) );
188
189     $y_pos = $page_height - $top_margin - ( $label_height * $rowcount ) -
190       ( $rowspace * ( $rowcount - 1 ) );
191
192     warn "Start label specified: $start_label Beginning in row $rowcount, column $colcount" if $DEBUG;
193     warn "X position = $x_pos Y position = $y_pos" if $DEBUG;
194     warn "Rowspace = $rowspace Label height = $label_height" if $DEBUG;
195 }
196
197 #warn "ROW COL $rowcount, $colcount";
198
199 #my $barcodetype; # = 'Code39';
200
201 #
202 #    main foreach loop
203 #
204
205 foreach $item (@resultsloop) {
206     warn "Label parameters: xpos=$x_pos, ypos=$y_pos, lblwid=$label_width, lblhig=$label_height" if $DEBUG;
207     my $barcode = $item->{'barcode'};
208     if ( $printingtype eq 'BAR' ) {
209         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
210         DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $barcode,
211             $barcodetype );
212         CalcNextLabelPos();
213     }
214     elsif ( $printingtype eq 'BARBIB' ) {
215         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
216
217         # reposoitioning barcode up the top of label
218         my $barcode_height = ($label_height / 1.5 );    ## scaling voodoo
219         my $text_height    = $label_height / 2;
220         my $barcode_y      = $y_pos + ( $label_height / 2.5  );   ## scaling voodoo
221
222         DrawBarcode( $x_pos, $barcode_y, $barcode_height, $label_width,
223             $barcode, $barcodetype );
224         DrawSpineText( $y_pos, $text_height, $fontsize, $x_pos,
225             $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
226
227         CalcNextLabelPos();
228
229     }    # correct
230     elsif ( $printingtype eq 'BIBBAR' ) {
231         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
232         my $barcode_height = $label_height / 2;
233         DrawBarcode( $x_pos, $y_pos, $barcode_height, $label_width, $barcode,
234             $barcodetype );
235         DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
236             $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
237
238         CalcNextLabelPos();
239     }
240
241     elsif ( $printingtype eq 'ALT' ) {
242         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
243         DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $barcode,
244             $barcodetype );
245         CalcNextLabelPos();
246         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
247         DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
248             $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
249
250         CalcNextLabelPos();
251     }
252
253
254     elsif ( $printingtype eq 'BIB' ) {
255         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
256         DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
257             $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
258         CalcNextLabelPos();
259     }
260
261
262
263
264
265
266
267
268
269
270
271 }    # end for item loop
272 prEnd();
273
274 #
275 #
276 #
277 #
278 #
279 sub CalcNextLabelPos {
280     if ( $colcount lt $label_cols ) {
281
282         #        warn "new col";
283         $x_pos = ( $x_pos + $label_width + $colspace );
284         $colcount++;
285     }
286
287     else {
288         $x_pos = $left_margin;
289         if ( $rowcount eq $label_rows ) {
290
291             #            warn "new page";
292             prPage();
293             $y_pos    = ( $page_height - $top_margin - $label_height );
294             $rowcount = 1;
295         }
296         else {
297
298             #            warn "new row";
299             $y_pos = ( $y_pos - $rowspace - $label_height );
300             $rowcount++;
301         }
302         $colcount = 1;
303     }
304 }
305