Bug 15208: Ease translation for shelves messages
[koha.git] / patroncards / create-pdf.pl
blob30440a44b6ac0f06f6b58a86c6dfdf2a50d93efc
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
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.
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.
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>.
20 use strict;
21 use warnings;
23 use CGI qw ( -utf8 );
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;
34 use C4::Patroncards;
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_card = $cgi->param('start_card') || 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 $pc_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 = $pc_template->get_attr('page_width');
75 my $upper_right_y = $pc_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) = $pc_template->get_label_position($start_card);
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}, {borrower_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 => $pc_template->get_attr('label_height'), # of the card
130 width => $pc_template->get_attr('label_width'),
131 layout => $layout_xml,
132 text_wrap_cols => 30, #FIXME: hardcoded,
135 $patron_card->draw_guide_box($pdf) if $layout_xml->{'guide_box'};
136 $patron_card->draw_barcode($pdf) if $layout_xml->{'barcode'};
138 # Do image foo and place binary image data into layout hash
139 my $image_data = {};
140 my $error = undef;
141 my $images = $layout_xml->{'images'};
142 PROCESS_IMAGES:
143 foreach (keys %{$images}) {
144 if (grep{m/source/} keys(%{$images->{$_}->{'data_source'}->[0]})) {
145 if ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'none') {
146 next PROCESS_IMAGES;
148 elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'patronimages') {
149 ($image_data, $error) = GetPatronImage($borrower_number);
150 warn sprintf('No image exists for borrower number %s.', $borrower_number) if !$image_data;
151 next PROCESS_IMAGES if !$image_data;
153 elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'creator_images') {
154 my $dbh = C4::Context->dbh();
155 $dbh->{LongReadLen} = 1000000; # allows us to read approx 1MB
156 $image_data = $dbh->selectrow_hashref("SELECT imagefile FROM creator_images WHERE image_name = \'$images->{$_}->{'data_source'}->[0]->{'image_name'}\'");
157 warn sprintf('Database returned the following error: %s.', $error) if $error;
158 warn sprintf('Image does not exists in db table %s.', $images->{$_}->{'data_source'}->[0]->{'image_name'}) if !$image_data;
159 next PROCESS_IMAGES if !$image_data;
161 else {
162 warn sprintf('No retrieval method for image source %s.', $images->{$_}->{'data_source'}->[0]->{'image_source'});
163 next PROCESS_IMAGES;
166 else {
167 warn sprintf("Unrecognized image data source: %s", $images->{$_}->{'data_source'});
168 next PROCESS_IMAGES;
171 my $binary_data = $image_data->{'imagefile'};
173 # invoke the display image object...
174 my $image = Graphics::Magick->new;
175 $image->BlobToImage($binary_data);
177 # invoke the alt (aka print) image object...
178 my $alt_image = Graphics::Magick->new;
179 $alt_image->BlobToImage($binary_data);
180 $alt_image->Set(magick => 'jpg', quality => 100);
182 #To avoid pixelation have the image 5 times bigger and
183 #scale it down in PDF itself
184 my $oversize_factor = 8;
185 my $pdf_scale_factor = 1 / $oversize_factor;
187 my $alt_width = ceil($image->Get('width')); # the rounding up is important: Adobe reader does not handle long decimal numbers well
188 my $alt_height = ceil($image->Get('height'));
189 my $ratio = $alt_width / $alt_height;
190 my $display_height = ceil($images->{$_}->{'Dx'});
191 my $display_width = ceil($ratio * $display_height);
194 $image->Resize(width => $oversize_factor * $display_width, height => $oversize_factor * $display_height);
195 $image->Set(magick => 'jpg', quality => 100);
197 # Write param for downsizing in pdf
198 $images->{$_}->{'scale'} = $pdf_scale_factor;
200 # Write params for alt image...
201 $images->{$_}->{'alt'}->{'Sx'} = $oversize_factor * $alt_width;
202 $images->{$_}->{'alt'}->{'Sy'} = $oversize_factor * $alt_height;
203 $images->{$_}->{'alt'}->{'data'} = $alt_image->ImageToBlob();
205 # Write params for display image...
206 $images->{$_}->{'Sx'} = $oversize_factor * $display_width;
207 $images->{$_}->{'Sy'} = $oversize_factor * $display_height;
208 $images->{$_}->{'data'} = $image->ImageToBlob();
210 my $err = $patron_card->draw_image($pdf);
211 warn sprintf ("Error encountered while attempting to draw image %s, %s", $_, $err) if $err;
213 $patron_card->draw_text($pdf);
215 ($llx, $lly, $new_page) = $pc_template->get_next_label_pos();
216 $pdf->Page() if $new_page;
219 $pdf->End();
221 # FIXME: Possibly do a redirect here if there were error encountered during PDF creation.