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.
27 use autouse 'Data::Dumper' => qw(Dumper);
31 use autouse 'C4::Members' => qw(GetPatronImage GetMember);
32 use C4::Creators 1.000000;
33 use C4::Patroncards 1.000000;
37 my $batch_id = $cgi->param('batch_id') if $cgi->param('batch_id');
38 my $template_id = $cgi->param('template_id') || undef;
39 my $layout_id = $cgi->param('layout_id') || undef;
40 my $start_label = $cgi->param('start_label') || 1;
41 my @label_ids = $cgi->param('label_id') if $cgi->param('label_id');
42 my @borrower_numbers = $cgi->param('borrower_number') if $cgi->param('borrower_number');
44 my $items = undef; # items = cards
47 my $pdf_file = (@label_ids || @borrower_numbers ? "card_single_" . scalar(@label_ids || @borrower_numbers) : "card_batch_$batch_id");
48 print $cgi->header( -type => 'application/pdf',
50 -attachment => "$pdf_file.pdf",
53 my $pdf = C4::Creators::PDF->new(InitVars => 0);
54 my $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
55 my $template = C4::Patroncards::Template->retrieve(template_id => $template_id, profile_id => 1);
56 my $layout = C4::Patroncards::Layout->retrieve(layout_id => $layout_id);
63 my $upper_right_x = $template->get_attr('page_width');
64 my $upper_right_y = $template->get_attr('page_height');
66 $pdf->Compress(1); # comment this out to debug pdf files, but be sure to uncomment it in production or you may be very sorry...
67 $pdf->Mbox($lower_left_x, $lower_left_y, $upper_right_x, $upper_right_y);
69 my ($llx, $lly) = 0,0;
70 (undef, undef, $llx, $lly) = $template->get_label_position($start_label);
73 my $batch_items = $batch->get_attr('items');
76 push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
79 elsif (@borrower_numbers) {
81 push(@{$items}, {item_number => $_});
85 $items = $batch->get_attr('items');
88 my $layout_xml = XMLin($layout->get_attr('layout_xml'), ForceArray => 1);
90 if ($layout_xml->{'page_side'} eq 'B') { # rearrange items on backside of page to swap columns
94 while ($even <= (scalar(@{$items})+1)) {
95 push (@swap_array, @{$items}[$even]);
96 push (@swap_array, @{$items}[$odd]);
100 @{$items} = @swap_array;
104 foreach my $item (@{$items}) {
106 my $borrower_number = $item->{'borrower_number'};
107 my $card_number = GetMember(borrowernumber => $borrower_number)->{'cardnumber'};
110 $layout_xml->{'barcode'}->[0]->{'data'} = $card_number if $layout_xml->{'barcode'};
112 # Create a new patroncard object
113 my $patron_card = C4::Patroncards::Patroncard->new(
115 borrower_number => $borrower_number,
116 llx => $llx, # lower left corner of the card
118 height => $template->get_attr('label_height'), # of the card
119 width => $template->get_attr('label_width'),
120 layout => $layout_xml,
121 text_wrap_cols => 30, #FIXME: hardcoded
123 $patron_card->draw_guide_box($pdf) if $layout_xml->{'guide_box'};
124 $patron_card->draw_barcode($pdf) if $layout_xml->{'barcode'};
126 # Do image foo and place binary image data into layout hash
129 my $images = $layout_xml->{'images'};
131 foreach (keys %{$images}) {
132 if (grep{m/source/} keys(%{$images->{$_}->{'data_source'}->[0]})) {
133 if ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'none') {
136 elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'patronimages') {
137 ($image_data, $error) = GetPatronImage($card_number);
138 warn sprintf('No image exists for borrower number %s.', $borrower_number) if !$image_data;
139 next PROCESS_IMAGES if !$image_data;
141 elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'creator_images') {
142 my $dbh = C4::Context->dbh();
143 $dbh->{LongReadLen} = 1000000; # allows us to read approx 1MB
144 $image_data = $dbh->selectrow_hashref("SELECT imagefile FROM creator_images WHERE image_name = \'$images->{$_}->{'data_source'}->[0]->{'image_name'}\'");
145 warn sprintf('Database returned the following error: %s.', $error) if $error;
146 warn sprintf('Image does not exists in db table %s.', $images->{$_}->{'data_source'}->[0]->{'image_name'}) if !$image_data;
147 next PROCESS_IMAGES if !$image_data;
150 warn sprintf('No retrieval method for image source %s.', $images->{$_}->{'data_source'}->[0]->{'image_source'});
155 warn sprintf("Unrecognized image data source: %s", $images->{$_}->{'data_source'});
159 my $binary_data = $image_data->{'imagefile'};
161 # invoke the display image object...
162 my $image = Graphics::Magick->new;
163 $image->BlobToImage($binary_data);
165 # invoke the alt (aka print) image object...
166 my $alt_image = Graphics::Magick->new;
167 $alt_image->BlobToImage($binary_data);
168 $alt_image->Set(magick => 'jpg', quality => 100);
170 my $alt_width = ceil($image->Get('width')); # the rounding up is important: Adobe reader does not handle long decimal numbers well
171 my $alt_height = ceil($image->Get('height'));
172 my $ratio = $alt_width / $alt_height;
173 my $display_height = ceil($images->{$_}->{'Dx'});
174 my $display_width = ceil($ratio * $display_height);
177 $image->Resize(width => $display_width, height => $display_height);
178 $image->Set(magick => 'jpg', quality => 100);
180 # Write params for alt image...
181 $images->{$_}->{'alt'}->{'Sx'} = $alt_width;
182 $images->{$_}->{'alt'}->{'Sy'} = $alt_height;
183 $images->{$_}->{'alt'}->{'data'} = $alt_image->ImageToBlob();
185 # Write params for display image...
186 $images->{$_}->{'Sx'} = $display_width;
187 $images->{$_}->{'Sy'} = $display_height;
188 $images->{$_}->{'data'} = $image->ImageToBlob();
190 my $err = $patron_card->draw_image($pdf);
191 warn sprintf ("Error encountered while attempting to draw image %s, %s", $_, $err) if $err;
193 $patron_card->draw_text($pdf);
195 ($llx, $lly, $new_page) = $template->get_next_label_pos();
196 $pdf->Page() if $new_page;
201 # FIXME: Possibly do a redirect here if there were error encountered during PDF creation.