(bug #4334) create needed sample datas
[koha.git] / labels / label-print-pdf.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use CGI;
6 use C4::Labels;
7 use C4::Auth;
8 use C4::Output;
9 use C4::Context;
10 use C4::Members;
11 use C4::Branch;
12 use HTML::Template::Pro;
13 use PDF::Reuse;
14 use PDF::Reuse::Barcode;
15 use POSIX;
16 use Data::Dumper;
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 # get the printing settings
28 my $template    = GetActiveLabelTemplate();
29 my $conf_data   = get_label_options();
30 my $profile     = GetAssociatedProfile($template->{'tmpl_id'});
31
32 my $batch_id =   $cgi->param('batch_id');
33 my @resultsloop;
34
35 my $batch_type   = $conf_data->{'type'};
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 if ($printingtype eq 'PATCRD') {
47     @resultsloop = GetPatronCardItems($batch_id);
48 } else {
49     @resultsloop = GetLabelItems($batch_id);
50 }
51
52 #warn "UNITS $units";
53 #warn "fontsize = $fontsize";
54 #warn Dumper $template;
55
56 my $unitvalue = GetUnitsValue($units);
57 my $prof_unitvalue = GetUnitsValue($profile->{'unit'});
58
59 warn "Template units: $units which converts to $unitvalue PostScript Points" if $DEBUG;
60 warn "Profile units: $profile->{'unit'} which converts to $prof_unitvalue PostScript Points" if $DEBUG;
61
62 my $tmpl_code = $template->{'tmpl_code'};
63 my $tmpl_desc = $template->{'tmpl_desc'};
64
65 my $page_height  = ( $template->{'page_height'} * $unitvalue );
66 my $page_width   = ( $template->{'page_width'} * $unitvalue );
67 my $label_height = ( $template->{'label_height'} * $unitvalue );
68 my $label_width  = ( $template->{'label_width'} * $unitvalue );
69 my $spine_width  = ( $template->{'label_width'} * $unitvalue );
70 my $circ_width   = ( $template->{'label_width'} * $unitvalue );
71 my $top_margin   = ( $template->{'topmargin'} * $unitvalue );
72 my $left_margin  = ( $template->{'leftmargin'} * $unitvalue );
73 my $colspace     = ( $template->{'colgap'} * $unitvalue );
74 my $rowspace     = ( $template->{'rowgap'} * $unitvalue );
75
76 warn "Converted dimensions are:" if $DEBUG;
77 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;
78
79 my $label_cols = $template->{'cols'};
80 my $label_rows = $template->{'rows'};
81
82 my $margin           = $top_margin;
83 my $left_text_margin = 3;       # FIXME: This value should not be hardcoded
84 my $str;
85
86 prInitVars();
87 $| = 1;
88 prFile();
89
90 # Some peritent notes from PDF::Reuse regarding prFont()...
91 # If a font wasn't found, Helvetica will be set.
92 # These names are always recognized: Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic, Courier, Courier-Bold,
93 #   Courier-Oblique, Courier-BoldOblique, Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique
94 # They can be abbreviated: TR, TB, TI, TBI, C, CB, CO, CBO, H, HB, HO, HBO
95
96 my $fontsize    = $template->{'fontsize'};
97 my $fontname    = $template->{'font'};
98
99 my $text_wrap_cols = GetTextWrapCols( $fontname, $fontsize, $label_width, $left_text_margin );
100
101 #warn $label_cols, $label_rows;
102
103 # set the paper size
104 my $lowerLeftX  = 0;
105 my $lowerLeftY  = 0;
106 my $upperRightX = $page_width;
107 my $upperRightY = $page_height;
108
109 warn "Active profile: " . ($profile->{prof_id} || "None") if $DEBUG;
110
111 #### PRINT PRELIMINARY DATA ####
112 print $cgi->header( -type => 'application/pdf', -attachment => 'barcode.pdf' ); 
113     # Don't print header until very last possible moment
114     # That way if error or die occurs, fatals_to_browser will still work.
115     # After we print this header, there is no way back to HTML.  All we can do is deliver PDF.
116 prInitVars();
117 $| = 1;
118 prFile();   # No args means to STDOUT
119 prCompress(1);  # turn on zip compression which dramatically reduces file size
120 prMbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
121
122 my $codetype; # = 'Code39';
123
124 #do page border
125 # drawbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
126
127 # draw margin box for alignment page
128 drawbox( ($left_margin), ($top_margin), ($page_width-(2*$left_margin)), ($page_height-(2*$top_margin)) ) if $DEBUG_LPT;
129
130 # Adjustments for image position and creep -fbcit
131 # NOTE: *All* of these factor in to image position and creep. Keep this in mind when makeing adjustments.
132 # Suggested proceedure: Adjust margins until both top and left margins are correct. Then adjust the label
133 # height and width to correct label creep across and down page. Units are PostScript Points (72 per inch).
134
135 warn "Active profile: " . ($profile->{'prof_id'}?$profile->{'prof_id'}:"None") if $DEBUG;
136
137 if ( $DEBUG ) {
138     warn "-------------------------INITIAL VALUES-----------------------------";
139     warn "top margin = $top_margin points\n";
140     warn "left margin = $left_margin points\n";
141     warn "label height = $label_height points\n";
142     warn "label width = $label_width points\n";
143 }
144
145 if ( $profile->{'prof_id'} ) {
146     $top_margin = $top_margin + ($profile->{'offset_vert'} * $prof_unitvalue);    #  controls vertical offset
147     $label_height = $label_height + ($profile->{'creep_vert'} * $prof_unitvalue);    # controls vertical creep
148     $left_margin = $left_margin + ($profile->{'offset_horz'} * $prof_unitvalue);    # controls horizontal offset
149     $label_width = $label_width + ($profile->{'creep_horz'} * $prof_unitvalue);    # controls horizontal creep
150 }
151
152 if ( $DEBUG && $profile->{'prof_id'} ) {
153     warn "-------------------------ADJUSTED VALUES-----------------------------";
154     warn "top margin = $top_margin points\n";
155     warn "left margin = $left_margin points\n";
156     warn "label height = $label_height points\n";
157     warn "label width = $label_width points\n";
158 } elsif ( $DEBUG ) {
159     warn "No profile associated so no adjustment applied.";
160 }
161
162 my ( $i, $i2 );    # loop counters
163
164 #warn " $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY";
165 #warn "$label_rows, $label_cols\n";
166 #warn "$label_height, $label_width\n";
167 #warn "$page_height, $page_width\n";
168
169 my ( $rowcount, $colcount, $x_pos, $y_pos, $rowtemp, $coltemp );
170
171 if ( $start_label eq 1 ) {
172     $rowcount = 1;
173     $colcount = 1;
174     $x_pos    = $left_margin;
175     $y_pos    = ( $page_height - $top_margin - $label_height );
176 }
177
178 else {
179     $rowcount = ceil( $start_label / $label_cols );
180     $colcount = ( $start_label - ( ( $rowcount - 1 ) * $label_cols ) );
181     $x_pos = $left_margin + ( $label_width * ( $colcount - 1 ) ) +
182       ( $colspace * ( $colcount - 1 ) );
183     $y_pos = $page_height - $top_margin - ( $label_height * $rowcount ) -
184       ( $rowspace * ( $rowcount - 1 ) );
185     warn "Start label specified: $start_label Beginning in row $rowcount, column $colcount" if $DEBUG;
186     warn "X position = $x_pos Y position = $y_pos" if $DEBUG;
187     warn "Rowspace = $rowspace Label height = $label_height" if $DEBUG;
188 }
189
190 #
191 #    main foreach loop
192 #
193
194 foreach my $item (@resultsloop) {
195     warn "Label parameters: xpos=$x_pos, ypos=$y_pos, lblwid=$label_width, lblhig=$label_height" if $DEBUG;
196     if ( $printingtype eq 'BAR' ) {
197         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
198         DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $item->{'barcode'},
199             $barcodetype );
200         CalcNextLabelPos();
201     }
202     elsif ( $printingtype eq 'BARBIB' ) {
203         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
204
205         # reposoitioning barcode up the top of label
206         my $barcode_height = ($label_height / 1.5 );    ## scaling voodoo
207         my $text_height    = $label_height / 2;
208         my $barcode_y      = $y_pos + ( $label_height / 2.5  );   ## scaling voodoo
209
210         DrawBarcode( $x_pos, $barcode_y, $barcode_height, $label_width,
211             $item->{'barcode'}, $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     }    # correct
218     elsif ( $printingtype eq 'BIBBAR' ) {
219         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
220         my $barcode_height = $label_height / 2;
221         DrawBarcode( $x_pos, $y_pos, $barcode_height, $label_width, $item->{'barcode'},
222             $barcodetype );
223         DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
224             $left_text_margin, $text_wrap_cols, \$item, \$conf_data, $printingtype );
225
226         CalcNextLabelPos();
227     }
228
229     elsif ( $printingtype eq 'ALT' ) {
230         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
231         DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $item->{'barcode'},
232             $barcodetype );
233         CalcNextLabelPos();
234         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
235         DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
236             $left_text_margin, $text_wrap_cols, \$item, \$conf_data, $printingtype );
237
238         CalcNextLabelPos();
239     }
240
241
242     elsif ( $printingtype eq 'BIB' ) {
243         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
244         DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
245             $left_text_margin, $text_wrap_cols, \$item, \$conf_data, $printingtype );
246         CalcNextLabelPos();
247     }
248
249     elsif ( $printingtype eq 'PATCRD' ) {
250         my $patron_data = $item;
251
252         #FIXME: This needs to be paramatized and passed in from the user...
253         #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.
254         my $text = {        
255             $patron_data->{'description'}  => $fontsize,
256             $patron_data->{'branchname'}   => ($fontsize + 3),
257         };
258
259         $DEBUG and warn "Generating patron card for cardnumber $patron_data->{'cardnumber'}";
260
261         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
262         my $barcode_height = $label_height / 2.75; #FIXME: Scaling barcode height; this needs to be a user parameter.
263         DrawBarcode( $x_pos, $y_pos, $barcode_height, $label_width, $patron_data->{'cardnumber'},
264             $barcodetype );
265         DrawPatronCardText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
266             $left_text_margin, $text_wrap_cols, $text, $printingtype );
267         CalcNextLabelPos();
268     }
269 }    # end for item loop
270 prEnd();
271
272 sub CalcNextLabelPos {
273     if ( $colcount lt $label_cols ) {
274
275         #        warn "new col";
276         $x_pos = ( $x_pos + $label_width + $colspace );
277         $colcount++;
278     }
279
280     else {
281         $x_pos = $left_margin;
282         if ( $rowcount eq $label_rows ) {
283
284             #            warn "new page";
285             prPage();
286             $y_pos    = ( $page_height - $top_margin - $label_height );
287             $rowcount = 1;
288         }
289         else {
290
291             #            warn "new row";
292             $y_pos = ( $y_pos - $rowspace - $label_height );
293             $rowcount++;
294         }
295         $colcount = 1;
296     }
297 }
298