X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=patroncards%2Fcreate-pdf.pl;h=e660af6179cba1e703e636017f7796bcd89b4835;hb=88e0264413b1dc9ecf5dbe562636f40c7e0555d1;hp=d507722ef227fe2bf0d5eb3931bf95b6aed1bc36;hpb=e20270fec4f6d34f01050bea4c5765d5b3c4ed33;p=koha.git diff --git a/patroncards/create-pdf.pl b/patroncards/create-pdf.pl index d507722ef2..e660af6179 100755 --- a/patroncards/create-pdf.pl +++ b/patroncards/create-pdf.pl @@ -4,18 +4,18 @@ # # This file is part of Koha. # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along -# with Koha; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . use strict; use warnings; @@ -29,9 +29,11 @@ use autouse 'Data::Dumper' => qw(Dumper); use C4::Debug; use C4::Context; -use autouse 'C4::Members' => qw(GetPatronImage GetMember); +use autouse 'C4::Members' => qw(GetMember); use C4::Creators; use C4::Patroncards; +use Koha::List::Patron; +use Koha::Patron::Images; my $cgi = new CGI; @@ -43,27 +45,30 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user({ flagsrequired => { tools => 'label_creator' }, debug => 1, }); - - my $batch_id = $cgi->param('batch_id') if $cgi->param('batch_id'); my $template_id = $cgi->param('template_id') || undef; my $layout_id = $cgi->param('layout_id') || undef; -my $start_label = $cgi->param('start_label') || 1; -my @label_ids = $cgi->param('label_id') if $cgi->param('label_id'); -my @borrower_numbers = $cgi->param('borrower_number') if $cgi->param('borrower_number'); +my $start_card = $cgi->param('start_card') || 1; +my @label_ids = $cgi->multi_param('label_id') if $cgi->param('label_id'); +my @borrower_numbers = $cgi->multi_param('borrower_number') if $cgi->param('borrower_number'); +my $patronlist_id = $cgi->param('patronlist_id'); my $items = undef; # items = cards my $new_page = 0; -my $pdf_file = (@label_ids || @borrower_numbers ? "card_single_" . scalar(@label_ids || @borrower_numbers) : "card_batch_$batch_id"); -print $cgi->header( -type => 'application/pdf', - -encoding => 'utf-8', - -attachment => "$pdf_file.pdf", - ); +# Wrap pdf creation part into an eval, some vars need scope outside eval +my $pdf_ok; +my $pdf; +my $pdf_file; +my $cardscount = 0; -my $pdf = C4::Creators::PDF->new(InitVars => 0); +#Note fo bug 14138: Indenting follows in separate patch to ease review +eval { +$pdf_file = (@label_ids || @borrower_numbers ? "card_single_" . scalar(@label_ids || @borrower_numbers) : "card_batch_$batch_id"); + +$pdf = C4::Creators::PDF->new(InitVars => 0); my $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id); -my $template = C4::Patroncards::Template->retrieve(template_id => $template_id, profile_id => 1); +my $pc_template = C4::Patroncards::Template->retrieve(template_id => $template_id, profile_id => 1); my $layout = C4::Patroncards::Layout->retrieve(layout_id => $layout_id); $| = 1; @@ -71,14 +76,14 @@ $| = 1; # set the paper size my $lower_left_x = 0; my $lower_left_y = 0; -my $upper_right_x = $template->get_attr('page_width'); -my $upper_right_y = $template->get_attr('page_height'); +my $upper_right_x = $pc_template->get_attr('page_width'); +my $upper_right_y = $pc_template->get_attr('page_height'); $pdf->Compress(1); # comment this out to debug pdf files, but be sure to uncomment it in production or you may be very sorry... $pdf->Mbox($lower_left_x, $lower_left_y, $upper_right_x, $upper_right_y); my ($llx, $lly) = 0,0; -(undef, undef, $llx, $lly) = $template->get_label_position($start_label); +(undef, undef, $llx, $lly) = $pc_template->get_label_position($start_card); if (@label_ids) { my $batch_items = $batch->get_attr('items'); @@ -89,9 +94,17 @@ if (@label_ids) { } elsif (@borrower_numbers) { grep { - push(@{$items}, {item_number => $_}); + push(@{$items}, {borrower_number => $_}); } @borrower_numbers; } +elsif ( $patronlist_id ) { + my ($list) = GetPatronLists( { patron_list_id => $patronlist_id } ); + my @borrowerlist = $list->patron_list_patrons()->search_related('borrowernumber') + ->get_column('borrowernumber')->all(); + grep { + push(@{$items}, {borrower_number => $_}); + } @borrowerlist; +} else { $items = $batch->get_attr('items'); } @@ -114,6 +127,7 @@ if ($layout_xml->{'page_side'} eq 'B') { # rearrange items on backside of page t CARD_ITEMS: foreach my $item (@{$items}) { if ($item) { + $cardscount ++; my $borrower_number = $item->{'borrower_number'}; my $card_number = GetMember(borrowernumber => $borrower_number)->{'cardnumber'}; @@ -126,11 +140,12 @@ foreach my $item (@{$items}) { borrower_number => $borrower_number, llx => $llx, # lower left corner of the card lly => $lly, - height => $template->get_attr('label_height'), # of the card - width => $template->get_attr('label_width'), + height => $pc_template->get_attr('label_height'), # of the card + width => $pc_template->get_attr('label_width'), layout => $layout_xml, - text_wrap_cols => 30, #FIXME: hardcoded + text_wrap_cols => 30, #FIXME: hardcoded, ); + $patron_card->draw_guide_box($pdf) if $layout_xml->{'guide_box'}; $patron_card->draw_barcode($pdf) if $layout_xml->{'barcode'}; @@ -145,9 +160,14 @@ foreach my $item (@{$items}) { next PROCESS_IMAGES; } elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'patronimages') { - ($image_data, $error) = GetPatronImage($borrower_number); - warn sprintf('No image exists for borrower number %s.', $borrower_number) if !$image_data; - next PROCESS_IMAGES if !$image_data; + my $patron_image = Koha::Patron::Images->find($borrower_number); + if ($patron_image) { + $image_data->{'imagefile'} = $patron_image->imagefile; + } + else { + warn sprintf('No image exists for borrower number %s.', $borrower_number); + } + next PROCESS_IMAGES unless $patron_image; } elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'creator_images') { my $dbh = C4::Context->dbh(); @@ -178,6 +198,11 @@ foreach my $item (@{$items}) { $alt_image->BlobToImage($binary_data); $alt_image->Set(magick => 'jpg', quality => 100); + #To avoid pixelation have the image 5 times bigger and + #scale it down in PDF itself + my $oversize_factor = 8; + my $pdf_scale_factor = 1 / $oversize_factor; + my $alt_width = ceil($image->Get('width')); # the rounding up is important: Adobe reader does not handle long decimal numbers well my $alt_height = ceil($image->Get('height')); my $ratio = $alt_width / $alt_height; @@ -185,17 +210,20 @@ foreach my $item (@{$items}) { my $display_width = ceil($ratio * $display_height); - $image->Resize(width => $display_width, height => $display_height); + $image->Resize(width => $oversize_factor * $display_width, height => $oversize_factor * $display_height); $image->Set(magick => 'jpg', quality => 100); +# Write param for downsizing in pdf + $images->{$_}->{'scale'} = $pdf_scale_factor; + # Write params for alt image... - $images->{$_}->{'alt'}->{'Sx'} = $alt_width; - $images->{$_}->{'alt'}->{'Sy'} = $alt_height; + $images->{$_}->{'alt'}->{'Sx'} = $oversize_factor * $alt_width; + $images->{$_}->{'alt'}->{'Sy'} = $oversize_factor * $alt_height; $images->{$_}->{'alt'}->{'data'} = $alt_image->ImageToBlob(); # Write params for display image... - $images->{$_}->{'Sx'} = $display_width; - $images->{$_}->{'Sy'} = $display_height; + $images->{$_}->{'Sx'} = $oversize_factor * $display_width; + $images->{$_}->{'Sy'} = $oversize_factor * $display_height; $images->{$_}->{'data'} = $image->ImageToBlob(); my $err = $patron_card->draw_image($pdf); @@ -203,12 +231,32 @@ foreach my $item (@{$items}) { } $patron_card->draw_text($pdf); } - ($llx, $lly, $new_page) = $template->get_next_label_pos(); + ($llx, $lly, $new_page) = $pc_template->get_next_label_pos(); $pdf->Page() if $new_page; } +# No errors occurred within eval, we can issue the pdf +$pdf_ok = 1 if ($cardscount > 0); +}; # end of eval block -$pdf->End(); - -# FIXME: Possibly do a redirect here if there were error encountered during PDF creation. +if ($pdf_ok) { + #issue the pdf + print $cgi->header( -type => 'application/pdf', + -encoding => 'utf-8', + -attachment => "$pdf_file.pdf", + ); + $pdf->End(); +} +else { + # warn user that pdf is not created + my $errparams = '&pdferr=1'; + $errparams .= "&errba=$batch_id" if $batch_id; + $errparams .= "&errpl=$patronlist_id" if $patronlist_id; + $errparams = $errparams.'&errpt='.$cgi->param('borrower_number') if $cgi->param('borrower_number'); + $errparams .= "&errlo=$layout_id" if $layout_id; + $errparams .= "&errtpl=$template_id" if $template_id; + $errparams .= "&errnocards=1" if !$cardscount; + + print $cgi->redirect("/cgi-bin/koha/patroncards/manage.pl?card_element=batch$errparams"); +} 1;