Added issue date column to issues list
[koha.git] / labels / label-print-pdf.pl
blobe2cfe2772b70b96d445caf9c32d646e2245a5e91
1 #!/usr/bin/perl
3 use strict;
4 use CGI;
5 use C4::Labels;
6 use C4::Auth;
7 use C4::Output;
8 use C4::Context;
9 use HTML::Template::Pro;
10 use PDF::Reuse;
11 use PDF::Reuse::Barcode;
12 use POSIX;
13 #use C4::Labels;
14 #use Smart::Comments;
16 my $DEBUG = 0;
17 my $DEBUG_LPT = 0;
19 my $htdocs_path = C4::Context->config('intrahtdocs');
20 my $cgi = new CGI;
21 print $cgi->header( -type => 'application/pdf', -attachment => 'barcode.pdf' );
23 my $spine_text = "";
25 #warn "label-print-pdf ***";
27 # get the printing settings
28 my $template = GetActiveLabelTemplate();
29 my $conf_data = get_label_options();
30 my $profile = GetAssociatedProfile($template->{'tmpl_id'});
32 my $batch_id = $cgi->param('batch_id');
33 my @resultsloop = get_label_items($batch_id);
35 #$DB::single = 1;
37 my $barcodetype = $conf_data->{'barcodetype'};
38 my $printingtype = $conf_data->{'printingtype'};
39 my $guidebox = $conf_data->{'guidebox'};
40 my $start_label = $conf_data->{'startlabel'};
41 if ($cgi->param('startlabel')) {
42 $start_label = $cgi->param('startlabel'); # A bit of a hack to allow setting the starting label from the address bar... -fbcit
44 warn "Starting on label #$start_label" if $DEBUG;
45 my $fontsize = $template->{'fontsize'};
46 my $units = $template->{'units'};
48 ### $printingtype;
51 ################### defaults for testing
52 my $barcodetype = 'CODE39';
53 my $printingtype = 'BARBIB';
54 my $guidebox = 1;
55 my $start_label = 1;
56 my $units = 'POINTS'
57 =cut
59 #my $fontsize = 3;
61 #warn "UNITS $units";
62 #warn "fontsize = $fontsize";
63 #warn Dumper $template;
65 my $unitvalue = GetUnitsValue($units);
66 my $prof_unitvalue = GetUnitsValue($profile->{'unit'});
68 warn "Template units: $units which converts to $unitvalue PostScript Points" if $DEBUG;
69 warn "Profile units: $profile->{'unit'} which converts to $prof_unitvalue PostScript Points" if $DEBUG;
71 my $tmpl_code = $template->{'tmpl_code'};
72 my $tmpl_desc = $template->{'tmpl_desc'};
74 my $page_height = ( $template->{'page_height'} * $unitvalue );
75 my $page_width = ( $template->{'page_width'} * $unitvalue );
76 my $label_height = ( $template->{'label_height'} * $unitvalue );
77 my $label_width = ( $template->{'label_width'} * $unitvalue );
78 my $spine_width = ( $template->{'label_width'} * $unitvalue );
79 my $circ_width = ( $template->{'label_width'} * $unitvalue );
80 my $top_margin = ( $template->{'topmargin'} * $unitvalue );
81 my $left_margin = ( $template->{'leftmargin'} * $unitvalue );
82 my $colspace = ( $template->{'colgap'} * $unitvalue );
83 my $rowspace = ( $template->{'rowgap'} * $unitvalue );
85 warn "Converted dimensions are:" if $DEBUG;
86 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;
88 my $label_cols = $template->{'cols'};
89 my $label_rows = $template->{'rows'};
91 my $text_wrap_cols = GetTextWrapCols( $fontsize, $label_width );
94 #warn $label_cols, $label_rows;
96 # set the paper size
97 my $lowerLeftX = 0;
98 my $lowerLeftY = 0;
99 my $upperRightX = $page_width;
100 my $upperRightY = $page_height;
102 prInitVars();
103 $| = 1;
104 prFile();
106 prMbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
108 # later feature, change the font-type and size?
109 prFont('C'); # Just setting a font
110 prFontSize($fontsize);
112 my $margin = $top_margin;
113 my $left_text_margin = 3;
114 my $str;
116 #warn "STARTROW = $startrow\n";
118 #my $page_break_count = $startrow;
119 my $codetype; # = 'Code39';
121 #do page border
122 # drawbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
124 # draw margin box for alignment page
125 drawbox( ($left_margin), ($top_margin), ($page_width-(2*$left_margin)), ($page_height-(2*$top_margin)) ) if $DEBUG_LPT;
127 # Adjustments for image position and creep -fbcit
128 # NOTE: *All* of these factor in to image position and creep. Keep this in mind when makeing adjustments.
129 # Suggested proceedure: Adjust margins until both top and left margins are correct. Then adjust the label
130 # height and width to correct label creep across and down page. Units are PostScript Points (72 per inch).
132 warn "Active profile: " . ($profile->{'prof_id'}?$profile->{'prof_id'}:"None") if $DEBUG;
134 if ( $DEBUG ) {
135 warn "-------------------------INITIAL VALUES-----------------------------";
136 warn "top margin = $top_margin points\n";
137 warn "left margin = $left_margin points\n";
138 warn "label height = $label_height points\n";
139 warn "label width = $label_width points\n";
142 if ( $profile->{'prof_id'} ) {
143 $top_margin = $top_margin + ($profile->{'offset_vert'} * $prof_unitvalue); # controls vertical offset
144 $label_height = $label_height + ($profile->{'creep_vert'} * $prof_unitvalue); # controls vertical creep
145 $left_margin = $left_margin + ($profile->{'offset_horz'} * $prof_unitvalue); # controls horizontal offset
146 $label_width = $label_width + ($profile->{'creep_horz'} * $prof_unitvalue); # controls horizontal creep
149 if ( $DEBUG && $profile->{'prof_id'} ) {
150 warn "-------------------------ADJUSTED VALUES-----------------------------";
151 warn "top margin = $top_margin points\n";
152 warn "left margin = $left_margin points\n";
153 warn "label height = $label_height points\n";
154 warn "label width = $label_width points\n";
155 } elsif ( $DEBUG ) {
156 warn "No profile associated so no adjustment applied.";
159 my $item;
160 my ( $i, $i2 ); # loop counters
162 # big row loop
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";
169 my ( $rowcount, $colcount, $x_pos, $y_pos, $rowtemp, $coltemp );
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 );
178 else {
180 #eval {
181 $rowcount = ceil( $start_label / $label_cols );
183 #} ;
184 #$rowcount = 1 if $@;
186 $colcount = ( $start_label - ( ( $rowcount - 1 ) * $label_cols ) );
188 $x_pos = $left_margin + ( $label_width * ( $colcount - 1 ) ) +
189 ( $colspace * ( $colcount - 1 ) );
191 $y_pos = $page_height - $top_margin - ( $label_height * $rowcount ) -
192 ( $rowspace * ( $rowcount - 1 ) );
194 warn "Start label specified: $start_label Beginning in row $rowcount, column $colcount" if $DEBUG;
195 warn "X position = $x_pos Y position = $y_pos" if $DEBUG;
196 warn "Rowspace = $rowspace Label height = $label_height" if $DEBUG;
199 #warn "ROW COL $rowcount, $colcount";
201 #my $barcodetype; # = 'Code39';
204 # main foreach loop
207 foreach $item (@resultsloop) {
208 warn "Label parameters: xpos=$x_pos, ypos=$y_pos, lblwid=$label_width, lblhig=$label_height" if $DEBUG;
209 my $barcode = $item->{'barcode'};
210 if ( $printingtype eq 'BAR' ) {
211 drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
212 DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $barcode,
213 $barcodetype );
214 CalcNextLabelPos();
216 elsif ( $printingtype eq 'BARBIB' ) {
217 drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
219 # reposoitioning barcode up the top of label
220 my $barcode_height = ($label_height / 1.5 ); ## scaling voodoo
221 my $text_height = $label_height / 2;
222 my $barcode_y = $y_pos + ( $label_height / 2.5 ); ## scaling voodoo
224 DrawBarcode( $x_pos, $barcode_y, $barcode_height, $label_width,
225 $barcode, $barcodetype );
226 DrawSpineText( $y_pos, $text_height, $fontsize, $x_pos,
227 $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
229 CalcNextLabelPos();
231 } # correct
232 elsif ( $printingtype eq 'BIBBAR' ) {
233 drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
234 my $barcode_height = $label_height / 2;
235 DrawBarcode( $x_pos, $y_pos, $barcode_height, $label_width, $barcode,
236 $barcodetype );
237 DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
238 $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
240 CalcNextLabelPos();
243 elsif ( $printingtype eq 'ALT' ) {
244 drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
245 DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $barcode,
246 $barcodetype );
247 CalcNextLabelPos();
248 drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
249 DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
250 $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
252 CalcNextLabelPos();
256 elsif ( $printingtype eq 'BIB' ) {
257 drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
258 DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
259 $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
260 CalcNextLabelPos();
273 } # end for item loop
274 prEnd();
281 sub CalcNextLabelPos {
282 if ( $colcount lt $label_cols ) {
284 # warn "new col";
285 $x_pos = ( $x_pos + $label_width + $colspace );
286 $colcount++;
289 else {
290 $x_pos = $left_margin;
291 if ( $rowcount eq $label_rows ) {
293 # warn "new page";
294 prPage();
295 $y_pos = ( $page_height - $top_margin - $label_height );
296 $rowcount = 1;
298 else {
300 # warn "new row";
301 $y_pos = ( $y_pos - $rowspace - $label_height );
302 $rowcount++;
304 $colcount = 1;