View | Details | Raw Unified | Return to bug 14138
Collapse All | Expand All

(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/patroncards-errors.inc (-1 / +11 lines)
Lines 1-7 Link Here
1
[% IF ( error ) %]
1
[% IF ( error || CGI.param('pdferr') )  %]
2
<div class="dialog alert">
2
<div class="dialog alert">
3
  <p>
3
  <p>
4
    <strong>WARNING:</strong>
4
    <strong>WARNING:</strong>
5
        [% IF CGI.param('pdferr') %]
6
        Error while creating the PDF file.
7
        [% IF CGI.param('errnocards') %]No cards created (empty batch or list?)[% END %]
8
        [% IF CGI.param('errba') %]Batch: [% CGI.param('errba') %][% END %]
9
        [% IF CGI.param('errpl') %]Patron list: [%  CGI.param('errpl') %][% END %]
10
        [% IF CGI.param('errlo') %]Layout: [%  CGI.param('errlo') %][% END %]
11
        [% IF CGI.param('errtpl') %]Template: [%  CGI.param('errtpl') %][% END %]
12
13
        Please have your system administrator check the error log for details.
14
        [% END %]
5
        [% IF ( error == 101 ) %]
15
        [% IF ( error == 101 ) %]
6
        The database returned an error while [% IF ( card_element ) %]saving [% card_element %] [% element_id %][% ELSE %]attempting a save operation[% END %]. Please have your system administrator check the error log for details.
16
        The database returned an error while [% IF ( card_element ) %]saving [% card_element %] [% element_id %][% ELSE %]attempting a save operation[% END %]. Please have your system administrator check the error log for details.
7
        [% ELSIF ( error == 102 ) %]
17
        [% ELSIF ( error == 102 ) %]
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/patroncards/manage.tt (+1 lines)
Lines 1-3 Link Here
1
[% USE CGI %]
1
[% BLOCK translate_card_element %]
2
[% BLOCK translate_card_element %]
2
[%-  SWITCH element -%]
3
[%-  SWITCH element -%]
3
[%-  CASE 'layout'    -%]layout
4
[%-  CASE 'layout'    -%]layout
(-)a/patroncards/create-pdf.pl (-149 / +169 lines)
Lines 45-52 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({ Link Here
45
                                                                     flagsrequired   => { tools => 'label_creator' },
45
                                                                     flagsrequired   => { tools => 'label_creator' },
46
                                                                     debug           => 1,
46
                                                                     debug           => 1,
47
                                                                     });
47
                                                                     });
48
49
50
my $batch_id    = $cgi->param('batch_id') if $cgi->param('batch_id');
48
my $batch_id    = $cgi->param('batch_id') if $cgi->param('batch_id');
51
my $template_id = $cgi->param('template_id') || undef;
49
my $template_id = $cgi->param('template_id') || undef;
52
my $layout_id   = $cgi->param('layout_id') || undef;
50
my $layout_id   = $cgi->param('layout_id') || undef;
Lines 58-239 my $patronlist_id = $cgi->param('patronlist_id'); Link Here
58
my $items = undef; # items = cards
56
my $items = undef; # items = cards
59
my $new_page = 0;
57
my $new_page = 0;
60
58
61
my $pdf_file = (@label_ids || @borrower_numbers ? "card_single_" . scalar(@label_ids || @borrower_numbers) : "card_batch_$batch_id");
59
# Wrap pdf creation part into an eval, some vars need scope outside eval
62
print $cgi->header( -type       => 'application/pdf',
60
my $pdf_ok;
63
                    -encoding   => 'utf-8',
61
my $pdf;
64
                    -attachment => "$pdf_file.pdf",
62
my $pdf_file;
65
                  );
