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