3 # Copyright 2009 Foundations Bible College.
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 use autouse 'Data::Dumper' => qw(Dumper);
32 use autouse 'C4::Members' => qw(GetPatronImage GetMember);
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
39 template_name => "labels/label-home.tt",
43 flagsrequired => { tools => 'label_creator' },
48 my $batch_id = $cgi->param('batch_id') if $cgi->param('batch_id');
49 my $template_id = $cgi->param('template_id') || undef;
50 my $layout_id = $cgi->param('layout_id') || undef;
51 my $start_label = $cgi->param('start_label') || 1;
52 my @label_ids = $cgi->param('label_id') if $cgi->param('label_id');
53 my @borrower_numbers = $cgi->param('borrower_number') if $cgi->param('borrower_number');
55 my $items = undef; # items = cards
58 my $pdf_file = (@label_ids || @borrower_numbers ? "card_single_" . scalar(@label_ids || @borrower_numbers) : "card_batch_$batch_id");
59 print $cgi->header( -type => 'application/pdf',
61 -attachment => "$pdf_file.pdf",
64 my $pdf = C4::Creators::PDF->new(InitVars => 0);
65 my $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
66 my $template = C4::Patroncards::Template->retrieve(template_id => $template_id, profile_id => 1);
67 my $layout = C4::Patroncards::Layout->retrieve(layout_id => $layout_id);
74 my $upper_right_x = $template->get_attr('page_width');
75 my $upper_right_y = $template->get_attr('page_height');
77 $pdf->Compress(1); # comment this out to debug pdf files, but be sure to uncomment it in production or you may be very sorry...
78 $pdf->Mbox($lower_left_x, $lower_left_y, $upper_right_x, $upper_right_y);
80 my ($llx, $lly) = 0,0;
81 (undef, undef, $llx, $lly) = $template->get_label_position($start_label);
84 my $batch_items = $batch->get_attr('items');
87 push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
90 elsif (@borrower_numbers) {
92 push(@{$items}, {item_number => $_});
96 $items = $batch->get_attr('items');
99 my $layout_xml = XMLin($layout->get_attr('layout_xml'), ForceArray => 1);
101 if ($layout_xml->{'page_side'} eq 'B') { # rearrange items on backside of page to swap columns
105 while ($even <= (scalar(@{$items})+1)) {
106 push (@swap_array, @{$items}[$even]);
107 push (@swap_array, @{$items}[$odd]);
111 @{$items} = @swap_array;
115 foreach my $item (@{$items}) {
117 my $borrower_number = $item->{'borrower_number'};
118 my $card_number = GetMember(borrowernumber => $borrower_number)->{'cardnumber'};
121 $layout_xml->{'barcode'}->[0]->{'data'} = $card_number if $layout_xml->{'barcode'};
123 # Create a new patroncard object
124 my $patron_card = C4::Patroncards::Patroncard->new(
126 borrower_number => $borrower_number,
127 llx => $llx, # lower left corner of the card
129 height => $template->get_attr('label_height'), # of the card
130 width => $template->get_attr('label_width'),
131 layout => $layout_xml,
132 text_wrap_cols => 30, #FIXME: hardcoded
134 $patron_card->draw_guide_box($pdf) if $layout_xml->{'guide_box'};
135 $patron_card->draw_barcode($pdf) if $layout_xml->{'barcode'};
137 # Do image foo and place binary image data into layout hash
140 my $images = $layout_xml->{'images'};
142 foreach (keys %{$images}) {
143 if (grep{m/source/} keys(%{$images->{$_}->{'data_source'}->[0]})) {
144 if ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'none') {
147 elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'patronimages') {
148 ($image_data, $error) = GetPatronImage($borrower_number);
149 warn sprintf('No image exists for borrower number %s.', $borrower_number) if !$image_data;
150 next PROCESS_IMAGES if !$image_data;
152 elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'creator_images') {
153 my $dbh = C4::Context->dbh();
154 $dbh->{LongReadLen} = 1000000; # allows us to read approx 1MB
155 $image_data = $dbh->selectrow_hashref("SELECT imagefile FROM creator_images WHERE image_name = \'$images->{$_}->{'data_source'}->[0]->{'image_name'}\'");
156 warn sprintf('Database returned the following error: %s.', $error) if $error;
157 warn sprintf('Image does not exists in db table %s.', $images->{$_}->{'data_source'}->[0]->{'image_name'}) if !$image_data;
158 next PROCESS_IMAGES if !$image_data;
161 warn sprintf('No retrieval method for image source %s.', $images->{$_}->{'data_source'}->[0]->{'image_source'});
166 warn sprintf("Unrecognized image data source: %s", $images->{$_}->{'data_source'});
170 my $binary_data = $image_data->{'imagefile'};
172 # invoke the display image object...
173 my $image = Graphics::Magick->new;
174 $image->BlobToImage($binary_data);
176 # invoke the alt (aka print) image object...
177 my $alt_image = Graphics::Magick->new;
178 $alt_image->BlobToImage($binary_data);
179 $alt_image->Set(magick => 'jpg', quality => 100);
181 my $alt_width = ceil($image->Get('width')); # the rounding up is important: Adobe reader does not handle long decimal numbers well
182 my $alt_height = ceil($image->Get('height'));
183 my $ratio = $alt_width / $alt_height;
184 my $display_height = ceil($images->{$_}->{'Dx'});
185 my $display_width = ceil($ratio * $display_height);
188 $image->Resize(width => $display_width, height => $display_height);
189 $image->Set(magick => 'jpg', quality => 100);
191 # Write params for alt image...
192 $images->{$_}->{'alt'}->{'Sx'} = $alt_width;
193 $images->{$_}->{'alt'}->{'Sy'} = $alt_height;
194 $images->{$_}->{'alt'}->{'data'} = $alt_image->ImageToBlob();
196 # Write params for display image...
197 $images->{$_}->{'Sx'} = $display_width;
198 $images->{$_}->{'Sy'} = $display_height;
199 $images->{$_}->{'data'} = $image->ImageToBlob();
201 my $err = $patron_card->draw_image($pdf);
202 warn sprintf ("Error encountered while attempting to draw image %s, %s", $_, $err) if $err;
204 $patron_card->draw_text($pdf);
206 ($llx, $lly, $new_page) = $template->get_next_label_pos();
207 $pdf->Page() if $new_page;
212 # FIXME: Possibly do a redirect here if there were error encountered during PDF creation.