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

(-)a/Koha/Patron/Discharge.pm (-29 / +17 lines)
Lines 3-8 package Koha::Patron::Discharge; Link Here
3
use Modern::Perl;
3
use Modern::Perl;
4
use CGI;
4
use CGI;
5
use File::Temp qw( tmpnam );
5
use File::Temp qw( tmpnam );
6
use IPC::Cmd;
6
use Carp qw( carp );
7
use Carp qw( carp );
7
8
8
use C4::Templates qw ( gettemplate );
9
use C4::Templates qw ( gettemplate );
Lines 128-167 sub generate_as_pdf { Link Here
128
    my $html_path = tmpnam() . '.html';
129
    my $html_path = tmpnam() . '.html';
129
    my $pdf_path = tmpnam() . '.pdf';
130
    my $pdf_path = tmpnam() . '.pdf';
130
    my $html_content = $tmpl->output;
131
    my $html_content = $tmpl->output;
132
133
    # export to HTML
131
    open my $html_fh, '>:encoding(utf8)', $html_path;
134
    open my $html_fh, '>:encoding(utf8)', $html_path;
132
    say $html_fh $html_content;
135
    say $html_fh $html_content;
133
    close $html_fh;
136
    close $html_fh;
134
    my $output = eval { require PDF::FromHTML; return; } || $@;
137
135
    if ($output && $params->{testing}) {
138
    if ( IPC::Cmd::can_run('weasyprint') ) {
136
        carp $output;
139
        my( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
137
        $pdf_path = undef;
140
            IPC::Cmd::run( command => "weasyprint $html_path $pdf_path", verbose => 0 );
138
    }
141
139
    elsif ($output) {
142
        map {warn $_} @$stderr_buf
140
        die $output;
143
          if $stderr_buf and scalar @$stderr_buf;
144
145
        unless ( $success ) {
146
            warn $error_message;
147
            $pdf_path = undef;
148
        }
141
    }
149
    }
142
    else {
150
    else {
143
        my $pdf = PDF::FromHTML->new( encoding => 'utf-8' );
151
        warn "weasyprint not found!";
144
        $pdf->load_file( $html_path );
152
        $pdf_path = undef;
145
146
        my $ttf = C4::Context->config('ttf');
147
        if ( $ttf  && exists $ttf->{font} ) {
148
149
            my $type2path;
150
            foreach my $font ( @{ $ttf->{font} } ) {
151
                    $type2path->{ $font->{type} } = $font->{content};
152
            }
153
154
            $pdf->convert(
155
                FontBold          => $type2path->{'HB'} || 'HelveticaBold',
156
                FontOblique       => $type2path->{'HO'} || 'HelveticaOblique',
157
                FontBoldOblique   => $type2path->{'HBO'}|| 'HelveticaBoldOblique',
158
                FontUnicode       => $type2path->{'H'}  || 'Helvetica',
159
                Font              => $type2path->{'H'}  || 'Helvetica',
160
            );
161
        } else {
162
            $pdf->convert();
163
        }
164
        $pdf->write_file( $pdf_path );
165
    }
153
    }
166
154
167
    return $pdf_path;
155
    return $pdf_path;
(-)a/t/db_dependent/Patron/Borrower_Discharge.t (-13 / +35 lines)
Lines 15-22 Link Here
15
# with Koha; if not, see <http://www.gnu.org/licenses>.
15
# with Koha; if not, see <http://www.gnu.org/licenses>.
16
16
17
use Modern::Perl;
17
use Modern::Perl;
18
use Test::More tests => 19;
18
19
use Test::More tests => 23;
20
21
use Test::MockModule;
19
use Test::Warn;
22
use Test::Warn;
23
24
use IPC::Cmd qw(can_run);
20
use MARC::Record;
25
use MARC::Record;
21
26
22
use C4::Circulation qw( AddIssue AddReturn );
27
use C4::Circulation qw( AddIssue AddReturn );
Lines 114-131 is(scalar( Koha::Patron::Discharge::get_pendings ), 1, 'There is a pending disch Link Here
114
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->{borrowernumber} } );
119
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->{borrowernumber} } );
115
is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request (second time)');
120
is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request (second time)');
116
121
117
# Check if PDF::FromHTML is installed.
122
SKIP: {
118
my $check = eval { require PDF::FromHTML; };
123
    skip "Skipping because weasyprint is not installed",
124
        5 unless can_run('weasyprint');
119
125
120
# Tests for if PDF::FromHTML is installed
126
    isnt(
121
if ($check) {
127
        Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->{borrowernumber} } ),
122
    isnt( Koha::Patron::Discharge::generate_as_pdf({ borrowernumber => $patron->{borrowernumber} }), undef, "Temporary PDF generated." );
128
        undef,
123
}
129
        "Temporary PDF generated."
124
# Tests for if PDF::FromHTML is not installed
130
    );
125
else {
131
126
    warning_like { Koha::Patron::Discharge::generate_as_pdf({ borrowernumber => $patron->{borrowernumber}, testing => 1 }) }
132
    my $mocked_ipc = Test::MockModule->new('IPC::Cmd');
127
          [ qr/Can't locate PDF\/FromHTML.pm in \@INC/ ],
133
128
          "Expected failure because of missing PDF::FromHTML.";
134
    $mocked_ipc->mock( 'run', sub { return 0, 'Some error' } );
135
136
    my $result;
137
    warning_is
138
        { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->{borrowernumber} } ); }
139
        'Some error',
140
        'Failed call to run() prints the generated error';
141
142
    is( $result, undef, 'undef returned if failed run' );
143
144
    $mocked_ipc->mock( 'can_run', undef );
145
146
    warning_is
147
        { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->{borrowernumber} } ); }
148
        'weasyprint not found!',
149
        'Expected failure because of missing weasyprint';
150
151
    is( $result, undef, 'undef returned if missing weasyprint' );
129
}
152
}
130
153
131
# FIXME Should be a Koha::Object object
154
# FIXME Should be a Koha::Object object
132
- 

Return to bug 14251