Bugfix: Correcting hash dereferencing for image data
[koha.git] / patroncards / create-pdf.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2009 Foundations Bible College.
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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
19
20 use strict;
21 use warnings;
22
23 use CGI;
24 use Graphics::Magick;
25 use XML::Simple;
26 use POSIX qw(ceil);
27 use autouse 'Data::Dumper' => qw(Dumper);
28
29 use C4::Debug;
30 use C4::Context;
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;
37
38 my $cgi = new CGI;
39
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');
46
47 my $items = undef;      # items = cards
48
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',
51                     -encoding   => 'utf-8',
52                     -attachment => "$pdf_file.pdf",
53                   );
54
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);
59
60 $| = 1;
61
62 # set the paper size
63 my $lower_left_x  = 0;
64 my $lower_left_y  = 0;
65 my $upper_right_x = $template->get_attr('page_width');
66 my $upper_right_y = $template->get_attr('page_height');
67
68 $pdf->Compress(1);
69 $pdf->Mbox($lower_left_x, $lower_left_y, $upper_right_x, $upper_right_y);
70
71 my ($llx, $lly) = 0,0;
72 (undef, undef, $llx, $lly) = $template->get_label_position($start_label);
73
74 if (@label_ids) {
75     my $batch_items = $batch->get_attr('items');
76     grep {
77         my $label_id = $_;
78         push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
79     } @label_ids;
80 }
81 elsif (@borrower_numbers) {
82     grep {
83         push(@{$items}, {item_number => $_});
84     } @borrower_numbers;
85 }
86 else {
87     $items = $batch->get_attr('items');
88 }
89
90 my $layout_xml = XMLin($layout->get_attr('layout_xml'), ForceArray => 1);
91
92 if ($layout_xml->{'page_side'} eq 'B') { # rearrange items on backside of page to swap columns
93     my $even = 1;
94     my $odd = 0;
95     my @swap_array = ();
96     while ($even <= (scalar(@{$items})+1)) {
97         push (@swap_array, @{$items}[$even]);
98         push (@swap_array, @{$items}[$odd]);
99         $even += 2;
100         $odd += 2;
101     }
102     @{$items} = @swap_array;
103 }
104
105 CARD_ITEMS:
106 foreach my $item (@{$items}) {
107     my $new_page = 0; #FIXME: this needs to be implimented or removed
108     if ($item) {
109         my $borrower_number = $item->{'borrower_number'};
110         my $card_number = GetMember(borrowernumber => $borrower_number)->{'cardnumber'};
111
112 #       Set barcode data
113         $layout_xml->{'barcode'}->{'data'} = $card_number if $layout_xml->{'barcode'};
114
115 #       Create a new patroncard object
116         my $patron_card = C4::Patroncards::Patroncard->new(
117                 batch_id                => 1,
118                 borrower_number         => $borrower_number,
119                 llx                     => $llx, # lower left corner of the card
120                 lly                     => $lly,
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
125         );
126         $patron_card->draw_guide_box($pdf);
127         $patron_card->draw_barcode($pdf) if $layout_xml->{'barcode'};
128
129 #       Do image foo and place binary image data into layout hash
130         my $image_data = {};
131         my $error = undef;
132         my $images = $layout_xml->{'images'};
133         PROCESS_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') {
137                     next PROCESS_IMAGES;
138                 }
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;
143                 }
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;
151                 }
152                 else {
153                     warn sprintf('No retrieval method for image source %s.', $$layout_xml{'images'}{$_}{'data_source'}{'image_source'});
154                     next PROCESS_IMAGES;
155                 }
156             }
157             else {
158                 warn sprintf("Unrecognized image data source: %s", $images->{$_}->{'data_source'});
159                 next PROCESS_IMAGES;
160             }
161
162         my $binary_data = $image_data->{'imagefile'};
163
164 #       invoke the display image object...
165         my $image = Graphics::Magick->new;
166         $image->BlobToImage($binary_data);
167
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);
172
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);
178
179
180         $image->Resize(width => $display_width, height => $display_height);
181         $image->Set(magick => 'jpg', quality => 100);
182
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();
187
188 #       Write params for display image...
189             $images->{$_}->{'Sx'} = $display_width;
190             $images->{$_}->{'Sy'} = $display_height;
191             $images->{$_}->{'data'} = $image->ImageToBlob();
192
193             my $err = $patron_card->draw_image($pdf);
194             warn sprintf ("Error encountered while attempting to draw image %s, %s", $_, $err) if $err;
195         }
196         $patron_card->draw_text($pdf);
197     }
198     ($llx, $lly, $new_page) = $template->get_next_label_pos();
199     #$pdf->Page() if $new_page;
200 }
201
202 $pdf->End();
203
204 # FIXME: Possibly do a redirect here if there were error encountered during PDF creation.
205
206 exit 0;