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