Bug 19386: Prevent t/db_dependent/SIP/Patron.t to fail randomly
[koha.git] / C4 / Creators / PDF.pm
index 1421a2b..9796feb 100644 (file)
@@ -4,27 +4,26 @@ package C4::Creators::PDF;
 #
 # 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., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use strict;
 use warnings;
 use PDF::Reuse;
 use PDF::Reuse::Barcode;
+use File::Temp;
+use List::Util qw/first/;
 
-BEGIN {
-    use version; our $VERSION = qv('1.0.0_1');
-}
 
 sub _InitVars {
     my $self = shift;
@@ -42,14 +41,27 @@ sub new {
     delete($opts{InitVars});
     prDocDir($opts{'DocDir'}) if $opts{'DocDir'};
     delete($opts{'DocDir'});
-    prFile(%opts);
+
+    my $fh = File::Temp->new( UNLINK => 0, SUFFIX => '.pdf' );
+    $opts{Name} = $self->{filename} = "$fh"; # filename
+    close $fh; # we need just filename
+
+    prFile(\%opts);
     bless ($self, $type);
     return $self;
 }
 
 sub End {
     my $self = shift;
+
     prEnd();
+
+    # slurp temporary filename and print it out for plack to pick up
+    local $/ = undef;
+    open(my $fh, '<', $self->{filename}) || die "$self->{filename}: $!";
+    print <$fh>;
+    close $fh;
+    unlink $self->{filename};
 }
 
 sub Add {
@@ -97,6 +109,17 @@ sub Field {
 sub Font {
     my $self = shift;
     my $fontName = shift;
+
+    my $ttf = C4::Context->config('ttf');
+
+    if ( $ttf ) {
+        my $ttf_path = first { $_->{type} eq $fontName } @{ $ttf->{font} };
+        if ( -e $ttf_path->{content} ) {
+            return prTTFont($ttf_path->{content});
+        } else {
+            warn "ERROR in koha-conf.xml -- missing <font type=\"$fontName\">/path/to/font.ttf</font>";
+        }
+    }
     return prFont($fontName);
 }
 
@@ -192,6 +215,10 @@ sub SinglePage {
 sub StrWidth {
     my $self = shift;
     my ($string, $font, $fontSize) = @_;
+
+    # replace font code with correct internal font
+    $font = C4::Creators::PDF->Font($font);
+
     return prStrWidth($string, $font, $fontSize);
 }
 
@@ -285,7 +312,7 @@ __END__
 
 =head1 NAME
 
-C4::Creators::PDF -   A class wrapper for PDF::Reuse and PDF::Reuse::Barcode to allow usage as a psuedo-object. For usage see
+C4::Creators::PDF -   A class wrapper for PDF::Reuse and PDF::Reuse::Barcode to allow usage as a pseudo-object. For usage see
                     PDF::Reuse documentation and C4::Creators::PDF code.
 
 =cut