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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
27 use autouse 'Data::Dumper' => qw(Dumper);
31 use autouse 'C4::Members' => qw(GetPatronImage GetMember);
32 use C4::Creators::PDF 1.000000;
33 use C4::Patroncards::Batch 1.000000;
34 use C4::Patroncards::Template 1.000000;
35 use C4::Patroncards::Layout 1.000000;
36 use C4::Patroncards::Patroncard 1.000000;
40 my $batch_id = $cgi->param('batch_id') if $cgi->param('batch_id');
41 my $template_id = $cgi->param('template_id') || undef;
42 my $layout_id = $cgi->param('layout_id') || undef;
43 my $start_label = $cgi->param('start_label') || 1;
44 my @label_ids = $cgi->param('label_id') if $cgi->param('label_id');
45 my @borrower_numbers = $cgi->param('borrower_number') if $cgi->param('borrower_number');
47 my $items = undef; # items = cards
49 my $pdf_file = (@label_ids || @borrower_numbers ? "card_single_" . scalar(@label_ids || @borrower_numbers) : "card_batch_$batch_id");
50 print $cgi->header( -type => 'application/pdf',
52 -attachment => "$pdf_file.pdf",
55 my $pdf = C4::Creators::PDF->new(InitVars => 0);
56 my $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
57 my $template = C4::Patroncards::Template->retrieve(template_id => $template_id, profile_id => 1);
58 my $layout = C4::Patroncards::Layout->retrieve(layout_id => $layout_id);
65 my $upper_right_x = $template->get_attr('page_width');
66 my $upper_right_y = $template->get_attr('page_height');
68 $pdf->Compress(1); # comment this out to debug pdf files, but be sure to uncomment it in production or you may be very sorry...
69 $pdf->Mbox($lower_left_x, $lower_left_y, $upper_right_x, $upper_right_y);
71 my ($llx, $lly) = 0,0;
72 (undef, undef, $llx, $lly) = $template->get_label_position($start_label);
75 my $batch_items = $batch->get_attr('items');
78 push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
81 elsif (@borrower_numbers) {
83 push(@{$items}, {item_number => $_});
87 $items = $batch->get_attr('items');
90 my $layout_xml = XMLin($layout->get_attr('layout_xml'), ForceArray => 1);
92 if ($layout_xml->{'page_side'} eq 'B') { # rearrange items on backside of page to swap columns
96 while ($even <= (scalar(@{$items})+1)) {
97 push (@swap_array, @{$items}[$even]);
98 push (@swap_array, @{$items}[$odd]);
102 @{$items} = @swap_array;
106 foreach my $item (@{$items}) {
107 my $new_page = 0; #FIXME: this needs to be implimented or removed
109 my $borrower_number = $item->{'borrower_number'};
110 my $card_number = GetMember(borrowernumber => $borrower_number)->{'cardnumber'};
113 $layout_xml->{'barcode'}->[0]->{'data'} = $card_number if $layout_xml->{'barcode'};
115 # Create a new patroncard object
116 my $patron_card = C4::Patroncards::Patroncard->new(
118 borrower_number => $borrower_number,
119 llx => $llx, # lower left corner of the card
121 height => $template->get_attr('label_height'), # of the card
122 width => $template->get_attr('label_width'),
123 layout => $layout_xml,
124 text_wrap_cols => 30, #FIXME: hardcoded
126 $patron_card->draw_guide_box($pdf);
127 $patron_card->draw_barcode($pdf) if $layout_xml->{'barcode'};
129 # Do image foo and place binary image data into layout hash
132 my $images = $layout_xml->{'images'};
134 foreach (keys %{$images}) {
135 if (grep{m/source/} keys(%{$images->{$_}->{'data_source'}->[0]})) {
136 if ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'none') {
139 elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'patronimages') {
140 ($image_data, $error) = GetPatronImage($card_number);
141 warn sprintf('No image exists for borrower number %s.', $borrower_number) if !$image_data;
142 next PROCESS_IMAGES if !$image_data;
144 elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'creator_images') {
145 my $dbh = C4::Context->dbh();
146 $dbh->{LongReadLen} = 1000000; # allows us to read approx 1MB
147 $image_data = $dbh->selectrow_hashref("SELECT imagefile FROM creator_images WHERE image_name = \'$$layout_xml{'images'}{$_}{'data_source'}{'image_name'}\'");
148 warn sprintf('Database returned the following error: %s.', $error) if $error;
149 warn sprintf('Image does not exists in db table %s.', $$layout_xml{'images'}{$_}{'data_source'}{'image_source'}) if !$image_data;
150 next PROCESS_IMAGES if !$image_data;
153 warn sprintf('No retrieval method for image source %s.', $$layout_xml{'images'}{$_}{'data_source'}{'image_source'});
158 warn sprintf("Unrecognized image data source: %s", $images->{$_}->{'data_source'});
162 my $binary_data = $image_data->{'imagefile'};
164 # invoke the display image object...
165 my $image = Graphics::Magick->new;
166 $image->BlobToImage($binary_data);
168 # invoke the alt (aka print) image object...
169 my $alt_image = Graphics::Magick->new;
170 $alt_image->BlobToImage($binary_data);
171 $alt_image->Set(magick => 'jpg', quality => 100);
173 my $alt_width = ceil($image->Get('width')); # the rounding up is important: Adobe reader does not handle long decimal numbers well
174 my $alt_height = ceil($image->Get('height'));
175 my $ratio = $alt_width / $alt_height;
176 my $display_height = ceil($images->{$_}->{'Dx'});
177 my $display_width = ceil($ratio * $display_height);
180 $image->Resize(width => $display_width, height => $display_height);
181 $image->Set(magick => 'jpg', quality => 100);
183 # Write params for alt image...
184 $images->{$_}->{'alt'}->{'Sx'} = $alt_width;
185 $images->{$_}->{'alt'}->{'Sy'} = $alt_height;
186 $images->{$_}->{'alt'}->{'data'} = $alt_image->ImageToBlob();
188 # Write params for display image...
189 $images->{$_}->{'Sx'} = $display_width;
190 $images->{$_}->{'Sy'} = $display_height;
191 $images->{$_}->{'data'} = $image->ImageToBlob();
193 my $err = $patron_card->draw_image($pdf);
194 warn sprintf ("Error encountered while attempting to draw image %s, %s", $_, $err) if $err;
196 $patron_card->draw_text($pdf);
198 ($llx, $lly, $new_page) = $template->get_next_label_pos();
199 #$pdf->Page() if $new_page;
204 # FIXME: Possibly do a redirect here if there were error encountered during PDF creation.