File Coverage

File:C4/Creators/PDF.pm
Coverage:27.5%

linestmtbrancondsubtimecode
1package 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
6
6
6
37
23
192
use strict;
21
6
6
6
39
19
218
use warnings;
22
6
6
6
59085
1111309
2291
use PDF::Reuse;
23
6
6
6
25809
30929
585
use PDF::Reuse::Barcode;
24
25BEGIN {
26
6
6
6
6
308
3373
133
585
    use version; our $VERSION = qv('1.0.0_1');
27}
28
29sub _InitVars {
30
1
22
    my $self = shift;
31
1
20
    my $param = shift;
32
1
31937
    prInitVars($param);
33}
34
35sub new {
36
1
464982
    my $invocant = shift;
37
1
40
    my $type = ref($invocant) || $invocant;
38
1
31
    my %opts = @_;
39
1
27
    my $self = {};
40
1
29
    _InitVars() if ($opts{InitVars} == 0);
41
1
50
    _InitVars($opts{InitVars}) if ($opts{InitVars} > 0);
42
1
3
    delete($opts{InitVars});
43
1
9
    prDocDir($opts{'DocDir'}) if $opts{'DocDir'};
44
1
1
    delete($opts{'DocDir'});
45
1
10
    prFile(%opts);
46
1
248
    bless ($self, $type);
47
1
8
    return $self;
48}
49
50sub End {
51
1
1867
    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
1
6
    utf8::encode($PDF::Reuse::stream) if utf8::is_utf8($PDF::Reuse::stream);
54
1
19
    prEnd();
55}
56
57sub Add {
58
1
1027
    my $self = shift;
59
1
3
    my $string = shift;
60
1
7
    prAdd($string);
61}
62
63sub Bookmark {
64
1
395
    my $self = shift;
65
1
2
    my $reference = shift;
66
1
42206
    prBookmark($reference);
67}
68
69sub Compress {
70
1
535
    my $self = shift;
71
1
0
    my $directive = shift;
72
1
124451
    prCompress($directive);
73}
74
75sub Doc {
76
0
0
    my $self = shift;
77
0
0
    my %params = @_;
78
0
0
    prDoc(%params);
79}
80
81sub DocForm {
82
0
0
    my $self = shift;
83
0
0
    my %params = @_;
84
0
0
    return prDocForm(%params);
85}
86
87sub Extract {
88
0
0
    my $self = shift;
89
0
0
    my ($pdfFile, $pageNo, $oldInternalName) = @_;
90
0
0
    return prExtract($pdfFile, $pageNo, $oldInternalName);
91}
92
93sub Field {
94
0
0
    my $self = shift;
95
0
0
    my ($fieldName, $value) = @_;
96
0
0
    prField($fieldName, $value);
97}
98
99sub Font {
100
1
499
    my $self = shift;
101
1
4
    my $fontName = shift;
102
1
7
    return prFont($fontName);
103}
104
105sub FontSize {
106
3
4410
    my $self = shift;
107
3
5
    my $size = shift;
108
3
21
    return prFontSize($size);
109}
110
111sub Form {
112
0
0
    my $self = shift;
113
0
0
    my %params = @_;
114
0
0
    return prForm(%params);
115}
116
117sub GetLogBuffer {
118
0
0
    my $self = shift;
119
0
0
    return prGetLogBuffer();
120}
121
122sub GraphState {
123
0
0
    my $self = shift;
124
0
0
    my $string = shift;
125
0
0
    prGraphState($string);
126}
127
128sub Image {
129
0
0
    my $self = shift;
130
0
0
    my %params = @_;
131
0
0
    return prImage(%params);
132}
133
134sub Init {
135
0
0
    my $self = shift;
136
0
0
    my ($string, $duplicateCode) = @_;
137
0
0
    prInit($string, $duplicateCode);
138}
139
140sub AltJpeg {
141
0
0
    my $self = shift;
142
0
0
    my ($imageData, $width, $height, $imageFormat, $altImageData, $altImageWidth, $altImageHeight, $altImageFormat) = @_;
143
0
0
    return prAltJpeg($imageData, $width, $height, $imageFormat, $altImageData, $altImageWidth, $altImageHeight, $altImageFormat);
144}
145
146sub Jpeg {
147
0
0
    my $self = shift;
148
0
0
    my ($imageData, $width, $height, $imageFormat) = @_;
149
0
0
    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
154sub prAltJpeg
155
0
0
{ my ($iData, $iWidth, $iHeight, $iFormat,$aiData, $aiWidth, $aiHeight, $aiFormat) = @_;
156
0
0
   my ($namnet, $utrad);
157
0
0
   if (! $PDF::Reuse::pos) # If no output is active, it is no use to continue
158
0
0
   { return undef;
159   }
160
0
0
   prJpegBlob($aiData, $aiWidth, $aiHeight, $aiFormat);
161
0
0
   my $altObjNr = $PDF::Reuse::objNr;
162
0
0
   $PDF::Reuse::imageNr++;
163
0
0
   $namnet = 'Ig' . $PDF::Reuse::imageNr;
164
0
0
   $PDF::Reuse::objNr++;
165
0
0
   $PDF::Reuse::objekt[$PDF::Reuse::objNr] = $PDF::Reuse::pos;
166
0
0
   $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
0
0
   $PDF::Reuse::pos += syswrite *PDF::Reuse::UTFIL, $utrad;
173
0
0
   if ($PDF::Reuse::runfil)
174
0
0
   { $PDF::Reuse::log .= "Jpeg~AltImage\n";
175   }
176
0
0
   $PDF::Reuse::objRef{$namnet} = $PDF::Reuse::objNr;
177
0
0
   $namnet = prJpegBlob($iData, $iWidth, $iHeight, $iFormat, $PDF::Reuse::objNr);
178
0
0
   if (! $PDF::Reuse::pos)
179
0
0
   { errLog("No output file, you have to call prFile first");
180   }
181
0
0
   return $namnet;
182}
183
184sub prJpegBlob
185
0
0
{ my ($iData, $iWidth, $iHeight, $iFormat, $altArrayObjNr) = @_;
186
0
0
   my ($iLangd, $namnet, $utrad);
187
0
0
   if (! $PDF::Reuse::pos) # If no output is active, it is no use to continue
188
0
0
   { return undef;
189   }
190
0
0
   my $checkidOld = $PDF::Reuse::checkId;
191
0
0
   if (!$iFormat)
192
0
0
   { my ($iFile, $checkId) = findGet($iData, $checkidOld);
193
0
0
       if ($iFile)
194
0
0
       { $iLangd = (stat($iFile))[7];
195
0
0
          $PDF::Reuse::imageNr++;
196
0
0
          $namnet = 'Ig' . $PDF::Reuse::imageNr;
197
0
0
          $PDF::Reuse::objNr++;
198
0
0
          $PDF::Reuse::objekt[$PDF::Reuse::objNr] = $PDF::Reuse::pos;
199
0
0
          open (BILDFIL, "<$iFile") || errLog("Couldn't open $iFile, $!, aborts");
200
0
0
          binmode BILDFIL;
201
0
0
          my $iStream;
202
0
0
          sysread BILDFIL, $iStream, $iLangd;
203
0
0
          $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
0
0
          close BILDFIL;
209
0
0
          $PDF::Reuse::pos += syswrite $PDF::Reuse::UTFIL, $utrad;
210
0
0
          if ($PDF::Reuse::runfil)
211
0
0
          { $PDF::Reuse::log .= "Cid~$PDF::Reuse::checkId\n";
212
0
0
             $PDF::Reuse::log .= "Jpeg~$iFile~$iWidth~$iHeight\n";
213          }
214
0
0
          $PDF::Reuse::objRef{$namnet} = $PDF::Reuse::objNr;
215       }
216
0
0
       undef $checkId;
217   }
218   elsif ($iFormat == 1)
219
0
0
   { my $iBlob = $iData;
220
0
0
      $iLangd = length($iBlob);
221
0
0
      $PDF::Reuse::imageNr++;
222
0
0
      $namnet = 'Ig' . $PDF::Reuse::imageNr;
223
0
0
      $PDF::Reuse::objNr++;
224
0
0
      $PDF::Reuse::objekt[$PDF::Reuse::objNr] = $PDF::Reuse::pos;
225
0
0
      $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
0
0
      $PDF::Reuse::pos += syswrite *PDF::Reuse::UTFIL, $utrad;
231
0
0
      if ($PDF::Reuse::runfil)
232
0
0
      { $PDF::Reuse::log .= "Jpeg~Blob~$iWidth~$iHeight\n";
233      }
234
0
0
      $PDF::Reuse::objRef{$namnet} = $PDF::Reuse::objNr;
235   }
236
0
0
   if (! $PDF::Reuse::pos)
237
0
0
   { errLog("No output file, you have to call prFile first");
238   }
239
0
0
   return $namnet;
240}
241
242sub Js {
243
0
0
    my $self = shift;
244
0
0
    my $string_or_fileName = shift;
245
0
0
    prJs($string_or_fileName);
246}
247
248sub Link {
249
0
0
    my $self = shift;
250
0
0
    my %params = @_;
251
0
0
    prLink(%params);
252}
253
254sub Log {
255
0
0
    my $self = shift;
256
0
0
    my $string = shift;
257
0
0
    prLog($string);
258}
259
260sub LogDir {
261
0
0
    my $self = shift;
262
0
0
    my $directory = shift;
263
0
0
    prLogDir($directory);
264}
265
266sub Mbox {
267
0
0
    my $self = shift;
268
0
0
    my ($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY) = @_;
269
0
0
    prMbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
270}
271
272sub Page {
273
1
19
    my $self = shift;
274
1
2
    my $noLog = shift;
275
1
8
    prPage($noLog);
276}
277
278sub SinglePage {
279
0
0
    my $self = shift;
280
0
0
    my ($file, $pageNumber) = @_;
281
0
0
    return prSinglePage($file, $pageNumber);
282}
283
284sub StrWidth {
285
1
29920
    my $self = shift;
286
1
4
    my ($string, $font, $fontSize) = @_;
287
1
7
    return prStrWidth($string, $font, $fontSize);
288}
289
290sub Text {
291
1
12071
    my $self = shift;
292
1
4
    my ($x, $y, $string, $align, $rotation) = @_;
293
1
10
    return prText($x, $y, $string, $align, $rotation);
294}
295
296sub TTFont {
297
0
    my $self = shift;
298
0
    my $path = shift;
299
0
    return prTTFont($path);
300}
301
302sub Code128 {
303
0
    my $self = shift;
304
0
    my %opts = @_;
305
0
    PDF::Reuse::Barcode::Code128(%opts);
306}
307
308sub Code39 {
309
0
    my $self = shift;
310
0
    my %opts = @_;
311
0
    PDF::Reuse::Barcode::Code39(%opts);
312}
313
314sub COOP2of5 {
315
0
    my $self = shift;
316
0
    my %opts = @_;
317
0
    PDF::Reuse::Barcode::COOP2of5(%opts);
318}
319
320sub EAN13 {
321
0
    my $self = shift;
322
0
    my %opts = @_;
323
0
    PDF::Reuse::Barcode::EAN13(%opts);
324}
325
326sub EAN8 {
327
0
    my $self = shift;
328
0
    my %opts = @_;
329
0
    PDF::Reuse::Barcode::EAN8(%opts);
330}
331
332sub IATA2of5 {
333
0
    my $self = shift;
334
0
    my %opts = @_;
335
0
    PDF::Reuse::Barcode::IATA2of5(%opts);
336}
337
338sub Industrial2of5 {
339
0
    my $self = shift;
340
0
    my %opts = @_;
341
0
    PDF::Reuse::Barcode::Industrial2of5(%opts);
342}
343
344sub ITF {
345
0
    my $self = shift;
346
0
    my %opts = @_;
347
0
    PDF::Reuse::Barcode::ITF(%opts);
348}
349
350sub Matrix2of5 {
351
0
    my $self = shift;
352
0
    my %opts = @_;
353
0
    PDF::Reuse::Barcode::Matrix2of5(%opts);
354}
355
356sub NW7 {
357
0
    my $self = shift;
358
0
    my %opts = @_;
359
0
    PDF::Reuse::Barcode::NW7(%opts);
360}
361
362sub UPCA {
363
0
    my $self = shift;
364
0
    my %opts = @_;
365
0
    PDF::Reuse::Barcode::UPCA(%opts);
366}
367
368sub UPCE {
369
0
    my $self = shift;
370
0
    my %opts = @_;
371
0
    PDF::Reuse::Barcode::UPCE(%opts);
372}
373
3741;