63
my $cardscount = 0;
66
64
67
my $pdf = C4::Creators::PDF->new(InitVars => 0);
65
eval {
68
my $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
66
    $pdf_file = (@label_ids || @borrower_numbers ? "card_single_" . scalar(@label_ids || @borrower_numbers) : "card_batch_$batch_id");
69
my $pc_template = C4::Patroncards::Template->retrieve(template_id => $template_id, profile_id => 1);
70
my $layout = C4::Patroncards::Layout->retrieve(layout_id => $layout_id);
71
67
72
$| = 1;
68
    $pdf = C4::Creators::PDF->new(InitVars => 0);
69
    my $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
70
    my $pc_template = C4::Patroncards::Template->retrieve(template_id => $template_id, profile_id => 1);
71
    my $layout = C4::Patroncards::Layout->retrieve(layout_id => $layout_id);
73
72
74
# set the paper size
73
    $| = 1;
75
my $lower_left_x  = 0;
76
my $lower_left_y  = 0;
77
my $upper_right_x = $pc_template->get_attr('page_width');
78
my $upper_right_y = $pc_template->get_attr('page_height');
79
74
80
$pdf->Compress(1); # comment this out to debug pdf files, but be sure to uncomment it in production or you may be very sorry...
75
    # set the paper size
81
$pdf->Mbox($lower_left_x, $lower_left_y, $upper_right_x, $upper_right_y);
76
    my $lower_left_x  = 0;
77
    my $lower_left_y  = 0;
78
    my $upper_right_x = $pc_template->get_attr('page_width');
79
    my $upper_right_y = $pc_template->get_attr('page_height');
82
80
83
my ($llx, $lly) = 0,0;
81
    $pdf->Compress(1); # comment this out to debug pdf files, but be sure to uncomment it in production or you may be very sorry...
84
(undef, undef, $llx, $lly) = $pc_template->get_label_position($start_card);
82
    $pdf->Mbox($lower_left_x, $lower_left_y, $upper_right_x, $upper_right_y);
85
83
86
if (@label_ids) {
84
    my ($llx, $lly) = 0,0;
85
    (undef, undef, $llx, $lly) = $pc_template->get_label_position($start_card);
86
87
    if (@label_ids) {
87
    my $batch_items = $batch->get_attr('items');
88
    my $batch_items = $batch->get_attr('items');
88
    grep {
89
        grep {
89
        my $label_id = $_;
90
            my $label_id = $_;
90
        push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
91
            push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
91
    } @label_ids;
92
         } @label_ids;
92
}
93
    }
93
elsif (@borrower_numbers) {
94
    elsif (@borrower_numbers) {
94
    grep {
95
        grep {
95
        push(@{$items}, {borrower_number => $_});
96
            push(@{$items}, {borrower_number => $_});
96
    } @borrower_numbers;
97
        } @borrower_numbers;
97
}
98
    }
98
elsif ( $patronlist_id  ) {
99
    elsif ( $patronlist_id  ) {
99
    my ($list) = GetPatronLists( { patron_list_id => $patronlist_id } );
100
        my ($list) = GetPatronLists( { patron_list_id => $patronlist_id } );
100
    my @borrowerlist = $list->patron_list_patrons()->search_related('borrowernumber')
101
        my @borrowerlist = $list->patron_list_patrons()->search_related('borrowernumber')
101
    ->get_column('borrowernumber')->all();
102
        ->get_column('borrowernumber')->all();
102
    grep {
103
        grep {
103
        push(@{$items}, {borrower_number => $_});
104
            push(@{$items}, {borrower_number => $_});
104
    } @borrowerlist;
105
        } @borrowerlist;
105
}
106
    }
106
else {
107
    else {
107
    $items = $batch->get_attr('items');
108
        $items = $batch->get_attr('items');
108
}
109
    }
109
110
110
my $layout_xml = XMLin($layout->get_attr('layout_xml'), ForceArray => 1);
111
    my $layout_xml = XMLin($layout->get_attr('layout_xml'), ForceArray => 1);
111
112
112
if ($layout_xml->{'page_side'} eq 'B') { # rearrange items on backside of page to swap columns
113
    if ($layout_xml->{'page_side'} eq 'B') { # rearrange items on backside of page to swap columns
113
    my $even = 1;
114
        my $even = 1;
114
    my $odd = 0;
115
        my $odd = 0;
115
    my @swap_array = ();
116
        my @swap_array = ();
116
    while ($even <= (scalar(@{$items})+1)) {
117
        while ($even <= (scalar(@{$items})+1)) {
117
        push (@swap_array, @{$items}[$even]);
118
            push (@swap_array, @{$items}[$even]);
118
        push (@swap_array, @{$items}[$odd]);
119
            push (@swap_array, @{$items}[$odd]);
119
        $even += 2;
120
            $even += 2;
120
        $odd += 2;
121
            $odd += 2;
122
        }
123
        @{$items} = @swap_array;
121
    }
124
    }
122
    @{$items} = @swap_array;
123
}
124
125
125
CARD_ITEMS:
126
    CARD_ITEMS:
126
foreach my $item (@{$items}) {
127
    foreach my $item (@{$items}) {
127
    if ($item) {
128
        if ($item) {
128
        my $borrower_number = $item->{'borrower_number'};
129
            $cardscount ++;
129
        my $card_number = GetMember(borrowernumber => $borrower_number)->{'cardnumber'};
130
            my $borrower_number = $item->{'borrower_number'};
130
131
            my $card_number = GetMember(borrowernumber => $borrower_number)->{'cardnumber'};
131
#       Set barcode data
132
132
        $layout_xml->{'barcode'}->[0]->{'data'} = $card_number if $layout_xml->{'barcode'};
133
    #       Set barcode data
133
134
            $layout_xml->{'barcode'}->[0]->{'data'} = $card_number if $layout_xml->{'barcode'};
134
#       Create a new patroncard object
135
135
        my $patron_card = C4::Patroncards::Patroncard->new(
136
    #       Create a new patroncard object
136
                batch_id                => 1,
137
            my $patron_card = C4::Patroncards::Patroncard->new(
137
                borrower_number         => $borrower_number,
138
                    batch_id                => 1,
138
                llx                     => $llx, # lower left corner of the card
139
                    borrower_number         => $borrower_number,
139
                lly                     => $lly,
140
                    llx                     => $llx, # lower left corner of the card
140
                height                  => $pc_template->get_attr('label_height'), # of the card
141
                    lly                     => $lly,
141
                width                   => $pc_template->get_attr('label_width'),
142
                    height                  => $pc_template->get_attr('label_height'), # of the card
142
                layout                  => $layout_xml,
143
                    width                   => $pc_template->get_attr('label_width'),
143
                text_wrap_cols          => 30, #FIXME: hardcoded,
144
                    layout                  => $layout_xml,
144
        );
145
                    text_wrap_cols          => 30, #FIXME: hardcoded,
145
146
            );
146
        $patron_card->draw_guide_box($pdf) if $layout_xml->{'guide_box'};
147
147
        $patron_card->draw_barcode($pdf) if $layout_xml->{'barcode'};
148
            $patron_card->draw_guide_box($pdf) if $layout_xml->{'guide_box'};
148
149
            $patron_card->draw_barcode($pdf) if $layout_xml->{'barcode'};
149
#       Do image foo and place binary image data into layout hash
150
150
        my $image_data = {};
151
    #       Do image foo and place binary image data into layout hash
151
        my $error = undef;
152
            my $image_data = {};
152
        my $images = $layout_xml->{'images'};
153
            my $error = undef;
153
        PROCESS_IMAGES:
154
            my $images = $layout_xml->{'images'};
154
        foreach (keys %{$images}) {
155
            PROCESS_IMAGES:
155
            if (grep{m/source/} keys(%{$images->{$_}->{'data_source'}->[0]})) {
156
            foreach (keys %{$images}) {
156
                if ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'none') {
157
                if (grep{m/source/} keys(%{$images->{$_}->{'data_source'}->[0]})) {
157
                    next PROCESS_IMAGES;
158
                    if ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'none') {
158
                }
159
                        next PROCESS_IMAGES;
159
                elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'patronimages') {
160
                    }
160
                    my $patron_image = Koha::Patron::Images->find($borrower_number);
161
                    elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'patronimages') {
161
                    if ($patron_image) {
162
                        my $patron_image = Koha::Patron::Images->find($borrower_number);
162
                        $image_data->{'imagefile'} = $patron_image->imagefile;
163
                        if ($patron_image) {
164
                            $image_data->{'imagefile'} = $patron_image->imagefile;
165
                        }
166
                        else {
167
                            warn sprintf('No image exists for borrower number %s.', $borrower_number);
168
                        }
169
                        next PROCESS_IMAGES unless $patron_image;
170
                    }
171
                    elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'creator_images') {
172
                        my $dbh = C4::Context->dbh();
173
                        $dbh->{LongReadLen} = 1000000;      # allows us to read approx 1MB
174
                        $image_data = $dbh->selectrow_hashref("SELECT imagefile FROM creator_images WHERE image_name = \'$images->{$_}->{'data_source'}->[0]->{'image_name'}\'");
175
                        warn sprintf('Database returned the following error: %s.', $error) if $error;
176
                        warn sprintf('Image does not exists in db table %s.', $images->{$_}->{'data_source'}->[0]->{'image_name'}) if !$image_data;
177
                        next PROCESS_IMAGES if !$image_data;
163
                    }
178
                    }
164
                    else {
179
                    else {
165
                        warn sprintf('No image exists for borrower number %s.', $borrower_number);
180
                        warn sprintf('No retrieval method for image source %s.', $images->{$_}->{'data_source'}->[0]->{'image_source'});
181
                        next PROCESS_IMAGES;
166
                    }
182
                    }
167
                    next PROCESS_IMAGES unless $patron_image;
168
                }
169
                elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'creator_images') {
170
                    my $dbh = C4::Context->dbh();
171
                    $dbh->{LongReadLen} = 1000000;      # allows us to read approx 1MB
172
                    $image_data = $dbh->selectrow_hashref("SELECT imagefile FROM creator_images WHERE image_name = \'$images->{$_}->{'data_source'}->[0]->{'image_name'}\'");
173
                    warn sprintf('Database returned the following error: %s.', $error) if $error;
174
                    warn sprintf('Image does not exists in db table %s.', $images->{$_}->{'data_source'}->[0]->{'image_name'}) if !$image_data;
175
                    next PROCESS_IMAGES if !$image_data;
176
                }
183
                }
177
                else {
184
                else {
178
                    warn sprintf('No retrieval method for image source %s.', $images->{$_}->{'data_source'}->[0]->{'image_source'});
185
                    warn sprintf("Unrecognized image data source: %s", $images->{$_}->{'data_source'});
179
                    next PROCESS_IMAGES;
186
                    next PROCESS_IMAGES;
180
                }
187
                }
181
            }
182
            else {
183
                warn sprintf("Unrecognized image data source: %s", $images->{$_}->{'data_source'});
184
                next PROCESS_IMAGES;
185
            }
186
188
187
        my $binary_data = $image_data->{'imagefile'};
189
            my $binary_data = $image_data->{'imagefile'};
188
190
189
#       invoke the display image object...
191
    #       invoke the display image object...
190
        my $image = Graphics::Magick->new;
192
            my $image = Graphics::Magick->new;
191
        $image->BlobToImage($binary_data);
193
            $image->BlobToImage($binary_data);
192
194
193
#       invoke the alt (aka print) image object...
195
    #       invoke the alt (aka print) image object...
194
        my $alt_image = Graphics::Magick->new;
196
            my $alt_image = Graphics::Magick->new;
195
        $alt_image->BlobToImage($binary_data);
197
            $alt_image->BlobToImage($binary_data);
196
        $alt_image->Set(magick => 'jpg', quality => 100);
198
            $alt_image->Set(magick => 'jpg', quality => 100);
197
199
198
        #To avoid pixelation have the image 5 times bigger and
200
            #To avoid pixelation have the image 5 times bigger and
199
        #scale it down in PDF itself
201
            #scale it down in PDF itself
200
        my $oversize_factor = 8;
202
            my $oversize_factor = 8;
201
        my $pdf_scale_factor = 1 / $oversize_factor;
203
            my $pdf_scale_factor = 1 / $oversize_factor;
202
204
203
        my $alt_width = ceil($image->Get('width')); # the rounding up is important: Adobe reader does not handle long decimal numbers well
205
            my $alt_width = ceil($image->Get('width')); # the rounding up is important: Adobe reader does not handle long decimal numbers well
204
        my $alt_height = ceil($image->Get('height'));
206
            my $alt_height = ceil($image->Get('height'));
205
        my $ratio = $alt_width / $alt_height;
207
            my $ratio = $alt_width / $alt_height;
206
        my $display_height = ceil($images->{$_}->{'Dx'});
208
            my $display_height = ceil($images->{$_}->{'Dx'});
207
        my $display_width = ceil($ratio * $display_height);
209
            my $display_width = ceil($ratio * $display_height);
208
210
209
211
210
        $image->Resize(width => $oversize_factor * $display_width, height => $oversize_factor * $display_height);
212
            $image->Resize(width => $oversize_factor * $display_width, height => $oversize_factor * $display_height);
211
        $image->Set(magick => 'jpg', quality => 100);
213
            $image->Set(magick => 'jpg', quality => 100);
212
214
213
#       Write param for downsizing in pdf
215
    #       Write param for downsizing in pdf
214
            $images->{$_}->{'scale'} = $pdf_scale_factor;
216
                $images->{$_}->{'scale'} = $pdf_scale_factor;
215
217
216
#       Write params for alt image...
218
    #       Write params for alt image...
217
            $images->{$_}->{'alt'}->{'Sx'} = $oversize_factor * $alt_width;
219
                $images->{$_}->{'alt'}->{'Sx'} = $oversize_factor * $alt_width;
218
            $images->{$_}->{'alt'}->{'Sy'} = $oversize_factor * $alt_height;
220
                $images->{$_}->{'alt'}->{'Sy'} = $oversize_factor * $alt_height;
219
            $images->{$_}->{'alt'}->{'data'} = $alt_image->ImageToBlob();
221
                $images->{$_}->{'alt'}->{'data'} = $alt_image->ImageToBlob();
220
222
221
#       Write params for display image...
223
    #       Write params for display image...
222
            $images->{$_}->{'Sx'} = $oversize_factor * $display_width;
224
                $images->{$_}->{'Sx'} = $oversize_factor * $display_width;
223
            $images->{$_}->{'Sy'} = $oversize_factor * $display_height;
225
                $images->{$_}->{'Sy'} = $oversize_factor * $display_height;
224
            $images->{$_}->{'data'} = $image->ImageToBlob();
226
                $images->{$_}->{'data'} = $image->ImageToBlob();
225
227
226
            my $err = $patron_card->draw_image($pdf);
228
                my $err = $patron_card->draw_image($pdf);
227
            warn sprintf ("Error encountered while attempting to draw image %s, %s", $_, $err) if $err;
229
                warn sprintf ("Error encountered while attempting to draw image %s, %s", $_, $err) if $err;
230
            }
