Bug 7952 - PDF::Reuse under plack writes to console STDOUT instead to browser
[srvgit] / C4 / Creators / PDF.pm
index 413b064..41ff548 100644 (file)
@@ -21,6 +21,7 @@ use strict;
 use warnings;
 use PDF::Reuse;
 use PDF::Reuse::Barcode;
+use File::Temp;
 
 BEGIN {
     use version; our $VERSION = qv('3.07.00.049');
@@ -42,7 +43,12 @@ 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;
 }
@@ -52,6 +58,13 @@ sub End {
     # 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
+    local $/ = undef;
+    open(my $fh, '<', $self->{filename}) || die "$self->{filename}: $!";
+    print <$fh>;
+    close $fh;
+    unlink $self->{filename};
 }
 
 sub Add {