Labels Cleanup (Part 2 of Many)
[koha-ffzg.git] / labels / label-print-pdf.pl
1 #!/usr/bin/perl
2
3 use strict;
4 # use warnings;  # FIXME
5 use CGI;
6 use C4::Labels;     # GetActiveLabelTemplate get_label_options GetAssociatedProfile 
7 # GetPatronCardItems GetLabelItems GetUnitsValue...
8 use C4::Auth;
9 use C4::Output;
10 use C4::Context;
11 use C4::Members;
12 use C4::Branch;
13 use HTML::Template::Pro;
14 use PDF::Reuse;
15 use PDF::Reuse::Barcode;
16 use POSIX;  # ceil
17 use Data::Dumper;
18
19 my $DEBUG = 0;
20 my $DEBUG_LPT = 0;
21
22 my $cgi         = new CGI;
23
24 #### Tons of Initialization ####
25 # get the printing settings
26 my $template    = GetActiveLabelTemplate();
27 my $conf_data   = get_label_options() or die "get_label_options failed";
28 my $profile     = GetAssociatedProfile($template->{'tmpl_id'});
29
30 my $batch_id =   $cgi->param('batch_id');
31 my @resultsloop;
32
33 my $batch_type   = $conf_data->{'type'};
34 my $barcodetype  = $conf_data->{'barcodetype'};
35 my $printingtype = $conf_data->{'printingtype'} or die "No printingtype in conf_data";
36 my $guidebox     = $conf_data->{'guidebox'};
37 my $start_label  = $conf_data->{'startlabel'};
38 if ($cgi->param('startlabel')) {
39     $start_label = $cgi->param('startlabel');       # A bit of a hack to allow setting the starting label from the address bar... -fbcit
40 }
41 warn "Starting on label #$start_label" if $DEBUG;
42 my $units        = $template->{'units'};
43
44 if ($printingtype eq 'PATCRD') {
45     @resultsloop = GetPatronCardItems($batch_id);
46 } else {
47     @resultsloop = GetLabelItems($batch_id);
48 }
49
50 #warn "UNITS $units";
51 #warn "fontsize = $fontsize";
52 #warn Dumper $template;
53
54 my $unitvalue      = GetUnitsValue($units);
55 my $prof_unitvalue = GetUnitsValue($profile->{'unit'});
56
57 warn "Template units: $units which converts to $unitvalue PostScript Points" if $DEBUG;
58 warn "Profile units: $profile->{'unit'} which converts to $prof_unitvalue PostScript Points" if $DEBUG;
59
60 my $tmpl_code = $template->{'tmpl_code'};
61 my $tmpl_desc = $template->{'tmpl_desc'};
62
63 my $page_height  = ( $template->{'page_height'}  * $unitvalue );
64 my $page_width   = ( $template->{'page_width'}   * $unitvalue );
65 my $label_height = ( $template->{'label_height'} * $unitvalue );
66 my $label_width  = ( $template->{'label_width'}  * $unitvalue );
67 my $spine_width  = ( $template->{'label_width'}  * $unitvalue );
68 my $circ_width   = ( $template->{'label_width'}  * $unitvalue );
69 my $top_margin   = ( $template->{'topmargin'}    * $unitvalue );
70 my $left_margin  = ( $template->{'leftmargin'}   * $unitvalue );
71 my $colspace     = ( $template->{'colgap'}       * $unitvalue );
72 my $rowspace     = ( $template->{'rowgap'}       * $unitvalue );
73
74 warn "Converted dimensions are:" if $DEBUG;
75 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;
76
77 my $label_cols = $template->{'cols'};
78 my $label_rows = $template->{'rows'};
79
80 my $margin           = $top_margin;
81 my $left_text_margin = 3;       # FIXME: This value should not be hardcoded
82 my $str;
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 my $codetype; # = 'Code39';
103
104 warn "Active profile: " . ($profile->{prof_id} || "None") if $DEBUG;
105
106 #### PRINT PRELIMINARY DATA ####
107 print $cgi->header( -type => 'application/pdf', -attachment => 'barcode.pdf' ); 
108     # Don't print header until very last possible moment
109     # That way if error or die occurs, fatals_to_browser will still work.
110     # After we print this header, there is no way back to HTML.  All we can do is deliver PDF.
111 prInitVars();
112 $| = 1;
113 prFile();   # No args means to STDOUT
114 prMbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
115
116 # drawbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );  #do page border
117 # draw margin box for alignment page
118 drawbox($left_margin, $top_margin, $page_width-(2*$left_margin), $page_height-(2*$top_margin)) if $DEBUG_LPT;
119
120
121 #### TWEAKS and DEBUGGING ###
122 # Adjustments for image position and creep -fbcit
123 # NOTE: *All* of these factor in to image position and creep. Keep this in mind when makeing adjustments.
124 # Suggested proceedure: Adjust margins until both top and left margins are correct. Then adjust the label
125 # height and width to correct label creep across and down page. Units are PostScript Points (72 per inch).
126
127 sub debug_drop {
128     my $title = @_ || "";
129     warn "-------------------------$title-----------------------------\n"
130      . "  top margin = $top_margin points\n" 
131      . " left margin = $left_margin points\n"
132      . "label height = $label_height points\n"
133      . "label width  = $label_width points\n";
134 }
135
136 debug_drop('INITIAL VALUES') if ($DEBUG);
137
138 if ( $profile->{'prof_id'} ) {
139     $top_margin   += ($profile->{'offset_vert'} * $prof_unitvalue);    # controls vertical offset
140     $label_height += ($profile->{'creep_vert'}  * $prof_unitvalue);    # controls vertical creep
141     $left_margin  += ($profile->{'offset_horz'} * $prof_unitvalue);    # controls horizontal offset
142     $label_width  += ($profile->{'creep_horz'}  * $prof_unitvalue);    # controls horizontal creep
143 }
144
145 if ($DEBUG) {
146     if ($profile->{'prof_id'}) {
147         debug_drop('ADJUSTED VALUES');
148     } else {
149         warn "No profile associated so no adjustment applied.";
150     }
151 }
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 ($item, $rowcount, $colcount, $x_pos, $y_pos, $rowtemp, $coltemp);
159
160 if ( $start_label and $start_label == 1 ) {
161     $rowcount = 1;
162     $colcount = 1;
163     $x_pos    = $left_margin;
164     $y_pos    = ( $page_height - $top_margin - $label_height );
165 } else {
166     $rowcount = ceil( $start_label / $label_cols );
167     $colcount = ( $start_label - ( ( $rowcount - 1 ) * $label_cols ) );
168     $x_pos = $left_margin + ( $label_width * ( $colcount - 1 ) ) +
169       ( $colspace * ( $colcount - 1 ) );
170     $y_pos = $page_height - $top_margin - ( $label_height * $rowcount ) -
171       ( $rowspace * ( $rowcount - 1 ) );
172     $DEBUG and warn "Start label: $start_label. Beginning in row $rowcount, column $colcount\n"
173         . "(X,Y) positions = ($x_pos,$y_pos)\n"
174         . "Rowspace = $rowspace, Label height = $label_height";
175 }
176
177 #
178 #### main foreach loop #### 
179 #
180
181 foreach $item (@resultsloop) {
182     warn "Label parameters: xpos=$x_pos, ypos=$y_pos, lblwid=$label_width, lblhig=$label_height" if $DEBUG;
183
184     drawbox($x_pos, $y_pos, $label_width, $label_height) if $guidebox;  # regardless of printingtype
185
186     if ( $printingtype eq 'BAR' ) {
187         DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $item->{'barcode'}, $barcodetype );
188     }
189     elsif ( $printingtype eq 'BARBIB' ) {
190         # reposoitioning barcode up the top of label
191         my $barcode_height = ($label_height / 1.5 );    ## scaling voodoo
192         my $text_height    = $label_height / 2;
193         my $barcode_y      = $y_pos + ( $label_height / 2.5  );   ## scaling voodoo
194
195         DrawBarcode( $x_pos, $barcode_y, $barcode_height, $label_width, $item->{'barcode'}, $barcodetype );
196         DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
197             $left_text_margin, $text_wrap_cols, \$item, \$conf_data, $printingtype );
198     }    # correct
199     elsif ( $printingtype eq 'BIBBAR' ) {
200         my $barcode_height = $label_height / 2;
201         DrawBarcode( $x_pos, $y_pos, $barcode_height, $label_width, $item->{'barcode'}, $barcodetype );
202         DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
203             $left_text_margin, $text_wrap_cols, \$item, \$conf_data, $printingtype );
204     }
205     elsif ( $printingtype eq 'ALT' ) {
206         DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $item->{'barcode'}, $barcodetype );
207         CalcNextLabelPos();
208         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
209         DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
210             $left_text_margin, $text_wrap_cols, \$item, \$conf_data, $printingtype );
211     }
212     elsif ( $printingtype eq 'BIB' ) {
213         DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
214             $left_text_margin, $text_wrap_cols, \$item, \$conf_data, $printingtype );
215     }
216     elsif ( $printingtype eq 'PATCRD' ) {
217         my $patron_data = $item;
218         #FIXME: This needs to be paramatized and passed in from the user...
219         #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.
220         my $text = {        
221             $patron_data->{'description'}  => $fontsize,
222             $patron_data->{'branchname'}   => ($fontsize + 3),
223         };
224         $DEBUG and warn "Generating patron card for cardnumber $patron_data->{'cardnumber'}";
225         my $barcode_height = $label_height / 2.75; #FIXME: Scaling barcode height; this needs to be a user parameter.
226         DrawBarcode( $x_pos, $y_pos, $barcode_height, $label_width, $patron_data->{'cardnumber'}, $barcodetype );
227         DrawPatronCardText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
228             $left_text_margin, $text_wrap_cols, $text, $printingtype );
229     }
230     else {
231         die "CANNOT PRINT: Unknown printingtype '$printingtype'";
232     }
233
234     CalcNextLabelPos();     # regardless of printingtype
235 }    # end for item loop
236 prEnd();
237
238 sub CalcNextLabelPos {
239     if ($colcount < $label_cols) {
240         # warn "new col";
241         $x_pos = ( $x_pos + $label_width + $colspace );
242         $colcount++;
243     } else {
244         $x_pos = $left_margin;
245         if ($rowcount == $label_rows) {
246             # warn "new page";
247             prPage();
248             $y_pos    = ( $page_height - $top_margin - $label_height );
249             $rowcount = 1;
250         } else {
251             # warn "new row";
252             $y_pos = ( $y_pos - $rowspace - $label_height );
253             $rowcount++;
254         }
255         $colcount = 1;
256     }
257 }
258