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