Remove an invalid db update
[koha.git] / patroncards / create-pdf.pl
blob3e7607f77ccd1d8e1b7de187ca565e8ede1d373c
1 #!/usr/bin/perl
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
10 # version.
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
20 use strict;
21 use warnings;
23 use CGI;
24 use Graphics::Magick;
25 use XML::Simple;
26 use POSIX qw(ceil);
27 use autouse 'Data::Dumper' => qw(Dumper);
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;
38 my $cgi = new CGI;
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',
51 -encoding => 'utf-8',
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);
60 $| = 1;
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');
68 $pdf->Compress(1);
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);
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;
81 elsif (@borrower_numbers) {
82 grep {
83 push(@{$items}, {item_number => $_});
84 } @borrower_numbers;
86 else {
87 $items = $batch->get_attr('items');
90 my $layout_xml = XMLin($layout->get_attr('layout_xml'));
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;
102 @{$items} = @swap_array;
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'};
112 # Set barcode data
113 $layout_xml->{'barcode'}->{'data'} = $card_number if $layout_xml->{'barcode'};
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
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
130 my $image_data = {};
131 my $error = undef;
132 PROCESS_IMAGES:
133 foreach (keys %{$layout_xml->{'images'}}) {
134 if (grep{m/source/} keys(%{$layout_xml->{'images'}->{$_}->{'data_source'}})) {
135 if ($layout_xml->{'images'}->{$_}->{'data_source'}->{'image_source'} eq 'none') {
136 next PROCESS_IMAGES;
138 elsif ($layout_xml->{'images'}->{$_}->{'data_source'}->{'image_source'} eq 'patronimages') {
139 ($image_data, $error) = GetPatronImage($card_number);
140 warn sprintf('No image exists for borrower number %s.', $borrower_number) if !$image_data;
141 next PROCESS_IMAGES if !$image_data;
143 elsif ($layout_xml->{'images'}->{$_}->{'data_source'}->{'image_source'} eq 'creator_images') {
144 my $dbh = C4::Context->dbh();
145 $dbh->{LongReadLen} = 1000000; # allows us to read approx 1MB
146 $image_data = $dbh->selectrow_hashref("SELECT imagefile FROM creator_images WHERE image_name = \'$$layout_xml{'images'}{$_}{'data_source'}{'image_name'}\'");
147 warn sprintf('Database returned the following error: %s.', $error) if $error;
148 warn sprintf('Image does not exists in db table %s.', $$layout_xml{'images'}{$_}{'data_source'}{'image_source'}) if !$image_data;
149 next PROCESS_IMAGES if !$image_data;
151 else {
152 warn sprintf('No retrieval method for image source %s.', $$layout_xml{'images'}{$_}{'data_source'}{'image_source'});
153 next PROCESS_IMAGES;
156 else {
157 warn sprintf("Unrecognized image data source: %s", $layout_xml->{'images'}->{$_}->{'data_source'});
158 next PROCESS_IMAGES;
161 my $binary_data = $image_data->{'imagefile'};
163 # invoke the display image object...
164 my $image = Graphics::Magick->new;
165 $image->BlobToImage($binary_data);
167 # invoke the alt (aka print) image object...
168 my $alt_image = Graphics::Magick->new;
169 $alt_image->BlobToImage($binary_data);
170 $alt_image->Set(magick => 'jpg', quality => 100);
172 my $alt_width = ceil($image->Get('width')); # the rounding up is important: Adobe reader does not handle long decimal numbers well
173 my $alt_height = ceil($image->Get('height'));
174 my $ratio = $alt_width / $alt_height;
175 my $display_height = ceil($layout_xml->{'images'}->{$_}->{'Dx'});
176 my $display_width = ceil($ratio * $display_height);
179 $image->Resize(width => $display_width, height => $display_height);
180 $image->Set(magick => 'jpg', quality => 100);
182 # Write params for alt image...
183 $layout_xml->{'images'}->{$_}->{'alt'}->{'Sx'} = $alt_width;
184 $layout_xml->{'images'}->{$_}->{'alt'}->{'Sy'} = $alt_height;
185 $layout_xml->{'images'}->{$_}->{'alt'}->{'data'} = $alt_image->ImageToBlob();
187 # Write params for display image...
188 $layout_xml->{'images'}->{$_}->{'Sx'} = $display_width;
189 $layout_xml->{'images'}->{$_}->{'Sy'} = $display_height;
190 $layout_xml->{'images'}->{$_}->{'data'} = $image->ImageToBlob();
192 my $err = $patron_card->draw_image($pdf);
193 warn sprintf ("Error encountered while attempting to draw image %s, %s", $_, $err) if $err;
195 $patron_card->draw_text($pdf);
197 ($llx, $lly, $new_page) = $template->get_next_label_pos();
198 #$pdf->Page() if $new_page;
201 $pdf->End();
203 # FIXME: Possibly do a redirect here if there were error encountered during PDF creation.
205 exit 0;