Bug 7284 follow-up, DBrev number
[koha.git] / patroncards / create-pdf.pl
blob961dfbfc055e9cf90abbcb38e97997921102d966
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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 use warnings;
23 use CGI;
24 use C4::Auth;
25 use Graphics::Magick;
26 use XML::Simple;
27 use POSIX qw(ceil);
28 use autouse 'Data::Dumper' => qw(Dumper);
30 use C4::Debug;
31 use C4::Context;
32 use autouse 'C4::Members' => qw(GetPatronImage GetMember);
33 use C4::Creators 1.000000;
34 use C4::Patroncards 1.000000;
36 my $cgi = new CGI;
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
39 template_name => "labels/label-home.tt",
40 query => $cgi,
41 type => "intranet",
42 authnotrequired => 0,
43 flagsrequired => { tools => 'label_creator' },
44 debug => 1,
45 });
48 my $batch_id = $cgi->param('batch_id') if $cgi->param('batch_id');
49 my $template_id = $cgi->param('template_id') || undef;
50 my $layout_id = $cgi->param('layout_id') || undef;
51 my $start_label = $cgi->param('start_label') || 1;
52 my @label_ids = $cgi->param('label_id') if $cgi->param('label_id');
53 my @borrower_numbers = $cgi->param('borrower_number') if $cgi->param('borrower_number');
55 my $items = undef; # items = cards
56 my $new_page = 0;
58 my $pdf_file = (@label_ids || @borrower_numbers ? "card_single_" . scalar(@label_ids || @borrower_numbers) : "card_batch_$batch_id");
59 print $cgi->header( -type => 'application/pdf',
60 -encoding => 'utf-8',
61 -attachment => "$pdf_file.pdf",
64 my $pdf = C4::Creators::PDF->new(InitVars => 0);
65 my $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
66 my $template = C4::Patroncards::Template->retrieve(template_id => $template_id, profile_id => 1);
67 my $layout = C4::Patroncards::Layout->retrieve(layout_id => $layout_id);
69 $| = 1;
71 # set the paper size
72 my $lower_left_x = 0;
73 my $lower_left_y = 0;
74 my $upper_right_x = $template->get_attr('page_width');
75 my $upper_right_y = $template->get_attr('page_height');
77 $pdf->Compress(1); # comment this out to debug pdf files, but be sure to uncomment it in production or you may be very sorry...
78 $pdf->Mbox($lower_left_x, $lower_left_y, $upper_right_x, $upper_right_y);
80 my ($llx, $lly) = 0,0;
81 (undef, undef, $llx, $lly) = $template->get_label_position($start_label);
83 if (@label_ids) {
84 my $batch_items = $batch->get_attr('items');
85 grep {
86 my $label_id = $_;
87 push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
88 } @label_ids;
90 elsif (@borrower_numbers) {
91 grep {
92 push(@{$items}, {item_number => $_});
93 } @borrower_numbers;
95 else {
96 $items = $batch->get_attr('items');
99 my $layout_xml = XMLin($layout->get_attr('layout_xml'), ForceArray => 1);
101 if ($layout_xml->{'page_side'} eq 'B') { # rearrange items on backside of page to swap columns
102 my $even = 1;
103 my $odd = 0;
104 my @swap_array = ();
105 while ($even <= (scalar(@{$items})+1)) {
106 push (@swap_array, @{$items}[$even]);
107 push (@swap_array, @{$items}[$odd]);
108 $even += 2;
109 $odd += 2;
111 @{$items} = @swap_array;
114 CARD_ITEMS:
115 foreach my $item (@{$items}) {
116 if ($item) {
117 my $borrower_number = $item->{'borrower_number'};
118 my $card_number = GetMember(borrowernumber => $borrower_number)->{'cardnumber'};
120 # Set barcode data
121 $layout_xml->{'barcode'}->[0]->{'data'} = $card_number if $layout_xml->{'barcode'};
123 # Create a new patroncard object
124 my $patron_card = C4::Patroncards::Patroncard->new(
125 batch_id => 1,
126 borrower_number => $borrower_number,
127 llx => $llx, # lower left corner of the card
128 lly => $lly,
129 height => $template->get_attr('label_height'), # of the card
130 width => $template->get_attr('label_width'),
131 layout => $layout_xml,
132 text_wrap_cols => 30, #FIXME: hardcoded
134 $patron_card->draw_guide_box($pdf) if $layout_xml->{'guide_box'};
135 $patron_card->draw_barcode($pdf) if $layout_xml->{'barcode'};
137 # Do image foo and place binary image data into layout hash
138 my $image_data = {};
139 my $error = undef;
140 my $images = $layout_xml->{'images'};
141 PROCESS_IMAGES:
142 foreach (keys %{$images}) {
143 if (grep{m/source/} keys(%{$images->{$_}->{'data_source'}->[0]})) {
144 if ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'none') {
145 next PROCESS_IMAGES;
147 elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'patronimages') {
148 ($image_data, $error) = GetPatronImage($card_number);
149 warn sprintf('No image exists for borrower number %s.', $borrower_number) if !$image_data;
150 next PROCESS_IMAGES if !$image_data;
152 elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'creator_images') {
153 my $dbh = C4::Context->dbh();
154 $dbh->{LongReadLen} = 1000000; # allows us to read approx 1MB
155 $image_data = $dbh->selectrow_hashref("SELECT imagefile FROM creator_images WHERE image_name = \'$images->{$_}->{'data_source'}->[0]->{'image_name'}\'");
156 warn sprintf('Database returned the following error: %s.', $error) if $error;
157 warn sprintf('Image does not exists in db table %s.', $images->{$_}->{'data_source'}->[0]->{'image_name'}) if !$image_data;
158 next PROCESS_IMAGES if !$image_data;
160 else {
161 warn sprintf('No retrieval method for image source %s.', $images->{$_}->{'data_source'}->[0]->{'image_source'});
162 next PROCESS_IMAGES;
165 else {
166 warn sprintf("Unrecognized image data source: %s", $images->{$_}->{'data_source'});
167 next PROCESS_IMAGES;
170 my $binary_data = $image_data->{'imagefile'};
172 # invoke the display image object...
173 my $image = Graphics::Magick->new;
174 $image->BlobToImage($binary_data);
176 # invoke the alt (aka print) image object...
177 my $alt_image = Graphics::Magick->new;
178 $alt_image->BlobToImage($binary_data);
179 $alt_image->Set(magick => 'jpg', quality => 100);
181 my $alt_width = ceil($image->Get('width')); # the rounding up is important: Adobe reader does not handle long decimal numbers well
182 my $alt_height = ceil($image->Get('height'));
183 my $ratio = $alt_width / $alt_height;
184 my $display_height = ceil($images->{$_}->{'Dx'});
185 my $display_width = ceil($ratio * $display_height);
188 $image->Resize(width => $display_width, height => $display_height);
189 $image->Set(magick => 'jpg', quality => 100);
191 # Write params for alt image...
192 $images->{$_}->{'alt'}->{'Sx'} = $alt_width;
193 $images->{$_}->{'alt'}->{'Sy'} = $alt_height;
194 $images->{$_}->{'alt'}->{'data'} = $alt_image->ImageToBlob();
196 # Write params for display image...
197 $images->{$_}->{'Sx'} = $display_width;
198 $images->{$_}->{'Sy'} = $display_height;
199 $images->{$_}->{'data'} = $image->ImageToBlob();
201 my $err = $patron_card->draw_image($pdf);
202 warn sprintf ("Error encountered while attempting to draw image %s, %s", $_, $err) if $err;
204 $patron_card->draw_text($pdf);
206 ($llx, $lly, $new_page) = $template->get_next_label_pos();
207 $pdf->Page() if $new_page;
210 $pdf->End();
212 # FIXME: Possibly do a redirect here if there were error encountered during PDF creation.