Bug 19802: Add stack trace to ease debugging
[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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22
23 use CGI qw ( -utf8 );
24 use C4::Auth;
25 use Graphics::Magick;
26 use XML::Simple;
27 use POSIX qw(ceil);
28 use Storable qw(dclone);
29 use autouse 'Data::Dumper' => qw(Dumper);
30
31 use C4::Debug;
32 use C4::Context;
33 use C4::Creators;
34 use C4::Patroncards;
35 use Koha::List::Patron;
36 use Koha::Patrons;
37 use Koha::Patron::Images;
38
39 my $cgi = new CGI;
40
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
42                                                                      template_name   => "labels/label-home.tt",
43                                                                      query           => $cgi,
44                                                                      type            => "intranet",
45                                                                      authnotrequired => 0,
46                                                                      flagsrequired   => { tools => 'label_creator' },
47                                                                      debug           => 1,
48                                                                      });
49 my $batch_id    = $cgi->param('batch_id') if $cgi->param('batch_id');
50 my $template_id = $cgi->param('template_id') || undef;
51 my $layout_id   = $cgi->param('layout_id') || undef;
52 my $layout_back_id   = $cgi->param('layout_back_id') || undef;
53 my $start_card = $cgi->param('start_card') || 1;
54 my @label_ids   = $cgi->multi_param('label_id') if $cgi->param('label_id');
55 my @borrower_numbers  = $cgi->multi_param('borrower_number') if $cgi->param('borrower_number');
56 my $patronlist_id = $cgi->param('patronlist_id');
57
58 my $items = undef; # items = cards
59 my $new_page = 0;
60
61 # Wrap pdf creation part into an eval, some vars need scope outside eval
62 my $pdf_ok;
63 my $pdf;
64 my $pdf_file;
65 my $cardscount = 0;
66
67 #Note fo bug 14138: Indenting follows in separate patch to ease review
68 eval {
69 $pdf_file = (@label_ids || @borrower_numbers ? "card_single_" . scalar(@label_ids || @borrower_numbers) : "card_batch_$batch_id");
70
71 $pdf = C4::Creators::PDF->new(InitVars => 0);
72 my $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
73 my $pc_template = C4::Patroncards::Template->retrieve(template_id => $template_id, profile_id => 1);
74 my $layout = C4::Patroncards::Layout->retrieve(layout_id => $layout_id);
75 my $layout_back = C4::Patroncards::Layout->retrieve(layout_id => $layout_back_id) if ( $layout_back_id );
76
77 $| = 1;
78
79 # set the paper size
80 my $lower_left_x  = 0;
81 my $lower_left_y  = 0;
82 my $upper_right_x = $pc_template->get_attr('page_width');
83 my $upper_right_y = $pc_template->get_attr('page_height');
84
85 $pdf->Compress(1); # comment this out to debug pdf files, but be sure to uncomment it in production or you may be very sorry...
86 $pdf->Mbox($lower_left_x, $lower_left_y, $upper_right_x, $upper_right_y);
87
88 my ($llx, $lly) = 0,0;
89 (undef, undef, $llx, $lly) = $pc_template->get_label_position($start_card);
90
91 if (@label_ids) {
92     my $batch_items = $batch->get_attr('items');
93     grep {
94         my $label_id = $_;
95         push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
96     } @label_ids;
97 }
98 elsif (@borrower_numbers) {
99     grep {
100         push(@{$items}, {borrower_number => $_});
101     } @borrower_numbers;
102 }
103 elsif ( $patronlist_id  ) {
104     my ($list) = GetPatronLists( { patron_list_id => $patronlist_id } );
105     my @borrowerlist = $list->patron_list_patrons()->search_related('borrowernumber')
106     ->get_column('borrowernumber')->all();
107     grep {
108         push(@{$items}, {borrower_number => $_});
109     } @borrowerlist;
110 }
111 else {
112     $items = $batch->get_attr('items');
113 }
114
115 my $layout_xml = XMLin($layout->get_attr('layout_xml'), ForceArray => 1);
116 my $layout_back_xml = XMLin($layout_back->get_attr('layout_xml'), ForceArray => 1) if ( defined $layout_back );
117
118 if ($layout_xml->{'page_side'} eq 'B') { # rearrange items on backside of page to swap columns
119     my $even = 1;
120     my $odd = 0;
121     my @swap_array = ();
122     while ($even <= (scalar(@{$items})+1)) {
123         push (@swap_array, @{$items}[$even]);
124         push (@swap_array, @{$items}[$odd]);
125         $even += 2;
126         $odd += 2;
127     }
128     @{$items} = @swap_array;
129 }
130
131 # WARNING: Referential nightmare ahead...
132
133 CARD_ITEMS:
134 foreach my $item (@{$items}) {
135     if ($item) {
136         my $print_layout_xml = (( ($cardscount % 2  == 1) && ( $layout_back_id ) ) ?
137             dclone($layout_back_xml) : dclone($layout_xml) );   # We must have a true copy of the layout xml hash, otherwise
138                                                                 # we modify the original template and very bad things happen.
139
140         $cardscount ++;
141         my $borrower_number = $item->{'borrower_number'};
142         my $card_number = Koha::Patrons->find( $borrower_number)->cardnumber;
143
144 #       Set barcode data
145         $print_layout_xml->{'barcode'}->[0]->{'data'} = $card_number if $print_layout_xml->{'barcode'};
146
147 #       Create a new patroncard object
148         my $patron_card = C4::Patroncards::Patroncard->new(
149                 batch_id                => 1,
150                 borrower_number         => $borrower_number,
151                 llx                     => $llx, # lower left corner of the card
152                 lly                     => $lly,
153                 height                  => $pc_template->get_attr('label_height'), # of the card
154                 width                   => $pc_template->get_attr('label_width'),
155                 layout                  => $print_layout_xml,
156                 text_wrap_cols          => 30, #FIXME: hardcoded,
157         );
158
159         $patron_card->draw_guide_box($pdf) if $print_layout_xml->{'guide_box'};
160         $patron_card->draw_guide_grid($pdf) if $print_layout_xml->{'guide_grid'};
161         $patron_card->draw_barcode($pdf) if $print_layout_xml->{'barcode'};
162
163 #       Do image foo and place binary image data into layout hash
164         my $image_data = {};
165         my $error = undef;
166         my $images = $print_layout_xml->{'images'};
167         PROCESS_IMAGES:
168         foreach my $card_image (sort(keys %{$images})) {
169             if (grep{m/(source)/} keys(%{$images->{$card_image}->{'data_source'}->[0]})) {
170                 if ($images->{$card_image}->{'data_source'}->[0]->{'image_source'} eq 'none') {
171                 }
172                 elsif ($images->{$card_image}->{'data_source'}->[0]->{'image_source'} eq 'patronimages') {
173                     my $patron_image = Koha::Patron::Images->find($borrower_number);
174                     if ($patron_image) {
175                         $image_data->{'imagefile'} = $patron_image->imagefile;
176                     }
177                     else {
178                         warn sprintf('No image exists for borrower number %s.', $borrower_number);
179                     }
180                 }
181                 elsif ($images->{$card_image}->{'data_source'}->[0]->{'image_source'} eq 'creator_images') {
182                     ## FIXME: The DB stuff here needs to be religated to a Koha::Creator::Images object -chris_n
183                     my $dbh = C4::Context->dbh();
184                     $dbh->{LongReadLen} = 1000000;      # allows us to read approx 1MB
185                     $image_data = $dbh->selectrow_hashref("SELECT imagefile FROM creator_images WHERE image_name = \'$images->{$card_image}->{'data_source'}->[0]->{'image_name'}\'");
186                     warn sprintf('Database returned the following error: %s.', $error) if $error;
187                     unless($image_data){
188                         warn sprintf('Image does not exists in db table %s.', $images->{$card_image}->{'data_source'}->[0]->{'image_name'});
189                     }
190                 }
191                 else {
192                     warn sprintf('No retrieval method for image source %s.', $images->{$card_image}->{'data_source'}->[0]->{'image_source'});
193                 }
194             }
195             else {
196                 warn sprintf("Unrecognized image data source: %s", $images->{$card_image}->{'data_source'});
197             }
198
199         my $binary_data = $image_data->{'imagefile'} || next PROCESS_IMAGES;
200
201 #       invoke the display image object...
202         my $image = Graphics::Magick->new;
203         $image->BlobToImage($binary_data);
204
205 #       invoke the alt (aka print) image object...
206         my $alt_image = Graphics::Magick->new;
207         $alt_image->BlobToImage($binary_data);
208         $alt_image->Set(magick => 'jpg', quality => 100);
209
210         #To avoid pixelation have the image 5 times bigger and
211         #scale it down in PDF itself
212         my $oversize_factor = 8;
213         my $pdf_scale_factor = 1 / $oversize_factor;
214
215         my $alt_width = ceil($image->Get('width')); # the rounding up is important: Adobe reader does not handle long decimal numbers well
216         my $alt_height = ceil($image->Get('height'));
217         my $ratio = $alt_width / $alt_height;
218         my $display_height = ceil($images->{$card_image}->{'Dx'});
219         my $display_width = ceil($ratio * $display_height);
220
221
222         $image->Resize(width => $oversize_factor * $display_width, height => $oversize_factor * $display_height);
223         $image->Set(magick => 'jpg', quality => 100);
224
225 #       Write param for downsizing in pdf
226             $images->{$card_image}->{'scale'} = $pdf_scale_factor;
227
228 #       Write params for alt image...
229             $images->{$card_image}->{'alt'}->{'Sx'} = $oversize_factor * $alt_width;
230             $images->{$card_image}->{'alt'}->{'Sy'} = $oversize_factor * $alt_height;
231             $images->{$card_image}->{'alt'}->{'data'} = $alt_image->ImageToBlob();
232
233 #       Write params for display image...
234             $images->{$card_image}->{'Sx'} = $oversize_factor * $display_width;
235             $images->{$card_image}->{'Sy'} = $oversize_factor * $display_height;
236             $images->{$card_image}->{'data'} = $image->ImageToBlob();
237
238             my $err = $patron_card->draw_image($pdf);
239             warn sprintf ("Error encountered while attempting to draw image %s, %s", $card_image, $err) if $err;
240             # Destroy all Graphics::Magick objects and related references
241             # or bad things will happen.
242             undef $image;
243             undef $alt_image;
244             undef $binary_data;
245         }
246         $patron_card->draw_text($pdf);
247     }
248     ($llx, $lly, $new_page) = $pc_template->get_next_label_pos();
249
250     if ( ($cardscount % 2  == 1) && ( $layout_back_id ) ) {
251         $pdf->Page();
252         redo; # Use same patron data again for backside in card printer
253     }
254
255     $pdf->Page() if $new_page;
256 }
257 # No errors occurred within eval, we can issue the pdf
258 $pdf_ok = 1 if ($cardscount > 0);
259 }; # end of eval block
260
261 if ($pdf_ok) {
262     #issue the pdf
263     print $cgi->header( -type       => 'application/pdf',
264                     -encoding   => 'utf-8',
265                     -attachment => "$pdf_file.pdf",
266                   );
267     $pdf->End();
268 }
269 else {
270     # warn user that pdf is not created
271     my $errparams = '&pdferr=1';
272     $errparams .= "&errba=$batch_id" if $batch_id;
273     $errparams .= "&errpl=$patronlist_id" if $patronlist_id;
274     $errparams =  $errparams.'&errpt='.$cgi->param('borrower_number') if $cgi->param('borrower_number');
275     $errparams .= "&errlo=$layout_id" if $layout_id;
276     $errparams .= "&errtpl=$template_id" if $template_id;
277     $errparams .= "&errnocards=1" if !$cardscount;
278
279     print $cgi->redirect("/cgi-bin/koha/patroncards/manage.pl?card_element=batch$errparams");
280 }
281
282 1;