Bug 12508: adding an error message if a contract cannot be removed
[koha_fer] / C4 / Creators / PDF.pm
index d181bdb..738c34f 100644 (file)
@@ -21,9 +21,11 @@ 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');
+    use version; our $VERSION = qv('3.07.00.049');
 }
 
 sub _InitVars {
@@ -42,14 +44,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 +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);
 }
 
@@ -153,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;
@@ -183,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)
@@ -194,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";
@@ -282,6 +308,18 @@ sub SinglePage {
 sub StrWidth {
     my $self = shift;
     my ($string, $font, $fontSize) = @_;
+
+    # replace font code with path to TTF font file if need be
+    my $ttf = C4::Context->config('ttf');
+    if ( $ttf ) {
+        my $ttf_path = first { $_->{type} eq $font } @{ $ttf->{font} };
+        if ( -e $ttf_path->{content} ) {
+            $font = $ttf_path->{content};
+        } else {
+            warn "ERROR in koha-conf.xml -- missing <font type=\"$font\">/path/to/font.ttf</font>";
+        }
+    }
+
     return prStrWidth($string, $font, $fontSize);
 }