[20/40] Work on label printing code.
[koha_fer] / labels / label-print-pdf.pl
index 2abacd6..1c6bbba 100755 (executable)
 #!/usr/bin/perl
 
+# Copyright 2006 Katipo Communications.
+# Some parts Copyright 2009 Foundations Bible College.
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+# Suite 330, Boston, MA  02111-1307 USA
+
 use strict;
+use warnings;
+
 use CGI;
+use HTML::Template::Pro;
+use POSIX qw(ceil);
+use Data::Dumper;
+use Sys::Syslog qw(syslog);
+
 use C4::Labels;
 use C4::Auth;
 use C4::Output;
 use C4::Context;
-use HTML::Template::Pro;
-use PDF::Reuse;
-use PDF::Reuse::Barcode;
-use POSIX;
-#use C4::Labels;
-#use Smart::Comments;
+use C4::Members;
+use C4::Branch;
+use C4::Debug;
+use C4::Labels::Batch 1.000000;
+use C4::Labels::Template 1.000000;
+use C4::Labels::Layout 1.000000;
+use C4::Labels::PDF 1.000000;
+use C4::Labels::Label 1.000000;
 
-my $DEBUG = 0;
-my $DEBUG_LPT = 0;
+my $cgi = new CGI;
 
 my $htdocs_path = C4::Context->config('intrahtdocs');
