Fourth 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 $barcodetype  = $conf_data->{'barcodetype'};
40 my $printingtype = $conf_data->{'printingtype'};
41 my $guidebox     = $conf_data->{'guidebox'};
42 my $start_label  = $conf_data->{'startlabel'};
43 if ($cgi->param('startlabel')) {
44         $start_label = $cgi->param('startlabel');       # A bit of a hack to allow setting the starting label from the address bar... -fbcit
45     }
46 warn "Starting on label #$start_label" if $DEBUG;
47 my $units        = $template->{'units'};
48
49 if ($printingtype eq 'PATCRD') {
50     @resultsloop = GetPatronCardItems($batch_id);
51 } else {
52     @resultsloop = GetLabelItems($batch_id);
53 }
54
55 #warn "UNITS $units";
56 #warn "fontsize = $fontsize";
57 #warn Dumper $template;
58
59 my $unitvalue = GetUnitsValue($units);
60 my $prof_unitvalue = GetUnitsValue($profile->{'unit'});
61
62 warn "Template units: $units which converts to $unitvalue PostScript Points" if $DEBUG;
63 warn "Profile units: $profile->{'unit'} which converts to $prof_unitvalue PostScript Points" if $DEBUG;
64
65 my $tmpl_code = $template->{'tmpl_code'};
66 my $tmpl_desc = $template->{'tmpl_desc'};
67
68 my $page_height  = ( $template->{'page_height'} * $unitvalue );
69 my $page_width   = ( $template->{'page_width'} * $unitvalue );
70 my $label_height = ( $template->{'label_height'} * $unitvalue );
71 my $label_width  = ( $template->{'label_width'} * $unitvalue );
72 my $spine_width  = ( $template->{'label_width'} * $unitvalue );
73 my $circ_width   = ( $template->{'label_width'} * $unitvalue );
74 my $top_margin   = ( $template->{'topmargin'} * $unitvalue );
75 my $left_margin  = ( $template->{'leftmargin'} * $unitvalue );
76 my $colspace     = ( $template->{'colgap'} * $unitvalue );
77 my $rowspace     = ( $template->{'rowgap'} * $unitvalue );
78
79 warn "Converted dimensions are:" if $DEBUG;
80 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;
81
82 my $label_cols = $template->{'cols'};
83 my $label_rows = $template->{'rows'};
84
85 my $margin           = $top_margin;
86 my $left_text_margin = 3;       # FIXME: This value should not be hardcoded
87 my $str;
88
89 prInitVars();
90 $| = 1;
91 prFile();
92
93 # Some peritent notes from PDF::Reuse regarding prFont()...
94 # If a font wasn't found, Helvetica will be set.
95 # These names are always recognized: Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic, Courier, Courier-Bold,
96 #   Courier-Oblique, Courier-BoldOblique, Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique
97 # They can be abbreviated: TR, TB, TI, TBI, C, CB, CO, CBO, H, HB, HO, HBO
98
99 my $fontsize    = $template->{'fontsize'};
100 my $fontname    = $template->{'font'};
101
102 my $text_wrap_cols = GetTextWrapCols( $fontname, $fontsize, $label_width, $left_text_margin );
103
104 #warn $label_cols, $label_rows;
105
106 # set the paper size
107 my $lowerLeftX  = 0;
108 my $lowerLeftY  = 0;
109 my $upperRightX = $page_width;
110 my $upperRightY = $page_height;
111
112 prMbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
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'} * $prof_unitvalue);    #  controls vertical offset
142     $label_height = $label_height + ($profile->{'creep_vert'} * $prof_unitvalue);    # controls vertical creep
143     $left_margin = $left_margin + ($profile->{'offset_horz'} * $prof_unitvalue);    # controls horizontal offset
144     $label_width = $label_width + ($profile->{'creep_horz'} * $prof_unitvalue);    # 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     if ( $printingtype eq 'BAR' ) {
208         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
209         DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $item->{'barcode'},
210             $barcodetype );
211         CalcNextLabelPos();
212     }
213     elsif ( $printingtype eq 'BARBIB' ) {
214         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
215
216         # reposoitioning barcode up the top of label
217         my $barcode_height = ($label_height / 1.5 );    ## scaling voodoo
218         my $text_height    = $label_height / 2;
219         my $barcode_y      = $y_pos + ( $label_height / 2.5  );   ## scaling voodoo
220
221         DrawBarcode( $x_pos, $barcode_y, $barcode_height, $label_width,
222             $item->{'barcode'}, $barcodetype );
223         DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
224             $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
225
226         CalcNextLabelPos();
227
228     }    # correct
229     elsif ( $printingtype eq 'BIBBAR' ) {
230         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
231         my $barcode_height = $label_height / 2;
232         DrawBarcode( $x_pos, $y_pos, $barcode_height, $label_width, $item->{'barcode'},
233             $barcodetype );
234         DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
235             $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
236
237         CalcNextLabelPos();
238     }
239
240     elsif ( $printingtype eq 'ALT' ) {
241         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
242         DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $item->{'barcode'},
243             $barcodetype );
244         CalcNextLabelPos();
245         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
246         DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
247             $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
248
249         CalcNextLabelPos();
250     }
251
252
253     elsif ( $printingtype eq 'BIB' ) {
254         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
255         DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
256             $left_text_margin, $text_wrap_cols, \$item, \$conf_data, $printingtype );
257         CalcNextLabelPos();
258     }
259
260     elsif ( $printingtype eq 'PATCRD' ) {
261         my $borrowernumber = '3';       # Hardcoded for testing purposes...
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