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