-my $cgi         = new CGI;
-print $cgi->header( -type => 'application/pdf', -attachment => 'barcode.pdf' );
-
-my $spine_text = "";
-
-#warn "label-print-pdf ***";
-
-# get the printing settings
-my $template    = GetActiveLabelTemplate();
-my $conf_data   = get_label_options();
-my $profile = GetAssociatedProfile($template->{'tmpl_id'});
-
-my $batch_id =   $cgi->param('batch_id');
-my @resultsloop = get_label_items($batch_id);
-
-#$DB::single = 1;
-
-my $barcodetype  = $conf_data->{'barcodetype'};
-my $printingtype = $conf_data->{'printingtype'};
-my $guidebox     = $conf_data->{'guidebox'};
-my $start_label  = $conf_data->{'startlabel'};
-if ($cgi->param('startlabel')) {
-        $start_label = $cgi->param('startlabel');       # A bit of a hack to allow setting the starting label from the address bar... -fbcit
+my $batch_id    = $cgi->param('batch_id') || $ARGV[0];
+my $template_id = $cgi->param('template_id') || $ARGV[1];
+my $layout_id   = $cgi->param('layout_id') || $ARGV[2];
+my $start_label = $cgi->param('start_label') || $ARGV[3];
+
+print $cgi->header( -type => 'application/pdf', -attachment => "koha_batch_$batch_id.pdf" );
+
+my $pdf = C4::Labels::PDF->new(InitVars => 0);
+my $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
+my $template = C4::Labels::Template->retrieve(template_id => $template_id, profile_id => 1);
+my $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
+
+sub _calc_next_label_pos {
+    my ($row_count, $col_count, $llx, $lly) = @_;
+    if ($col_count lt $template->get_attr('cols')) {
+        $llx = ($llx + $template->get_attr('label_width') + $template->get_attr('col_gap'));
+        $col_count++;
     }
-warn "Starting on label #$start_label" if $DEBUG;
-my $fontsize     = $template->{'fontsize'};
-my $units        = $template->{'units'};
-
-### $printingtype;
-
-=c
-################### defaults for testing
-my $barcodetype  = 'CODE39';
-my $printingtype = 'BARBIB';
-my $guidebox     = 1;
-my $start_label  = 1;
-my $units        = 'POINTS'
-=cut
-
-#my $fontsize = 3;
-
-#warn "UNITS $units";
-#warn "fontsize = $fontsize";
-#warn Dumper $template;
-
-my $unitvalue = GetUnitsValue($units);
-
-warn "Template units: $units which converts to $unitvalue PostScript Points" if $DEBUG;
-
-my $tmpl_code = $template->{'tmpl_code'};
-my $tmpl_desc = $template->{'tmpl_desc'};
-
-my $page_height  = ( $template->{'page_height'} * $unitvalue );
-my $page_width   = ( $template->{'page_width'} * $unitvalue );
-my $label_height = ( $template->{'label_height'} * $unitvalue );
-my $label_width  = ( $template->{'label_width'} * $unitvalue );
-my $spine_width  = ( $template->{'label_width'} * $unitvalue );
-my $circ_width   = ( $template->{'label_width'} * $unitvalue );
-my $top_margin   = ( $template->{'topmargin'} * $unitvalue );
-my $left_margin  = ( $template->{'leftmargin'} * $unitvalue );
-my $colspace     = ( $template->{'colgap'} * $unitvalue );
-my $rowspace     = ( $template->{'rowgap'} * $unitvalue );
-
-warn "Converted dimensions are:" if $DEBUG;
-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;
-
-my $label_cols = $template->{'cols'};
-my $label_rows = $template->{'rows'};
-
-my $text_wrap_cols = GetTextWrapCols( $fontsize, $label_width );
+    else {
+        $llx = $template->get_attr('left_margin');
+        if ($row_count eq $template->get_attr('rows')) {
+            $pdf->Page();
+            $lly = ($template->get_attr('page_height') - $template->get_attr('top_margin') - $template->get_attr('label_height'));
+            $row_count = 1;
+        }
+        else {
+            $lly = ($lly - $template->get_attr('row_gap') - $template->get_attr('label_height'));
+            $row_count++;
+        }
+        $col_count = 1;
+    }
+    return ($row_count, $col_count, $llx, $lly);
+}
 
+sub _print_text {
+    my $label_text = shift;
+    foreach my $text_line (@$label_text) {
+        my $pdf_font = $pdf->Font($text_line->{'font'});
+        my $line = "BT /$pdf_font $text_line->{'font_size'} Tf $text_line->{'text_llx'} $text_line->{'text_lly'} Td ($text_line->{'line'}) Tj ET";
+        $pdf->Add($line);
+    }
+}
 
-#warn $label_cols, $label_rows;
+$| = 1;
 
 # set the paper size
 my $lowerLeftX  = 0;
 my $lowerLeftY  = 0;
-my $upperRightX = $page_width;
-my $upperRightY = $page_height;
-
-prInitVars();
-$| = 1;
-prFile();
-
-prMbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
-
-# later feature, change the font-type and size?
-prFont('C');    # Just setting a font
-prFontSize($fontsize);
-
-my $margin           = $top_margin;
-my $left_text_margin = 3;
-my $str;
-
-#warn "STARTROW = $startrow\n";
-
-#my $page_break_count = $startrow;
-my $codetype; # = 'Code39';
-
-#do page border
-# drawbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
-
-# draw margin box for alignment page
-drawbox( ($left_margin), ($top_margin), ($page_width-(2*$left_margin)), ($page_height-(2*$top_margin)) ) if $DEBUG_LPT;
-
-# Adjustments for image position and creep -fbcit
-# NOTE: *All* of these factor in to image position and creep. Keep this in mind when makeing adjustments.
-# Suggested proceedure: Adjust margins until both top and left margins are correct. Then adjust the label
-# height and width to correct label creep across and down page. Units are PostScript Points (72 per inch).
-
-warn "Active profile: $profile->{'prof_id'}" if $DEBUG;
-
-if ( $DEBUG ) {
-warn "-------------------------INITIAL VALUES-----------------------------";
-warn "top margin = $top_margin points\n";
-warn "left margin = $left_margin points\n";
-warn "label height = $label_height points\n";
-warn "label width = $label_width points\n";
-}
-
-$top_margin = $top_margin + $profile->{'offset_vert'};    #  controls vertical offset
-$label_height = $label_height + $profile->{'creep_vert'};    # controls vertical creep
-$left_margin = $left_margin + $profile->{'offset_horz'};    # controls horizontal offset
-$label_width = $label_width + $profile->{'creep_horz'};    # controls horizontal creep
-
-if ( $DEBUG ) {
-warn "-------------------------ADJUSTED VALUES-----------------------------";
-warn "top margin = $top_margin points\n";
-warn "left margin = $left_margin points\n";
-warn "label height = $label_height points\n";
-warn "label width = $label_width points\n";
-}
-
-my $item;
-my ( $i, $i2 );    # loop counters
-
-# big row loop
-
-#warn " $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY";
-#warn "$label_rows, $label_cols\n";
-#warn "$label_height, $label_width\n";
-#warn "$page_height, $page_width\n";
-
-my ( $rowcount, $colcount, $x_pos, $y_pos, $rowtemp, $coltemp );
-
-if ( $start_label eq 1 ) {
-    $rowcount = 1;
-    $colcount = 1;
-    $x_pos    = $left_margin;
-    $y_pos    = ( $page_height - $top_margin - $label_height );
-}
-
-else {
-
-    #eval {
-    $rowcount = ceil( $start_label / $label_cols );
-
-    #} ;
-    #$rowcount = 1 if $@;
-
-    $colcount = ( $start_label - ( ( $rowcount - 1 ) * $label_cols ) );
-
-    $x_pos = $left_margin + ( $label_width * ( $colcount - 1 ) ) +
-      ( $colspace * ( $colcount - 1 ) );
-
-    $y_pos = $page_height - $top_margin - ( $label_height * $rowcount ) -
-      ( $rowspace * ( $rowcount - 1 ) );
-
-    warn "Start label specified: $start_label Beginning in row $rowcount, column $colcount" if $DEBUG;
-    warn "X position = $x_pos Y position = $y_pos" if $DEBUG;
-    warn "Rowspace = $rowspace Label height = $label_height" if $DEBUG;
-}
-
-#warn "ROW COL $rowcount, $colcount";
-
-#my $barcodetype; # = 'Code39';
-
-#
-#    main foreach loop
-#
-
-foreach $item (@resultsloop) {
-    warn "Label parameters: xpos=$x_pos, ypos=$y_pos, lblwid=$label_width, lblhig=$label_height" if $DEBUG;
-    my $barcode = $item->{'barcode'};
-    if ( $printingtype eq 'BAR' ) {
-        drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
-        DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $barcode,
-            $barcodetype );
-        CalcNextLabelPos();
-    }
-    elsif ( $printingtype eq 'BARBIB' ) {
-        drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
-
-        # reposoitioning barcode up the top of label
-        my $barcode_height = ($label_height / 1.5 );    ## scaling voodoo
-        my $text_height    = $label_height / 2;
-        my $barcode_y      = $y_pos + ( $label_height / 2.5  );   ## scaling voodoo
-
-        DrawBarcode( $x_pos, $barcode_y, $barcode_height, $label_width,
-            $barcode, $barcodetype );
-        DrawSpineText( $y_pos, $text_height, $fontsize, $x_pos,
-            $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
-
-        CalcNextLabelPos();
-
-    }    # correct
-    elsif ( $printingtype eq 'BIBBAR' ) {
-        drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
-        my $barcode_height = $label_height / 2;
-        DrawBarcode( $x_pos, $y_pos, $barcode_height, $label_width, $barcode,
-            $barcodetype );
-        DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
-            $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
-
-        CalcNextLabelPos();
-    }
-
-    elsif ( $printingtype eq 'ALT' ) {
-        drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
-        DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $barcode,
-            $barcodetype );
-        CalcNextLabelPos();
-        drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
-        DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
-            $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
-
-        CalcNextLabelPos();
-    }
-
-
-    elsif ( $printingtype eq 'BIB' ) {
-        drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
-        DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
-            $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
-        CalcNextLabelPos();
+my $upperRightX = $template->get_attr('page_width');
+my $upperRightY = $template->get_attr('page_height');
+
+$pdf->Compress(1);
+$pdf->Mbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
+
+my ($row_count, $col_count, $llx, $lly) = $template->get_label_position($start_label);
+LABEL_ITEMS:
+foreach my $item (@{$batch->get_attr('items')}) {
+    my ($barcode_llx, $barcode_lly, $barcode_width, $barcode_y_scale_factor) = 0,0,0,0;
+    my $label = C4::Labels::Label->new(
+                                    batch_id            => $batch_id,
+                                    item_number         => $item->{'item_number'},
+                                    width               => $template->get_attr('label_width'),
+                                    height              => $template->get_attr('label_height'),
+                                    top_text_margin     => $template->get_attr('top_text_margin'),
+                                    left_text_margin    => $template->get_attr('left_text_margin'),
+                                    barcode_type        => $layout->get_attr('barcode_type'),
+                                    printing_type       => $layout->get_attr('printing_type'),
+                                    guidebox            => $layout->get_attr('guidebox'),
+                                    font                => $layout->get_attr('font'),
+                                    font_size           => $layout->get_attr('font_size'),
+                                    callnum_split       => $layout->get_attr('callnum_split'),
+                                    justify             => $layout->get_attr('text_justify'),
+                                    format_string       => $layout->get_attr('format_string'),
+                                    text_wrap_cols      => $layout->get_text_wrap_cols(label_width => $template->get_attr('label_width'), left_text_margin => $template->get_attr('left_text_margin')),
+                                      );
+    my $label_type = $label->get_label_type;
+    if ($label_type eq 'BIB') {
+        my $line_spacer = ($label->get_attr('font_size') * 1);    # number of pixels between text rows (This is actually leading: baseline to baseline minus font size. Recommended starting point is 20% of font size.).
+        my $text_lly = ($lly + ($template->get_attr('label_height') - $template->get_attr('top_text_margin')));
+        my $label_text = $label->draw_label_text(
+                                        llx             => $llx,
+                                        lly             => $text_lly,
+                                        line_spacer     => $line_spacer,
+                                        );
+        _print_text($label_text);
+        ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
+        next LABEL_ITEMS;
     }
-
-
-
-
-
-
-
-
-
-
-
-}    # end for item loop
-prEnd();
-
-#
-#
-#
-#
-#
-sub CalcNextLabelPos {
-    if ( $colcount lt $label_cols ) {
-
-        #        warn "new col";
-        $x_pos = ( $x_pos + $label_width + $colspace );
-        $colcount++;
+    elsif ($label_type eq 'BARBIB') {
+        $barcode_llx = $llx + $template->get_attr('left_text_margin');                             # this places the bottom left of the barcode the left text margin distance to right of the the left edge of the label ($llx)
+        $barcode_lly = ($lly + $template->get_attr('label_height')) - $template->get_attr('top_text_margin');        # this places the bottom left of the barcode the top text margin distance below the top of the label ($lly)
+        $barcode_width = 0.8 * $template->get_attr('label_width');                                 # this scales the barcode width to 80% of the label width
+        $barcode_y_scale_factor = 0.01 * $template->get_attr('label_height');                      # this scales the barcode height to 10% of the label height
+        my $line_spacer = ($label->get_attr('font_size') * 1);    # number of pixels between text rows (This is actually leading: baseline to baseline minus font size. Recommended starting point is 20% of font size.).
+        my $text_lly = ($lly + ($template->get_attr('label_height') - $template->get_attr('top_text_margin')));
+        my $label_text = $label->draw_label_text(
+                                        llx             => $llx,
+                                        lly             => $text_lly,
+                                        line_spacer     => $line_spacer,
+                                        );
+        _print_text($label_text);
     }
-
     else {
-        $x_pos = $left_margin;
-        if ( $rowcount eq $label_rows ) {
-
-            #            warn "new page";
-            prPage();
-            $y_pos    = ( $page_height - $top_margin - $label_height );
-            $rowcount = 1;
+        $barcode_llx = $llx + $template->get_attr('left_text_margin');             # this places the bottom left of the barcode the left text margin distance to right of the the left edge of the label ($llx)
+        $barcode_lly = $lly + $template->get_attr('top_text_margin');              # this places the bottom left of the barcode the top text margin distance above the bottom of the label ($lly)
+        $barcode_width = 0.8 * $template->get_attr('label_width');                 # this scales the barcode width to 80% of the label width
+        $barcode_y_scale_factor = 0.01 * $template->get_attr('label_height');      # this scales the barcode height to 10% of the label height
+        if ($label_type eq 'BIBBAR' || $label_type eq 'ALT') {
+            my $line_spacer = ($label->get_attr('font_size') * 1);    # number of pixels between text rows (This is actually leading: baseline to baseline minus font size. Recommended starting point is 20% of font size.).
+            my $text_lly = ($lly + ($template->get_attr('label_height') - $template->get_attr('top_text_margin')));
+            my $label_text = $label->draw_label_text(
+                                            llx             => $llx,
+                                            lly             => $text_lly,
+                                            line_spacer     => $line_spacer,
+                                            );
+            _print_text($label_text);
         }
-        else {
-
-            #            warn "new row";
-            $y_pos = ( $y_pos - $rowspace - $label_height );
-            $rowcount++;
+        if ($label_type eq 'ALT') {
+        ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
         }
-        $colcount = 1;
     }
+    $label->barcode(
+                llx                 => $barcode_llx,
+                lly                 => $barcode_lly,
+                width               => $barcode_width,
+                y_scale_factor      => $barcode_y_scale_factor,
+    );
+    ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
 }
 
+$pdf->End();