Bug 17600: Standardize our EXPORT_OK
[srvgit] / 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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22 use PDF::Reuse qw(
23     prAdd
24     prAltJpeg
25     prBookmark
26     prCompress
27     prDoc
28     prDocDir
29     prDocForm
30     prEnd
31     prExtract
32     prField
33     prFile
34     prFont
35     prFontSize
36     prForm
37     prGetLogBuffer
38     prGraphState
39     prImage
40     prInit
41     prInitVars
42     prJpeg
43     prJs
44     prLink
45     prLog
46     prLogDir
47     prMbox
48     prPage
49     prSinglePage
50     prStrWidth
51     prText
52     prTTFont
53 );
54 use PDF::Reuse::Barcode;
55 use File::Temp;
56 use List::Util qw( first );
57
58
59 sub _InitVars {
60     my $self = shift;
61     my $param = shift;
62     prInitVars($param);
63 }
64
65 sub new {
66     my $invocant = shift;
67     my $type = ref($invocant) || $invocant;
68     my %opts = @_;
69     my $self = {};
70     _InitVars() if ($opts{InitVars} == 0);
71     _InitVars($opts{InitVars}) if ($opts{InitVars} > 0);
72     delete($opts{InitVars});
73     prDocDir($opts{'DocDir'}) if $opts{'DocDir'};
74     delete($opts{'DocDir'});
75
76     my $fh = File::Temp->new( UNLINK => 0, SUFFIX => '.pdf' );
77     $opts{Name} = $self->{filename} = "$fh"; # filename
78     close $fh; # we need just filename
79
80     prFile(\%opts);
81     bless ($self, $type);
82     return $self;
83 }
84
85 sub End {
86     my $self = shift;
87
88     prEnd();
89
90     # slurp temporary filename and print it out for plack to pick up
91     local $/ = undef;
92     open(my $fh, '<', $self->{filename}) || die "$self->{filename}: $!";
93     print <$fh>;
94     close $fh;
95     unlink $self->{filename};
96 }
97
98 sub Add {
99     my $self = shift;
100     my $string = shift;
101     prAdd($string);
102 }
103
104 sub Bookmark {
105     my $self = shift;
106     my $reference = shift;
107     prBookmark($reference);
108 }
109
110 sub Compress {
111     my $self = shift;
112     my $directive = shift;
113     prCompress($directive);
114 }
115
116 sub Doc {
117     my $self = shift;
118     my %params = @_;
119     prDoc(%params);
120 }
121
122 sub DocForm {
123     my $self = shift;
124     my %params = @_;
125     return prDocForm(%params);
126 }
127
128 sub Extract {
129     my $self = shift;
130     my ($pdfFile, $pageNo, $oldInternalName) = @_;
131     return prExtract($pdfFile, $pageNo, $oldInternalName);
132 }
133
134 sub Field {
135     my $self = shift;
136     my ($fieldName, $value) = @_;
137     prField($fieldName, $value);
138 }
139
140 sub Font {
141     my $self = shift;
142     my $fontName = shift;
143
144     my $ttf = C4::Context->config('ttf');
145
146     if ( $ttf ) {
147         my $ttf_path = first { $_->{type} eq $fontName } @{ $ttf->{font} };
148         if ( -e $ttf_path->{content} ) {
149             return prTTFont($ttf_path->{content});
150         } else {
151             warn "ERROR in koha-conf.xml -- missing <font type=\"$fontName\">/path/to/font.ttf</font>";
152         }
153     }
154     return prFont($fontName);
155 }
156
157 sub FontSize {
158     my $self = shift;
159     my $size = shift;
160     return prFontSize($size);
161 }
162
163 sub Form {
164     my $self = shift;
165     my %params = @_;
166     return prForm(%params);
167 }
168
169 sub GetLogBuffer {
170     my $self = shift;
171     return prGetLogBuffer();
172 }
173
174 sub GraphState {
175     my $self = shift;
176     my $string = shift;
177     prGraphState($string);
178 }
179
180 sub Image {
181     my $self = shift;
182     my %params = @_;
183     return prImage(%params);
184 }
185
186 sub Init {
187     my $self = shift;
188     my ($string, $duplicateCode) = @_;
189     prInit($string, $duplicateCode);
190 }
191
192 sub AltJpeg {
193     my $self = shift;
194     my ($imageData, $width, $height, $imageFormat, $altImageData, $altImageWidth, $altImageHeight, $altImageFormat) = @_;
195     return prAltJpeg($imageData, $width, $height, $imageFormat, $altImageData, $altImageWidth, $altImageHeight, $altImageFormat);
196 }
197
198 sub Jpeg {
199     my $self = shift;
200     my ($imageData, $width, $height, $imageFormat) = @_;
201     return prJpeg($imageData, $width, $height, $imageFormat);
202 }
203
204 sub Js {
205     my $self = shift;
206     my $string_or_fileName = shift;
207     prJs($string_or_fileName);
208 }
209
210 sub Link {
211     my $self = shift;
212     my %params = @_;
213     prLink(%params);
214 }
215
216 sub Log {
217     my $self = shift;
218     my $string = shift;
219     prLog($string);
220 }
221
222 sub LogDir {
223     my $self = shift;
224     my $directory = shift;
225     prLogDir($directory);
226 }
227
228 sub Mbox {
229     my $self = shift;
230     my ($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY) = @_;
231     prMbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
232 }
233
234 sub Page {
235     my $self = shift;
236     my $noLog = shift;
237     prPage($noLog);
238 }
239
240 sub SinglePage {
241     my $self = shift;
242     my ($file, $pageNumber) = @_;
243     return prSinglePage($file, $pageNumber);
244 }
245
246 sub StrWidth {
247     my $self = shift;
248     my ($string, $font, $fontSize) = @_;
249
250     # replace font code with correct internal font
251     $font = C4::Creators::PDF->Font($font);
252
253     return prStrWidth($string, $font, $fontSize);
254 }
255
256 sub Text {
257     my $self = shift;
258     my ($x, $y, $string, $align, $rotation) = @_;
259     return prText($x, $y, $string, $align, $rotation);
260 }
261
262 sub TTFont {
263     my $self = shift;
264     my $path = shift;
265     return prTTFont($path);
266 }
267
268 sub Code128 {
269     my $self = shift;
270     my %opts = @_;
271     PDF::Reuse::Barcode::Code128(%opts);
272 }
273
274 sub Code39 {
275     my $self = shift;
276     my %opts = @_;
277     PDF::Reuse::Barcode::Code39(%opts);
278 }
279
280 sub COOP2of5 {
281     my $self = shift;
282     my %opts = @_;
283     PDF::Reuse::Barcode::COOP2of5(%opts);
284 }
285
286 sub EAN13 {
287     my $self = shift;
288     my %opts = @_;
289     PDF::Reuse::Barcode::EAN13(%opts);
290 }
291
292 sub EAN8 {
293     my $self = shift;
294     my %opts = @_;
295     PDF::Reuse::Barcode::EAN8(%opts);
296 }
297
298 sub IATA2of5 {
299     my $self = shift;
300     my %opts = @_;
301     PDF::Reuse::Barcode::IATA2of5(%opts);
302 }
303
304 sub Industrial2of5 {
305     my $self = shift;
306     my %opts = @_;
307     PDF::Reuse::Barcode::Industrial2of5(%opts);
308 }
309
310 sub ITF {
311     my $self = shift;
312     my %opts = @_;
313     PDF::Reuse::Barcode::ITF(%opts);
314 }
315
316 sub Matrix2of5 {
317     my $self = shift;
318     my %opts = @_;
319     PDF::Reuse::Barcode::Matrix2of5(%opts);
320 }
321
322 sub NW7 {
323     my $self = shift;
324     my %opts = @_;
325     PDF::Reuse::Barcode::NW7(%opts);
326 }
327
328 sub UPCA {
329     my $self = shift;
330     my %opts = @_;
331     PDF::Reuse::Barcode::UPCA(%opts);
332 }
333
334 sub UPCE {
335     my $self = shift;
336     my %opts = @_;
337     PDF::Reuse::Barcode::UPCE(%opts);
338 }
339
340 1;
341 __END__
342
343
344 =head1 NAME
345
346 C4::Creators::PDF -   A class wrapper for PDF::Reuse and PDF::Reuse::Barcode to allow usage as a pseudo-object. For usage see
347                     PDF::Reuse documentation and C4::Creators::PDF code.
348
349 =cut
350
351 =head1 AUTHOR
352
353 Chris Nighswonger <cnighswonger AT foundations DOT edu>
354
355 =cut