231
            $patron_card->draw_text($pdf);
228
        }
232
        }
229
        $patron_card->draw_text($pdf);
233
        ($llx, $lly, $new_page) = $pc_template->get_next_label_pos();
234
        $pdf->Page() if $new_page;
230
    }
235
    }
231
    ($llx, $lly, $new_page) = $pc_template->get_next_label_pos();
236
    # No errors occured within eval, we can issue the pdf
232
    $pdf->Page() if $new_page;
237
    $pdf_ok = 1 if ($cardscount > 0);
233
}
238
}; # end of eval block
234
235
$pdf->End();
236
239
237
# FIXME: Possibly do a redirect here if there were error encountered during PDF creation.
240
if ($pdf_ok) {
241
    #issue the pdf
242
    print $cgi->header( -type       => 'application/pdf',
243
                    -encoding   => 'utf-8',
244
                    -attachment => "$pdf_file.pdf",
245
                  );
246
    $pdf->End();
247
}
248
else {
249
    # warn user that pdf is not created
250
    my $errparams = '&pdferr=1';
251
    $errparams .= "&errba=$batch_id" if $batch_id;
252
    $errparams .= "&errpl=$patronlist_id" if $patronlist_id;
253
    $errparams .= "&errlo=$layout_id" if $layout_id;
254
    $errparams .= "&errtpl=$template_id" if $template_id;
255
    $errparams .= "&errnocards=1" if !$cardscount;
256
257
    print $cgi->redirect("/cgi-bin/koha/patroncards/manage.pl?card_element=batch$errparams");
258
}
238
259
239
1;
260
1;
240
- 

Return to bug 14138