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