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

(-)a/Koha/PDF/Loader.pm (+120 lines)
Line 0 Link Here
1
package Koha::PDF::Loader;
2
3
#
4
# This file is part of Koha.
5
#
6
# Koha is free software; you can redistribute it and/or modify it
7
# under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# Koha is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with Koha; if not, see <https://www.gnu.org/licenses>.
18
19
use Modern::Perl;
20
21
use PDF::API2;
22
23
#NOTE: Support PDF::API2 methods before and after version 2.042
24
my $pdf_api2_methods = {
25
    'to_string'   => 'to_string',
26
    'from_string' => 'from_string',
27
};
28
29
#NOTE: Initialise methods at module load time
30
_init_methods();
31
32
=head1 NAME
33
34
Koha::PDF::Loader - Koha class for loading PDFs
35
36
=head1 API
37
38
=head2 Class Methods
39
40
=cut
41
42
sub _init_methods {
43
    if ( PDF::API2->can('to_string') ) {
44
        __PACKAGE__->set_method_name( { method => 'to_string', name_to_use => 'to_string' } );
45
    } elsif ( PDF::API2->can('stringify') ) {
46
47
        #NOTE: Newer versions still contain the deprecated method name
48
        __PACKAGE__->set_method_name( { method => 'to_string', name_to_use => 'stringify' } );
49
    }
50
    if ( PDF::API2->can('from_string') ) {
51
        __PACKAGE__->set_method_name( { method => 'from_string', name_to_use => 'from_string' } );
52
    } elsif ( PDF::API2->can('open_scalar') ) {
53
54
        #NOTE: Newer versions still contain the deprecated method name
55
        __PACKAGE__->set_method_name( { method => 'from_string', name_to_use => 'open_scalar' } );
56
    }
57
}
58
59
=head3 get_method_name
60
61
Get method name. Shouldn't be a need to use it outside of testing.
62
63
=cut
64
65
sub get_method_name {
66
    my ( $class, $args ) = @_;
67
    my $method = $args->{method};
68
    if ($method) {
69
        if ( $pdf_api2_methods->{$method} ) {
70
            return $pdf_api2_methods->{$method};
71
        }
72
    }
73
    return;
74
}
75
76
=head3 set_method_name
77
78
Set method name. Shouldn't be be a need to use it outside of testing.
79
80
=cut
81
82
sub set_method_name {
83
    my ( $class, $args ) = @_;
84
    my $method      = $args->{method};
85
    my $name_to_use = $args->{name_to_use};
86
    if ( $method && $name_to_use ) {
87
        if ( $pdf_api2_methods->{$method} ) {
88
            $pdf_api2_methods->{$method} = $name_to_use;
89
            return 1;
90
        }
91
    }
92
    return;
93
}
94
95
=head3 load_pdf_from_template
96
97
Loads a PDF into memory from a template file in a backwards
98
compatible way.
99
100
=cut
101
102
sub load_pdf_from_template {
103
    my ( $class, $args ) = @_;
104
    my $pdf;
105
    my $method_to_string   = __PACKAGE__->get_method_name( { method => 'to_string' } );
106
    my $method_from_string = __PACKAGE__->get_method_name( { method => 'from_string' } );
107
    my $pdf_template       = $args->{pdf_template};
108
    if ($pdf_template) {
109
        my $pdf_template_obj = PDF::API2->open($pdf_template);
110
        if ($pdf_template_obj) {
111
            my $pdf_template_str = $pdf_template_obj->$method_to_string();
112
            if ($pdf_template_str) {
113
                $pdf = PDF::API2->$method_from_string($pdf_template_str);
114
            }
115
        }
116
    }
117
    return $pdf;
118
}
119
120
1;
(-)a/Koha/pdfformat/layout1page.pm (-4 / +2 lines)
Lines 37-43 use constant mm => 25.4 / 72; Link Here
37
use constant in => 1 / 72;
37
use constant in => 1 / 72;
38
use constant pt => 1;
38
use constant pt => 1;
39
39
40
use PDF::API2;
40
use Koha::PDF::Loader;
41
41
42
#A4 paper specs
42
#A4 paper specs
43
my ( $height, $width ) = ( 297, 210 );
43
my ( $height, $width ) = ( 297, 210 );
Lines 247-255 sub printpdf { Link Here
247
    # open the default PDF that will be used for base (1st page already filled)
247
    # open the default PDF that will be used for base (1st page already filled)
248
    my $pdf_template =
248
    my $pdf_template =
249
        C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout1page.pdf';
249
        C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout1page.pdf';
250
    my $pdf_template_obj = PDF::API2->open($pdf_template);
250
    my $pdf = Koha::PDF::Loader->load_pdf_from_template( { pdf_template => $pdf_template } );
251
    my $pdf_template_str = $pdf_template_obj->to_string();
252
    my $pdf              = PDF::API2->from_string($pdf_template_str);
253
    $pdf->pageLabel(
251
    $pdf->pageLabel(
254
        0,
252
        0,
255
        {
253
        {
(-)a/Koha/pdfformat/layout2pages.pm (-4 / +2 lines)
Lines 40-46 use constant mm => 25.4 / 72; Link Here
40
use constant in => 1 / 72;
40
use constant in => 1 / 72;
41
use constant pt => 1;
41
use constant pt => 1;
42
42
43
use PDF::API2;
43
use Koha::PDF::Loader;
44
44
45
#A4 paper specs
45
#A4 paper specs
46
my ( $height, $width ) = ( 297, 210 );
46
my ( $height, $width ) = ( 297, 210 );
Lines 262-270 sub printpdf { Link Here
262
    # open the default PDF that will be used for base (1st page already filled)
262
    # open the default PDF that will be used for base (1st page already filled)
263
    my $pdf_template =
263
    my $pdf_template =
264
        C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout2pages.pdf';
264
        C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout2pages.pdf';
265
    my $pdf_template_obj = PDF::API2->open($pdf_template);
265
    my $pdf = Koha::PDF::Loader->load_pdf_from_template( { pdf_template => $pdf_template } );
266
    my $pdf_template_str = $pdf_template_obj->to_string();
267
    my $pdf              = PDF::API2->from_string($pdf_template_str);
268
    $pdf->pageLabel(
266
    $pdf->pageLabel(
269
        0,
267
        0,
270
        {
268
        {
(-)a/Koha/pdfformat/layout2pagesde.pm (-4 / +2 lines)
Lines 40-46 use constant mm => 25.4 / 72; Link Here
40
use constant in => 1 / 72;
40
use constant in => 1 / 72;
41
use constant pt => 1;
41
use constant pt => 1;
42
42
43
use PDF::API2;
43
use Koha::PDF::Loader;
44
44
45
#A4 paper specs
45
#A4 paper specs
46
my ( $height, $width ) = ( 297, 210 );
46
my ( $height, $width ) = ( 297, 210 );
Lines 264-272 sub printpdf { Link Here
264
    # open the default PDF that will be used for base (1st page already filled)
264
    # open the default PDF that will be used for base (1st page already filled)
265
    my $pdf_template =
265
    my $pdf_template =
266
        C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout2pagesde.pdf';
266
        C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout2pagesde.pdf';
267
    my $pdf_template_obj = PDF::API2->open($pdf_template);
267
    my $pdf = Koha::PDF::Loader->load_pdf_from_template( { pdf_template => $pdf_template } );
268
    my $pdf_template_str = $pdf_template_obj->to_string();
269
    my $pdf              = PDF::API2->from_string($pdf_template_str);
270
    $pdf->pageLabel(
268
    $pdf->pageLabel(
271
        0,
269
        0,
272
        {
270
        {
(-)a/Koha/pdfformat/layout3pages.pm (-4 / +2 lines)
Lines 44-50 use constant mm => 25.4 / 72; Link Here
44
use constant in => 1 / 72;
44
use constant in => 1 / 72;
45
use constant pt => 1;
45
use constant pt => 1;
46
46
47
use PDF::API2;
47
use Koha::PDF::Loader;
48
48
49
#A4 paper specs
49
#A4 paper specs
50
my ( $height, $width ) = ( 297, 210 );
50
my ( $height, $width ) = ( 297, 210 );
Lines 455-463 sub printpdf { Link Here
455
    # open the default PDF that will be used for base (1st page already filled)
455
    # open the default PDF that will be used for base (1st page already filled)
456
    my $pdf_template =
456
    my $pdf_template =
457
        C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout3pages.pdf';
457
        C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout3pages.pdf';
458
    my $pdf_template_obj = PDF::API2->open($pdf_template);
458
    my $pdf = Koha::PDF::Loader->load_pdf_from_template( { pdf_template => $pdf_template } );
459
    my $pdf_template_str = $pdf_template_obj->to_string();
460
    my $pdf              = PDF::API2->from_string($pdf_template_str);
461
    $pdf->pageLabel(
459
    $pdf->pageLabel(
462
        0,
460
        0,
463
        {
461
        {
(-)a/Koha/pdfformat/layout3pagesfr.pm (-4 / +2 lines)
Lines 43-49 use constant mm => 25.4 / 72; Link Here
43
use constant in => 1 / 72;
43
use constant in => 1 / 72;
44
use constant pt => 1;
44
use constant pt => 1;
45
45
46
use PDF::API2;
46
use Koha::PDF::Loader;
47
47
48
#A4 paper specs
48
#A4 paper specs
49
my ( $height, $width ) = ( 297, 210 );
49
my ( $height, $width ) = ( 297, 210 );
Lines 447-455 sub printpdf { Link Here
447
    # open the default PDF that will be used for base (1st page already filled)
447
    # open the default PDF that will be used for base (1st page already filled)
448
    my $pdf_template =
448
    my $pdf_template =
449
        C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout3pagesfr.pdf';
449
        C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout3pagesfr.pdf';
450
    my $pdf_template_obj = PDF::API2->open($pdf_template);
450
    my $pdf = Koha::PDF::Loader->load_pdf_from_template( { pdf_template => $pdf_template } );
451
    my $pdf_template_str = $pdf_template_obj->to_string();
452
    my $pdf              = PDF::API2->from_string($pdf_template_str);
453
    $pdf->pageLabel(
451
    $pdf->pageLabel(
454
        0,
452
        0,
455
        {
453
        {
(-)a/t/Koha/PDF/Loader.t (-1 / +77 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <https://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
use File::Temp;
20
21
use Test::NoWarnings;
22
use Test::More tests => 4;
23
24
use Test::MockModule;
25
use Test::Exception;
26
27
use t::lib::Mocks;
28
29
use_ok('Koha::PDF::Loader');
30
31
subtest 'PDF loader tests' => sub {
32
    plan tests => 4;
33
34
    #Create a self-destructing temporary file
35
    my $tmpdir = File::Temp::tempdir( CLEANUP => 1 );
36
    my ( $fh, $filename ) = File::Temp::tempfile( DIR => $tmpdir, SUFFIX => '.pdf' );
37
38
    #Create a blank PDF template for testing purposes using temporary file
39
    my $template = PDF::API2->new();
40
    $template->save($filename);
41
42
    my $to_string   = Koha::PDF::Loader->get_method_name( { method => 'to_string' } );
43
    my $from_string = Koha::PDF::Loader->get_method_name( { method => 'from_string' } );
44
    is( $to_string,   'to_string',   'to_string method name correct' );
45
    is( $from_string, 'from_string', 'from_string method name correct' );
46
47
    #Load PDF object from PDF template
48
    my $pdf = Koha::PDF::Loader->load_pdf_from_template( { pdf_template => $filename } );
49
    ok( $pdf,                   'Koha::PDF::Loader loaded something' );
50
    ok( $pdf->isa('PDF::API2'), 'PDF object is a PDF::API2 object' );
51
};
52
53
subtest 'PDF loader tests (legacy)' => sub {
54
    plan tests => 4;
55
56
    #Create a self-destructing temporary file
57
    my $tmpdir = File::Temp::tempdir( CLEANUP => 1 );
58
    my ( $fh, $filename ) = File::Temp::tempfile( DIR => $tmpdir, SUFFIX => '.pdf' );
59
60
    #Create a blank PDF template for testing purposes using temporary file
61
    my $template = PDF::API2->new();
62
    $template->save($filename);
63
64
    #Test the use of deprecated function names, which will be automatically used for older versions of PDF::API2
65
    Koha::PDF::Loader->set_method_name( { method => 'to_string',   name_to_use => 'stringify' } );
66
    Koha::PDF::Loader->set_method_name( { method => 'from_string', name_to_use => 'open_scalar' } );
67
68
    my $to_string   = Koha::PDF::Loader->get_method_name( { method => 'to_string' } );
69
    my $from_string = Koha::PDF::Loader->get_method_name( { method => 'from_string' } );
70
    is( $to_string,   'stringify',   'to_string method name correct' );
71
    is( $from_string, 'open_scalar', 'from_string method name correct' );
72
73
    #Load PDF object from PDF template
74
    my $pdf = Koha::PDF::Loader->load_pdf_from_template( { pdf_template => $filename } );
75
    ok( $pdf,                   'Koha::PDF::Loader loaded something using legacy method names' );
76
    ok( $pdf->isa('PDF::API2'), 'PDF object is a PDF::API2 object' );
77
};

Return to bug 41601