Labels.pm - add deduplicate_batch sub
[koha.git] / labels / label-print-pdf.pl
blobefdbed86ca79edfc3a6bfb8a3f8391c5197a1a5e
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 $htdocs_path = C4::Context->config('intrahtdocs');
17 my $cgi = new CGI;
18 print $cgi->header( -type => 'application/pdf', -attachment => 'barcode.pdf' );
20 my $spine_text = "";
22 #warn "label-print-pdf ***";
24 # get the printing settings
25 my $template = GetActiveLabelTemplate();
26 my $conf_data = get_label_options();
28 my $batch_id = $cgi->param('batch_id');
29 my @resultsloop = get_label_items($batch_id);
31 #$DB::single = 1;
33 my $barcodetype = $conf_data->{'barcodetype'};
34 my $printingtype = $conf_data->{'printingtype'};
35 my $guidebox = $conf_data->{'guidebox'};
36 my $start_label = $conf_data->{'startlabel'};
37 my $fontsize = $template->{'fontsize'};
38 my $units = $template->{'units'};
40 ### $printingtype;
43 ################### defaults for testing
44 my $barcodetype = 'CODE39';
45 my $printingtype = 'BARBIB';
46 my $guidebox = 1;
47 my $start_label = 1;
48 my $units = 'POINTS'
49 =cut
51 #my $fontsize = 3;
53 #warn "UNITS $units";
54 #warn "fontsize = $fontsize";
55 #warn Dumper $template;
57 my $unitvalue = GetUnitsValue($units);
59 my $tmpl_code = $template->{'tmpl_code'};
60 my $tmpl_desc = $template->{'tmpl_desc'};
62 my $page_height = ( $template->{'page_height'} * $unitvalue );
63 my $page_width = ( $template->{'page_width'} * $unitvalue );
64 my $label_height = ( $template->{'label_height'} * $unitvalue );
65 my $label_width = ( $template->{'label_width'} * $unitvalue );
66 my $spine_width = ( $template->{'label_width'} * $unitvalue );
67 my $circ_width = ( $template->{'label_width'} * $unitvalue );
68 my $top_margin = ( $template->{'topmargin'} * $unitvalue );
69 my $left_margin = ( $template->{'leftmargin'} * $unitvalue );
70 my $colspace = ( $template->{'colgap'} * $unitvalue );
71 my $rowspace = ( $template->{'rowgap'} * $unitvalue );
73 my $label_cols = $template->{'cols'};
74 my $label_rows = $template->{'rows'};
76 my $text_wrap_cols = GetTextWrapCols( $fontsize, $label_width );
78 #warn $label_cols, $label_rows;
80 # set the paper size
81 my $lowerLeftX = 0;
82 my $lowerLeftY = 0;
83 my $upperRightX = $page_width;
84 my $upperRightY = $page_height;
86 prInitVars();
87 $| = 1;
88 prFile();
90 prMbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
92 # later feature, change the font-type and size?
93 prFont('C'); # Just setting a font
94 prFontSize($fontsize);
96 my $margin = $top_margin;
97 my $left_text_margin = 3;
98 my $str;
100 #warn "STARTROW = $startrow\n";
102 #my $page_break_count = $startrow;
103 my $codetype; # = 'Code39';
105 #do page border
106 #drawbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
108 my $item;
109 my ( $i, $i2 ); # loop counters
111 # big row loop
113 #warn " $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY";
114 #warn "$label_rows, $label_cols\n";
115 #warn "$label_height, $label_width\n";
116 #warn "$page_height, $page_width\n";
118 my ( $rowcount, $colcount, $x_pos, $y_pos, $rowtemp, $coltemp );
120 if ( $start_label eq 1 ) {
121 $rowcount = 1;
122 $colcount = 1;
123 $x_pos = $left_margin;
124 $y_pos = ( $page_height - $top_margin - $label_height );
127 else {
129 #eval {
130 $rowcount = ceil( $start_label / $label_cols );
132 #} ;
133 #$rowcount = 1 if $@;
135 $colcount = ( $start_label - ( ( $rowcount - 1 ) * $label_cols ) );
137 $x_pos = $left_margin + ( $label_width * ( $colcount - 1 ) ) +
138 ( $colspace * ( $colcount - 1 ) );
140 $y_pos = $page_height - $top_margin - ( $label_height * $rowcount ) -
141 ( $rowspace * ( $rowcount - 1 ) );
145 #warn "ROW COL $rowcount, $colcount";
147 #my $barcodetype; # = 'Code39';
150 # main foreach loop
153 foreach $item (@resultsloop) {
154 # warn "$x_pos, $y_pos, $label_width, $label_height";
155 my $barcode = $item->{'barcode'};
156 if ( $printingtype eq 'BAR' ) {
157 drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
158 DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $barcode,
159 $barcodetype );
160 CalcNextLabelPos();
162 elsif ( $printingtype eq 'BARBIB' ) {
163 drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
165 # reposoitioning barcode up the top of label
166 my $barcode_height = ($label_height / 1.5 ); ## scaling voodoo
167 my $text_height = $label_height / 2;
168 my $barcode_y = $y_pos + ( $label_height / 2.5 ); ## scaling voodoo
170 DrawBarcode( $x_pos, $barcode_y, $barcode_height, $label_width,
171 $barcode, $barcodetype );
172 DrawSpineText( $y_pos, $text_height, $fontsize, $x_pos,
173 $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
175 CalcNextLabelPos();
177 } # correct
178 elsif ( $printingtype eq 'BIBBAR' ) {
179 drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
180 my $barcode_height = $label_height / 2;
181 DrawBarcode( $x_pos, $y_pos, $barcode_height, $label_width, $barcode,
182 $barcodetype );
183 DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
184 $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
186 CalcNextLabelPos();
189 elsif ( $printingtype eq 'ALT' ) {
190 drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
191 DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $barcode,
192 $barcodetype );
193 CalcNextLabelPos();
194 drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
195 DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
196 $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
198 CalcNextLabelPos();
202 elsif ( $printingtype eq 'BIB' ) {
203 drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
204 DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
205 $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
206 CalcNextLabelPos();
219 } # end for item loop
220 prEnd();
227 sub CalcNextLabelPos {
228 if ( $colcount lt $label_cols ) {
230 # warn "new col";
231 $x_pos = ( $x_pos + $label_width + $colspace );
232 $colcount++;
235 else {
236 $x_pos = $left_margin;
237 if ( $rowcount eq $label_rows ) {
239 # warn "new page";
240 prPage();
241 $y_pos = ( $page_height - $top_margin - $label_height );
242 $rowcount = 1;
244 else {
246 # warn "new row";
247 $y_pos = ( $y_pos - $rowspace - $label_height );
248 $rowcount++;
250 $colcount = 1;