Bug 5489: Send hold email to branch email address if it exists instead of koha email...
[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     prEnd();
53 }
54
55 sub Add {
56     my $self = shift;
57     my $string = shift;
58     prAdd($string);
59 }
60
61 sub Bookmark {
62     my $self = shift;
63     my $reference = shift;
64     prBookmark($reference);
65 }
66
67 sub Compress {
68     my $self = shift;
69     my $directive = shift;
70     prCompress($directive);
71 }
72
73 sub Doc {
74     my $self = shift;
75     my %params = @_;
76     prDoc(%params);
77 }
78
79 sub DocForm {
80     my $self = shift;
81     my %params = @_;
82     return prDocForm(%params);
83 }
84
85 sub Extract {
86     my $self = shift;
87     my ($pdfFile, $pageNo, $oldInternalName) = @_;
88     return prExtract($pdfFile, $pageNo, $oldInternalName);
89 }
90
91 sub Field {
92     my $self = shift;
93     my ($fieldName, $value) = @_;
94     prField($fieldName, $value);
95 }
96
97 sub Font {
98     my $self = shift;
99     my $fontName = shift;
100     return prFont($fontName);
101 }
102
103 sub FontSize {
104     my $self = shift;
105     my $size = shift;
106     return prFontSize($size);
107 }
108
109 sub Form {
110     my $self = shift;
111     my %params = @_;
112     return prForm(%params);
113 }
114
115 sub GetLogBuffer {
116     my $self = shift;
117     return prGetLogBuffer();
118 }
119
120 sub GraphState {
121     my $self = shift;
122     my $string = shift;
123     prGraphState($string);
124 }
125
126 sub Image {
127     my $self = shift;
128     my %params = @_;
129     return prImage(%params);
130 }
131
132 sub Init {
133     my $self = shift;
134     my ($string, $duplicateCode) = @_;
135     prInit($string, $duplicateCode);
136 }
137
138 sub AltJpeg {
139     my $self = shift;
140     my ($imageData, $width, $height, $imageFormat, $altImageData, $altImageWidth, $altImageHeight, $altImageFormat) = @_;
141     return prAltJpeg($imageData, $width, $height, $imageFormat, $altImageData, $altImageWidth, $altImageHeight, $altImageFormat);
142 }
143
144 sub Jpeg {
145     my $self = shift;
146     my ($imageData, $width, $height, $imageFormat) = @_;
147     return prJpegBlob($imageData, $width, $height, $imageFormat);
148 }
149
150 # FIXME: This magick foo is an absolute hack until the maintainer of PDF::Reuse releases the next version which will include these features
151
152 sub prAltJpeg
153 {  my ($iData, $iWidth, $iHeight, $iFormat,$aiData, $aiWidth, $aiHeight, $aiFormat) = @_;
154    my ($namnet, $utrad);
155    if (! $PDF::Reuse::pos)                    # If no output is active, it is no use to continue
156    {   return undef;
157    }
158    prJpegBlob($aiData, $aiWidth, $aiHeight, $aiFormat);
159    my $altObjNr = $PDF::Reuse::objNr;
160    $PDF::Reuse::imageNr++;
161    $namnet = 'Ig' . $PDF::Reuse::imageNr;
162    $PDF::Reuse::objNr++;
163    $PDF::Reuse::objekt[$PDF::Reuse::objNr] = $PDF::Reuse::pos;
164    $utrad = "$PDF::Reuse::objNr 0 obj\n" .
165             "[ << /Image $altObjNr 0 R\n" .
166             "/DefaultForPrinting true\n" .
167             ">>\n" .
168             "]\n" .
169             "endobj\n";
170    $PDF::Reuse::pos += syswrite *PDF::Reuse::UTFIL, $utrad;
171    if ($PDF::Reuse::runfil)
172    {  $PDF::Reuse::log .= "Jpeg~AltImage\n";
173    }
174    $PDF::Reuse::objRef{$namnet} = $PDF::Reuse::objNr;
175    $namnet = prJpegBlob($iData, $iWidth, $iHeight, $iFormat, $PDF::Reuse::objNr);
176    if (! $PDF::Reuse::pos)
177    {  errLog("No output file, you have to call prFile first");
178    }
179    return $namnet;
180 }
181
182 sub prJpegBlob
183 {  my ($iData, $iWidth, $iHeight, $iFormat, $altArrayObjNr) = @_;
184    my ($iLangd, $namnet, $utrad);
185    if (! $PDF::Reuse::pos)                    # If no output is active, it is no use to continue
186    {   return undef;
187    }
188    my $checkidOld = $PDF::Reuse::checkId;
189    if (!$iFormat)
190    {   my ($iFile, $checkId) = findGet($iData, $checkidOld);
191        if ($iFile)
192        {  $iLangd = (stat($iFile))[7];
193           $PDF::Reuse::imageNr++;
194           $namnet = 'Ig' . $PDF::Reuse::imageNr;
195           $PDF::Reuse::objNr++;
196           $PDF::Reuse::objekt[$PDF::Reuse::objNr] = $PDF::Reuse::pos;
197           open (BILDFIL, "<$iFile") || errLog("Couldn't open $iFile, $!, aborts");
198           binmode BILDFIL;
199           my $iStream;
200           sysread BILDFIL, $iStream, $iLangd;
201           $utrad = "$PDF::Reuse::objNr 0 obj\n<</Type/XObject/Subtype/Image/Name/$namnet" .
202                     "/Width $iWidth /Height $iHeight /BitsPerComponent 8 " .
203                     ($altArrayObjNr ? "/Alternates $altArrayObjNr 0 R " : "") .
204                     "/Filter/DCTDecode/ColorSpace/DeviceRGB"
205                     . "/Length $iLangd >>stream\n$iStream\nendstream\nendobj\n";
206           close BILDFIL;
207           $PDF::Reuse::pos += syswrite $PDF::Reuse::UTFIL, $utrad;
208           if ($PDF::Reuse::runfil)
209           {  $PDF::Reuse::log .= "Cid~$PDF::Reuse::checkId\n";
210              $PDF::Reuse::log .= "Jpeg~$iFile~$iWidth~$iHeight\n";
211           }
212           $PDF::Reuse::objRef{$namnet} = $PDF::Reuse::objNr;
213        }
214        undef $checkId;
215    }
216    elsif ($iFormat == 1)
217    {  my $iBlob = $iData;
218       $iLangd = length($iBlob);
219       $PDF::Reuse::imageNr++;
220       $namnet = 'Ig' . $PDF::Reuse::imageNr;
221       $PDF::Reuse::objNr++;
222       $PDF::Reuse::objekt[$PDF::Reuse::objNr] = $PDF::Reuse::pos;
223       $utrad = "$PDF::Reuse::objNr 0 obj\n<</Type/XObject/Subtype/Image/Name/$namnet" .
224                 "/Width $iWidth /Height $iHeight /BitsPerComponent 8 " .
225                 ($altArrayObjNr ? "/Alternates $altArrayObjNr 0 R " : "") .
226                 "/Filter/DCTDecode/ColorSpace/DeviceRGB"
227                 . "/Length $iLangd >>stream\n$iBlob\nendstream\nendobj\n";
228       $PDF::Reuse::pos += syswrite *PDF::Reuse::UTFIL, $utrad;
229       if ($PDF::Reuse::runfil)
230       {  $PDF::Reuse::log .= "Jpeg~Blob~$iWidth~$iHeight\n";
231       }
232       $PDF::Reuse::objRef{$namnet} = $PDF::Reuse::objNr;
233    }
234    if (! $PDF::Reuse::pos)
235    {  errLog("No output file, you have to call prFile first");
236    }
237    return $namnet;
238 }
239
240 sub Js {
241     my $self = shift;
242     my $string_or_fileName = shift;
243     prJs($string_or_fileName);
244 }
245
246 sub Link {
247     my $self = shift;
248     my %params = @_;
249     prLink(%params);
250 }
251
252 sub Log {
253     my $self = shift;
254     my $string = shift;
255     prLog($string);
256 }
257
258 sub LogDir {
259     my $self = shift;
260     my $directory = shift;
261     prLogDir($directory);
262 }
263
264 sub Mbox {
265     my $self = shift;
266     my ($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY) = @_;
267     prMbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
268 }
269
270 sub Page {
271     my $self = shift;
272     my $noLog = shift;
273     prPage($noLog);
274 }
275
276 sub SinglePage {
277     my $self = shift;
278     my ($file, $pageNumber) = @_;
279     return prSinglePage($file, $pageNumber);
280 }
281
282 sub StrWidth {
283     my $self = shift;
284     my ($string, $font, $fontSize) = @_;
285     return prStrWidth($string, $font, $fontSize);
286 }
287
288 sub Text {
289     my $self = shift;
290     my ($x, $y, $string, $align, $rotation) = @_;
291     return prText($x, $y, $string, $align, $rotation);
292 }
293
294 sub TTFont {
295     my $self = shift;
296     my $path = shift;
297     return prTTFont($path);
298 }
299
300 sub Code128 {
301     my $self = shift;
302     my %opts = @_;
303     PDF::Reuse::Barcode::Code128(%opts);
304 }
305
306 sub Code39 {
307     my $self = shift;
308     my %opts = @_;
309     PDF::Reuse::Barcode::Code39(%opts);
310 }
311
312 sub COOP2of5 {
313     my $self = shift;
314     my %opts = @_;
315     PDF::Reuse::Barcode::COOP2of5(%opts);
316 }
317
318 sub EAN13 {
319     my $self = shift;
320     my %opts = @_;
321     PDF::Reuse::Barcode::EAN13(%opts);
322 }
323
324 sub EAN8 {
325     my $self = shift;
326     my %opts = @_;
327     PDF::Reuse::Barcode::EAN8(%opts);
328 }
329
330 sub IATA2of5 {
331     my $self = shift;
332     my %opts = @_;
333     PDF::Reuse::Barcode::IATA2of5(%opts);
334 }
335
336 sub Industrial2of5 {
337     my $self = shift;
338     my %opts = @_;
339     PDF::Reuse::Barcode::Industrial2of5(%opts);
340 }
341
342 sub ITF {
343     my $self = shift;
344     my %opts = @_;
345     PDF::Reuse::Barcode::ITF(%opts);
346 }
347
348 sub Matrix2of5 {
349     my $self = shift;
350     my %opts = @_;
351     PDF::Reuse::Barcode::Matrix2of5(%opts);
352 }
353
354 sub NW7 {
355     my $self = shift;
356     my %opts = @_;
357     PDF::Reuse::Barcode::NW7(%opts);
358 }
359
360 sub UPCA {
361     my $self = shift;
362     my %opts = @_;
363     PDF::Reuse::Barcode::UPCA(%opts);
364 }
365
366 sub UPCE {
367     my $self = shift;
368     my %opts = @_;
369     PDF::Reuse::Barcode::UPCE(%opts);
370 }
371
372 1;
373 __END__
374
375
376 =head1 NAME
377
378 C4::Creators::PDF -   A class wrapper for PDF::Reuse and PDF::Reuse::Barcode to allow usage as a psuedo-object. For usage see
379                     PDF::Reuse documentation and C4::Creators::PDF code.
380
381 =cut
382
383 =head1 AUTHOR
384
385 Chris Nighswonger <cnighswonger AT foundations DOT edu>
386
387 =cut