Bugs 6634: manager_id not recorded for payments and rental charges
[koha.git] / C4 / Creators / PDF.pm
1 package C4::Creators::PDF;
2
3 # Copyright 2009 Foundations Bible College.
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22 use PDF::Reuse;
23 use PDF::Reuse::Barcode;
24
25 BEGIN {
26     use version; our $VERSION = qv('1.0.0_1');
27 }
28
29 sub _InitVars {
30     my $self = shift;
31     my $param = shift;
32     prInitVars($param);
33 }
34
35 sub new {
36     my $invocant = shift;
37     my $type = ref($invocant) || $invocant;
38     my %opts = @_;
39     my $self = {};
40     _InitVars() if ($opts{InitVars} == 0);
41     _InitVars($opts{InitVars}) if ($opts{InitVars} > 0);
42     delete($opts{InitVars});
43     prDocDir($opts{'DocDir'}) if $opts{'DocDir'};
44     delete($opts{'DocDir'});
45     prFile(%opts);
46     bless ($self, $type);
47     return $self;
48 }
49
50 sub End {
51     my $self = shift;
52     # if the pdf stream is utf8, explicitly set it to utf8; this avoids at lease some wide character errors -chris_n
53     utf8::encode($PDF::Reuse::stream) if utf8::is_utf8($PDF::Reuse::stream);
54     prEnd();
55 }
56
57 sub Add {
58     my $self = shift;
59     my $string = shift;
60     prAdd($string);
61 }
62
63 sub Bookmark {
64     my $self = shift;
65     my $reference = shift;
66     prBookmark($reference);
67 }
68
69 sub Compress {
70     my $self = shift;
71     my $directive = shift;
72     prCompress($directive);
73 }
74
75 sub Doc {
76     my $self = shift;
77     my %params = @_;
78     prDoc(%params);
79 }
80
81 sub DocForm {
82     my $self = shift;
83     my %params = @_;
84     return prDocForm(%params);
85 }
86
87 sub Extract {
88     my $self = shift;
89     my ($pdfFile, $pageNo, $oldInternalName) = @_;
90     return prExtract($pdfFile, $pageNo, $oldInternalName);
91 }
92
93 sub Field {
94     my $self = shift;
95     my ($fieldName, $value) = @_;
96     prField($fieldName, $value);
97 }
98
99 sub Font {
100     my $self = shift;
101     my $fontName = shift;
102     return prFont($fontName);
103 }
104
105 sub FontSize {
106     my $self = shift;
107     my $size = shift;
108     return prFontSize($size);
109 }
110
111 sub Form {
112     my $self = shift;
113     my %params = @_;
114     return prForm(%params);
115 }
116
117 sub GetLogBuffer {
118     my $self = shift;
119     return prGetLogBuffer();
120 }
121
122 sub GraphState {
123     my $self = shift;
124     my $string = shift;
125     prGraphState($string);
126 }
127
128 sub Image {
129     my $self = shift;
130     my %params = @_;
131     return prImage(%params);
132 }
133
134 sub Init {
135     my $self = shift;
136     my ($string, $duplicateCode) = @_;
137     prInit($string, $duplicateCode);
138 }
139
140 sub AltJpeg {
141     my $self = shift;
142     my ($imageData, $width, $height, $imageFormat, $altImageData, $altImageWidth, $altImageHeight, $altImageFormat) = @_;
143     return prAltJpeg($imageData, $width, $height, $imageFormat, $altImageData, $altImageWidth, $altImageHeight, $altImageFormat);
144 }
145
146 sub Jpeg {
147     my $self = shift;
148     my ($imageData, $width, $height, $imageFormat) = @_;
149     return prJpegBlob($imageData, $width, $height, $imageFormat);
150 }
151
152 # FIXME: This magick foo is an absolute hack until the maintainer of PDF::Reuse releases the next version which will include these features
153
154 sub prAltJpeg
155 {  my ($iData, $iWidth, $iHeight, $iFormat,$aiData, $aiWidth, $aiHeight, $aiFormat) = @_;
156    my ($namnet, $utrad);
157    if (! $PDF::Reuse::pos)                    # If no output is active, it is no use to continue
158    {   return undef;
159    }
160    prJpegBlob($aiData, $aiWidth, $aiHeight, $aiFormat);
161    my $altObjNr = $PDF::Reuse::objNr;
162    $PDF::Reuse::imageNr++;
163    $namnet = 'Ig' . $PDF::Reuse::imageNr;
164    $PDF::Reuse::objNr++;
165    $PDF::Reuse::objekt[$PDF::Reuse::objNr] = $PDF::Reuse::pos;
166    $utrad = "$PDF::Reuse::objNr 0 obj\n" .
167             "[ << /Image $altObjNr 0 R\n" .
168             "/DefaultForPrinting true\n" .
169             ">>\n" .
170             "]\n" .
171             "endobj\n";
172    $PDF::Reuse::pos += syswrite *PDF::Reuse::UTFIL, $utrad;
173    if ($PDF::Reuse::runfil)
174    {  $PDF::Reuse::log .= "Jpeg~AltImage\n";
175    }
176    $PDF::Reuse::objRef{$namnet} = $PDF::Reuse::objNr;
177    $namnet = prJpegBlob($iData, $iWidth, $iHeight, $iFormat, $PDF::Reuse::objNr);
178    if (! $PDF::Reuse::pos)
179    {  errLog("No output file, you have to call prFile first");
180    }
181    return $namnet;
182 }
183
184 sub prJpegBlob
185 {  my ($iData, $iWidth, $iHeight, $iFormat, $altArrayObjNr) = @_;
186    my ($iLangd, $namnet, $utrad);
187    if (! $PDF::Reuse::pos)                    # If no output is active, it is no use to continue
188    {   return undef;
189    }
190    my $checkidOld = $PDF::Reuse::checkId;
191    if (!$iFormat)
192    {   my ($iFile, $checkId) = findGet($iData, $checkidOld);
193        if ($iFile)
194        {  $iLangd = (stat($iFile))[7];
195           $PDF::Reuse::imageNr++;
196           $namnet = 'Ig' . $PDF::Reuse::imageNr;
197           $PDF::Reuse::objNr++;
198           $PDF::Reuse::objekt[$PDF::Reuse::objNr] = $PDF::Reuse::pos;
199           open (BILDFIL, "<$iFile") || errLog("Couldn't open $iFile, $!, aborts");
200           binmode BILDFIL;
201           my $iStream;
202           sysread BILDFIL, $iStream, $iLangd;
203           $utrad = "$PDF::Reuse::objNr 0 obj\n<</Type/XObject/Subtype/Image/Name/$namnet" .
204                     "/Width $iWidth /Height $iHeight /BitsPerComponent 8 " .
205                     ($altArrayObjNr ? "/Alternates $altArrayObjNr 0 R " : "") .
206                     "/Filter/DCTDecode/ColorSpace/DeviceRGB"
207                     . "/Length $iLangd >>stream\n$iStream\nendstream\nendobj\n";
208           close BILDFIL;
209           $PDF::Reuse::pos += syswrite $PDF::Reuse::UTFIL, $utrad;
210           if ($PDF::Reuse::runfil)
211           {  $PDF::Reuse::log .= "Cid~$PDF::Reuse::checkId\n";
212              $PDF::Reuse::log .= "Jpeg~$iFile~$iWidth~$iHeight\n";
213           }
214           $PDF::Reuse::objRef{$namnet} = $PDF::Reuse::objNr;
215        }
216        undef $checkId;
217    }
218    elsif ($iFormat == 1)
219    {  my $iBlob = $iData;
220       $iLangd = length($iBlob);
221       $PDF::Reuse::imageNr++;
222       $namnet = 'Ig' . $PDF::Reuse::imageNr;
223       $PDF::Reuse::objNr++;
224       $PDF::Reuse::objekt[$PDF::Reuse::objNr] = $PDF::Reuse::pos;
225       $utrad = "$PDF::Reuse::objNr 0 obj\n<</Type/XObject/Subtype/Image/Name/$namnet" .
226                 "/Width $iWidth /Height $iHeight /BitsPerComponent 8 " .
227                 ($altArrayObjNr ? "/Alternates $altArrayObjNr 0 R " : "") .
228                 "/Filter/DCTDecode/ColorSpace/DeviceRGB"
229                 . "/Length $iLangd >>stream\n$iBlob\nendstream\nendobj\n";
230       $PDF::Reuse::pos += syswrite *PDF::Reuse::UTFIL, $utrad;
231       if ($PDF::Reuse::runfil)
232       {  $PDF::Reuse::log .= "Jpeg~Blob~$iWidth~$iHeight\n";
233       }
234       $PDF::Reuse::objRef{$namnet} = $PDF::Reuse::objNr;
235    }
236    if (! $PDF::Reuse::pos)
237    {  errLog("No output file, you have to call prFile first");
238    }
239    return $namnet;
240 }
241
242 sub Js {
243     my $self = shift;
244     my $string_or_fileName = shift;
245     prJs($string_or_fileName);
246 }
247
248 sub Link {
249     my $self = shift;
250     my %params = @_;
251     prLink(%params);
252 }
253
254 sub Log {
255     my $self = shift;
256     my $string = shift;
257     prLog($string);
258 }
259
260 sub LogDir {
261     my $self = shift;
262     my $directory = shift;
263     prLogDir($directory);
264 }
265
266 sub Mbox {
267     my $self = shift;
268     my ($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY) = @_;
269     prMbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
270 }
271
272 sub Page {
273     my $self = shift;
274     my $noLog = shift;
275     prPage($noLog);
276 }
277
278 sub SinglePage {
279     my $self = shift;
280     my ($file, $pageNumber) = @_;
281     return prSinglePage($file, $pageNumber);
282 }
283
284 sub StrWidth {
285     my $self = shift;
286     my ($string, $font, $fontSize) = @_;
287     return prStrWidth($string, $font, $fontSize);
288 }
289
290 sub Text {
291     my $self = shift;
292     my ($x, $y, $string, $align, $rotation) = @_;
293     return prText($x, $y, $string, $align, $rotation);
294 }
295
296 sub TTFont {
297     my $self = shift;
298     my $path = shift;
299     return prTTFont($path);
300 }
301
302 sub Code128 {
303     my $self = shift;
304     my %opts = @_;
305     PDF::Reuse::Barcode::Code128(%opts);
306 }
307
308 sub Code39 {
309     my $self = shift;
310     my %opts = @_;
311     PDF::Reuse::Barcode::Code39(%opts);
312 }
313
314 sub COOP2of5 {
315     my $self = shift;
316     my %opts = @_;
317     PDF::Reuse::Barcode::COOP2of5(%opts);
318 }
319
320 sub EAN13 {
321     my $self = shift;
322     my %opts = @_;
323     PDF::Reuse::Barcode::EAN13(%opts);
324 }
325
326 sub EAN8 {
327     my $self = shift;
328     my %opts = @_;
329     PDF::Reuse::Barcode::EAN8(%opts);
330 }
331
332 sub IATA2of5 {
333     my $self = shift;
334     my %opts = @_;
335     PDF::Reuse::Barcode::IATA2of5(%opts);
336 }
337
338 sub Industrial2of5 {
339     my $self = shift;
340     my %opts = @_;
341     PDF::Reuse::Barcode::Industrial2of5(%opts);
342 }
343
344 sub ITF {
345     my $self = shift;
346     my %opts = @_;
347     PDF::Reuse::Barcode::ITF(%opts);
348 }
349
350 sub Matrix2of5 {
351     my $self = shift;
352     my %opts = @_;
353     PDF::Reuse::Barcode::Matrix2of5(%opts);
354 }
355
356 sub NW7 {
357     my $self = shift;
358     my %opts = @_;
359     PDF::Reuse::Barcode::NW7(%opts);
360 }
361
362 sub UPCA {
363     my $self = shift;
364     my %opts = @_;
365     PDF::Reuse::Barcode::UPCA(%opts);
366 }
367
368 sub UPCE {
369     my $self = shift;
370     my %opts = @_;
371     PDF::Reuse::Barcode::UPCE(%opts);
372 }
373
374 1;
375 __END__
376
377
378 =head1 NAME
379
380 C4::Creators::PDF -   A class wrapper for PDF::Reuse and PDF::Reuse::Barcode to allow usage as a psuedo-object. For usage see
381                     PDF::Reuse documentation and C4::Creators::PDF code.
382
383 =cut
384
385 =head1 AUTHOR
386
387 Chris Nighswonger <cnighswonger AT foundations DOT edu>
388
389 =cut