X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=C4%2FCreators%2FPDF.pm;h=871e32bd87a78a92f19e734cc62d9fe49900cc96;hb=4f9174349c99574df0933f2b660938998580d8ba;hp=1421a2b6971661864b2c8e62af1373dfded61512;hpb=ff9b43e386585a558ecd0b4d5489d4fa0a731b62;p=koha_fer diff --git a/C4/Creators/PDF.pm b/C4/Creators/PDF.pm index 1421a2b697..871e32bd87 100644 --- a/C4/Creators/PDF.pm +++ b/C4/Creators/PDF.pm @@ -13,17 +13,18 @@ package C4::Creators::PDF; # 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. use strict; use warnings; use PDF::Reuse; use PDF::Reuse::Barcode; +use File::Temp; BEGIN { - use version; our $VERSION = qv('1.0.0_1'); + use version; our $VERSION = qv('3.07.00.049'); } sub _InitVars { @@ -42,14 +43,28 @@ 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; + # 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 { @@ -144,7 +159,97 @@ sub AltJpeg { sub Jpeg { my $self = shift; my ($imageData, $width, $height, $imageFormat) = @_; - return prJpeg($imageData, $width, $height, $imageFormat); + return prJpegBlob($imageData, $width, $height, $imageFormat); +} + +# FIXME: This magick foo is an absolute hack until the maintainer of PDF::Reuse releases the next version which will include these features + +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; + } + prJpegBlob($aiData, $aiWidth, $aiHeight, $aiFormat); + my $altObjNr = $PDF::Reuse::objNr; + $PDF::Reuse::imageNr++; + $namnet = 'Ig' . $PDF::Reuse::imageNr; + $PDF::Reuse::objNr++; + $PDF::Reuse::objekt[$PDF::Reuse::objNr] = $PDF::Reuse::pos; + $utrad = "$PDF::Reuse::objNr 0 obj\n" . + "[ << /Image $altObjNr 0 R\n" . + "/DefaultForPrinting true\n" . + ">>\n" . + "]\n" . + "endobj\n"; + $PDF::Reuse::pos += syswrite *PDF::Reuse::UTFIL, $utrad; + if ($PDF::Reuse::runfil) + { $PDF::Reuse::log .= "Jpeg~AltImage\n"; + } + $PDF::Reuse::objRef{$namnet} = $PDF::Reuse::objNr; + $namnet = prJpegBlob($iData, $iWidth, $iHeight, $iFormat, $PDF::Reuse::objNr); + if (! $PDF::Reuse::pos) + { errLog("No output file, you have to call prFile first"); + } + return $namnet; +} + +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; + } + my $checkidOld = $PDF::Reuse::checkId; + if (!$iFormat) + { my ($iFile, $checkId) = findGet($iData, $checkidOld); + if ($iFile) + { $iLangd = (stat($iFile))[7]; + $PDF::Reuse::imageNr++; + $namnet = 'Ig' . $PDF::Reuse::imageNr; + $PDF::Reuse::objNr++; + $PDF::Reuse::objekt[$PDF::Reuse::objNr] = $PDF::Reuse::pos; + open (my $fh, '<', "$iFile") || errLog("Couldn't open $iFile, $!, aborts"); + binmode $fh; + my $iStream; + sysread $fh, $iStream, $iLangd; + $utrad = "$PDF::Reuse::objNr 0 obj\n<>stream\n$iStream\nendstream\nendobj\n"; + close $fh; + $PDF::Reuse::pos += syswrite $PDF::Reuse::UTFIL, $utrad; + if ($PDF::Reuse::runfil) + { $PDF::Reuse::log .= "Cid~$PDF::Reuse::checkId\n"; + $PDF::Reuse::log .= "Jpeg~$iFile~$iWidth~$iHeight\n"; + } + $PDF::Reuse::objRef{$namnet} = $PDF::Reuse::objNr; + } + undef $checkId; + } + elsif ($iFormat == 1) + { my $iBlob = $iData; + $iLangd = length($iBlob); + $PDF::Reuse::imageNr++; + $namnet = 'Ig' . $PDF::Reuse::imageNr; + $PDF::Reuse::objNr++; + $PDF::Reuse::objekt[$PDF::Reuse::objNr] = $PDF::Reuse::pos; + $utrad = "$PDF::Reuse::objNr 0 obj\n<>stream\n$iBlob\nendstream\nendobj\n"; + $PDF::Reuse::pos += syswrite *PDF::Reuse::UTFIL, $utrad; + if ($PDF::Reuse::runfil) + { $PDF::Reuse::log .= "Jpeg~Blob~$iWidth~$iHeight\n"; + } + $PDF::Reuse::objRef{$namnet} = $PDF::Reuse::objNr; + } + if (! $PDF::Reuse::pos) + { errLog("No output file, you have to call prFile first"); + } + return $namnet; } sub Js {