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