Bug 8375: Use TrueType fonts in PDF::Reuse
[koha.git] / C4 / Creators / PDF.pm
index 41ff548..14bc9c4 100644 (file)
@@ -22,6 +22,7 @@ use warnings;
 use PDF::Reuse;
 use PDF::Reuse::Barcode;
 use File::Temp;
+use List::Util qw/first/;
 
 BEGIN {
     use version; our $VERSION = qv('3.07.00.049');
@@ -55,8 +56,7 @@ sub new {
 
 sub End {
     my $self = shift;
-    # if the pdf stream is utf8, explicitly set it to utf8; this avoids at lease some wide character errors -chris_n
-    utf8::encode($PDF::Reuse::stream) if utf8::is_utf8($PDF::Reuse::stream);
+
     prEnd();
 
     # slurp temporary filename and print it out for plack to pick up
@@ -112,6 +112,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);
 }
 
@@ -168,7 +179,7 @@ sub prAltJpeg
 {  my ($iData, $iWidth, $iHeight, $iFormat,$aiData, $aiWidth, $aiHeight, $aiFormat) = @_;
    my ($namnet, $utrad);
    if (! $PDF::Reuse::pos)                    # If no output is active, it is no use to continue
-   {   return undef;
+   {   return;
    }
    prJpegBlob($aiData, $aiWidth, $aiHeight, $aiFormat);
    my $altObjNr = $PDF::Reuse::objNr;
@@ -198,7 +209,7 @@ sub prJpegBlob
 {  my ($iData, $iWidth, $iHeight, $iFormat, $altArrayObjNr) = @_;
    my ($iLangd, $namnet, $utrad);
    if (! $PDF::Reuse::pos)                    # If no output is active, it is no use to continue
-   {   return undef;
+   {   return;
    }
    my $checkidOld = $PDF::Reuse::checkId;
    if (!$iFormat)
@@ -209,16 +220,16 @@ sub prJpegBlob
           $namnet = 'Ig' . $PDF::Reuse::imageNr;
           $PDF::Reuse::objNr++;
           $PDF::Reuse::objekt[$PDF::Reuse::objNr] = $PDF::Reuse::pos;
-          open (BILDFIL, "<$iFile") || errLog("Couldn't open $iFile, $!, aborts");
-          binmode BILDFIL;
+          open (my $fh, '<', "$iFile") || errLog("Couldn't open $iFile, $!, aborts");
+          binmode $fh;
           my $iStream;
-          sysread BILDFIL, $iStream, $iLangd;
+          sysread $fh, $iStream, $iLangd;
           $utrad = "$PDF::Reuse::objNr 0 obj\n<</Type/XObject/Subtype/Image/Name/$namnet" .
                     "/Width $iWidth /Height $iHeight /BitsPerComponent 8 " .
                     ($altArrayObjNr ? "/Alternates $altArrayObjNr 0 R " : "") .
                     "/Filter/DCTDecode/ColorSpace/DeviceRGB"
                     . "/Length $iLangd >>stream\n$iStream\nendstream\nendobj\n";
-          close BILDFIL;
+          close $fh;
           $PDF::Reuse::pos += syswrite $PDF::Reuse::UTFIL, $utrad;
           if ($PDF::Reuse::runfil)
           {  $PDF::Reuse::log .= "Cid~$PDF::Reuse::checkId\n";