SL::Helper::CreatePDF: gewisse Variablen nicht mit Variablen überschreiben lassen
[kivitendo-erp.git] / SL / Helper / CreatePDF.pm
1 package SL::Helper::CreatePDF;
2
3 use strict;
4
5 use Carp;
6 use Cwd;
7 use English qw(-no_match_vars);
8 use File::Slurp ();
9 use File::Temp ();
10 use List::MoreUtils qw(uniq);
11 use List::Util qw(first);
12 use String::ShellQuote ();
13
14 use SL::Form;
15 use SL::Common;
16 use SL::DB::Language;
17 use SL::DB::Printer;
18 use SL::MoreCommon;
19 use SL::Template;
20 use SL::Template::LaTeX;
21
22 use Exporter 'import';
23 our @EXPORT_OK = qw(create_pdf merge_pdfs find_template);
24 our %EXPORT_TAGS = (
25   all => \@EXPORT_OK,
26 );
27
28 sub create_pdf {
29   my ($class, %params) = @_;
30
31   my $userspath       = $::lx_office_conf{paths}->{userspath};
32   my $vars            = $params{variables} || {};
33   my $form            = Form->new('');
34   $form->{$_}         = $vars->{$_} for keys %{ $vars };
35   $form->{format}     = 'pdf';
36   $form->{cwd}        = getcwd();
37   $form->{templates}  = $::instance_conf->get_templates;
38   $form->{IN}         = $params{template};
39   $form->{tmpdir}     = $form->{cwd} . '/' . $userspath;
40   my ($suffix)        = $params{template} =~ m{\.(.+)};
41
42   my $temp_fh;
43   ($temp_fh, $form->{tmpfile}) = File::Temp::tempfile(
44     'kivitendo-printXXXXXX',
45     SUFFIX => ".${suffix}",
46     DIR    => $userspath,
47     UNLINK => ($::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files})? 0 : 1,
48   );
49
50   my $parser = SL::Template::LaTeX->new(
51     $form->{IN},
52     $form,
53     \%::myconfig,
54     $userspath,
55   );
56
57   my $result = $parser->parse($temp_fh);
58
59   close $temp_fh;
60   chdir $form->{cwd};
61
62   if (!$result) {
63     $form->cleanup;
64     die $parser->get_error;
65   }
66
67   if (($params{return} || 'content') eq 'file_name') {
68     my $new_name = $userspath . '/keep-' . $form->{tmpfile};
69     rename $userspath . '/' . $form->{tmpfile}, $new_name;
70
71     $form->cleanup;
72
73     return $new_name;
74   }
75
76   my $pdf = File::Slurp::read_file($userspath . '/' . $form->{tmpfile});
77
78   $form->cleanup;
79
80   return $pdf;
81 }
82
83 sub merge_pdfs {
84   my ($class, %params) = @_;
85
86   return scalar(File::Slurp::read_file($params{file_names}->[0])) if scalar(@{ $params{file_names} }) < 2;
87
88   my ($temp_fh, $temp_name) = File::Temp::tempfile(
89     'kivitendo-printXXXXXX',
90     SUFFIX => '.pdf',
91     DIR    => $::lx_office_conf{paths}->{userspath},
92     UNLINK => ($::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files})? 0 : 1,
93   );
94   close $temp_fh;
95
96   my $input_names = join ' ', String::ShellQuote::shell_quote(@{ $params{file_names} });
97   my $exe         = $::lx_office_conf{applications}->{ghostscript} || 'gs';
98   my $output      = `$exe -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=${temp_name} ${input_names} 2>&1`;
99
100   die "Executing gs failed: $ERRNO" if !defined $output;
101   die $output                       if $? != 0;
102
103   return scalar File::Slurp::read_file($temp_name);
104 }
105
106 sub find_template {
107   my ($class, %params) = @_;
108
109   $params{name} or croak "Missing parameter 'name'";
110
111   my $path                 = $::instance_conf->get_templates;
112   my $extension            = $params{extension} || "tex";
113   my ($printer, $language) = ('', '');
114
115   if ($params{printer} || $params{printer_id}) {
116     if ($params{printer} && !ref $params{printer}) {
117       $printer = '_' . $params{printer};
118     } else {
119       $printer = $params{printer} || SL::DB::Printer->new(id => $params{printer_id})->load;
120       $printer = $printer->template_code ? '_' . $printer->template_code : '';
121     }
122   }
123
124   if ($params{language} || $params{language_id}) {
125     if ($params{language} && !ref $params{language}) {
126       $language = '_' . $params{language};
127     } else {
128       $language = $params{language} || SL::DB::Language->new(id => $params{language_id})->load;
129       $language = $language->template_code ? '_' . $language->template_code : '';
130     }
131   }
132
133   my @template_files = (
134     $params{name} . "${language}${printer}",
135     $params{name} . "${language}",
136     $params{name},
137     "default",
138   );
139
140   if ($params{email}) {
141     unshift @template_files, (
142       $params{name} . "_email${language}${printer}",
143       $params{name} . "_email${language}",
144     );
145   }
146
147   @template_files = map { "${_}.${extension}" } uniq grep { $_ } @template_files;
148
149   my $template = first { -f ($path . "/$_") } @template_files;
150
151   return wantarray ? ($template, @template_files) : $template;
152 }
153
154 1;
155 __END__
156
157 =pod
158
159 =encoding utf8
160
161 =head1 NAME
162
163 SL::Helper::CreatePDF - A helper for creating PDFs from template files
164
165 =head1 SYNOPSIS
166
167   # Retrieve a sales order from the database and create a PDF for
168   # it:
169   my $order               = SL::DB::Order->new(id => …)->load;
170   my $print_form          = Form->new('');
171   $print_form->{type}     = 'invoice';
172   $print_form->{formname} = 'invoice',
173   $print_form->{format}   = 'pdf',
174   $print_form->{media}    = 'file';
175
176   $order->flatten_to_form($print_form, format_amounts => 1);
177   $print_form->prepare_for_printing;
178
179   my $pdf = SL::Helper::CreatePDF->create_pdf(
180     template  => 'sales_order',
181     variables => $print_form,
182   );
183
184 =head1 FUNCTIONS
185
186 =over 4
187
188 =item C<create_pdf %params>
189
190 Parses a LaTeX template file, creates a PDF for it and returns either
191 its content or its file name. The recognized parameters are:
192
193 =over 2
194
195 =item * C<template> – mandatory. The template file name relative to
196 the users' templates directory. Must be an existing file name,
197 e.g. one retrieved by L</find_template>.
198
199 =item * C<variables> – optional hash reference containing variables
200 available to the template.
201
202 =item * C<return> – optional scalar containing either C<content> (the
203 default) or C<file_name>. If it is set to C<file_name> then the file
204 name of the temporary file containing the PDF is returned, and the
205 caller is responsible for deleting it. Otherwise a scalar containing
206 the PDF itself is returned and all temporary files have already been
207 deleted by L</create_pdf>.
208
209 =back
210
211 =item C<find_template %params>
212
213 Searches the user's templates directory for a template file name to
214 use. The file names considered depend on the parameters; they can
215 contain a template base name and suffixes for email, language and
216 printers. As a fallback the name C<default.$extension> is also
217 considered.
218
219 The return value depends on the context. In scalar context the
220 template file name that matches the given parameters is returned. It's
221 a file name relative to the user's templates directory. If no template
222 file is found then C<undef> is returned.
223
224 In list context the first element is the same value as in scalar
225 context. Additionally a list of considered template file names is
226 returned.
227
228 The recognized parameters are:
229
230 =over 2
231
232 =item * C<name> – mandatory. The template's file name basis
233 without any additional suffix or extension, e.g. C<sales_quotation>.
234
235 =item * C<extension> – optional file name extension to use without the
236 dot. Defaults to C<tex>.
237
238 =item * C<email> – optional flag indicating whether or not the
239 template is to be sent via email. If set to true then template file
240 names containing C<_email> are considered as well.
241
242 =item * C<language> and C<language_id> – optional parameters
243 indicating the language to be used. C<language> can be either a string
244 containing the language code to use or an instance of
245 C<SL::DB::Language>. C<language_id> can contain the ID of the
246 C<SL::DB:Language> instance to load and use. If given template file
247 names containing C<_language_template_code> are considered as well.
248
249 =item * C<printer> and C<printer_id> – optional parameters indicating
250 the printer to be used. C<printer> can be either a string containing
251 the printer code to use or an instance of
252 C<SL::DB::Printer>. C<printer_id> can contain the ID of the
253 C<SL::DB:Printer> instance to load and use. If given template file
254 names containing C<_printer_template_code> are considered as well.
255
256 =back
257
258 =item C<merge_pdfs %params>
259
260 Merges two or more PDFs into a single PDF by using the external
261 application ghostscript.
262
263 The recognized parameters are:
264
265 =over 2
266
267 =item * C<file_names> – mandatory array reference containing the file
268 names to merge.
269
270 =back
271
272 Note that this function relies on the presence of the external
273 application ghostscript. The executable to use is configured via
274 kivitendo's configuration file setting C<application.ghostscript>.
275
276 =back
277
278 =head1 BUGS
279
280 Nothing here yet.
281
282 =head1 AUTHOR
283
284 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
285
286 =cut