HTML zu LaTeX: gewisse kaputte Konstrukte vom CKEditor entfernen
[kivitendo-erp.git] / SL / Template / LaTeX.pm
1 package SL::Template::LaTeX;
2
3 use parent qw(SL::Template::Simple);
4
5 use strict;
6
7 use Carp;
8 use Cwd;
9 use English qw(-no_match_vars);
10 use File::Basename;
11 use File::Temp;
12 use HTML::Entities ();
13 use List::MoreUtils qw(any);
14 use Scalar::Util qw(blessed);
15 use Unicode::Normalize qw();
16
17 use SL::DB::Default;
18 use SL::System::Process;
19
20 my %text_markup_replace = (
21   b => 'textbf',
22   i => 'textit',
23   u => 'underline',
24 );
25
26 sub _format_text {
27   my ($self, $content, %params) = @_;
28
29   $content = $::locale->quote_special_chars('Template/LaTeX', $content);
30
31   # Allow some HTML markup to be converted into the output format's
32   # corresponding markup code, e.g. bold or italic.
33   foreach my $key (keys(%text_markup_replace)) {
34     my $new   =  $text_markup_replace{$key};
35     $content =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
36   }
37
38   $content =~ s/[\x00-\x1f]//g;
39
40   return $content;
41 }
42
43 my %html_replace = (
44   '</p>'      => "\n\n",
45   '<ul>'      => "\\begin{itemize} ",
46   '</ul>'     => "\\end{itemize} ",
47   '<ol>'      => "\\begin{enumerate} ",
48   '</ol>'     => "\\end{enumerate} ",
49   '<li>'      => "\\item ",
50   '</li>'     => " ",
51   '<b>'       => "\\textbf{",
52   '</b>'      => "}",
53   '<strong>'  => "\\textbf{",
54   '</strong>' => "}",
55   '<i>'       => "\\textit{",
56   '</i>'      => "}",
57   '<em>'      => "\\textit{",
58   '</em>'     => "}",
59   '<u>'       => "\\underline{",
60   '</u>'      => "}",
61   '<s>'       => "\\sout{",
62   '</s>'      => "}",
63   '<sub>'     => "\\textsubscript{",
64   '</sub>'    => "}",
65   '<sup>'     => "\\textsuperscript{",
66   '</sup>'    => "}",
67   '<br/>'     => "\\newline ",
68   '<br>'      => "\\newline ",
69 );
70
71 sub _lb_to_space {
72   my ($to_replace) = @_;
73
74   my $vspace = '\vspace*{0.5cm}';
75   return $vspace x (length($to_replace) / length($html_replace{'<br>'}));
76 }
77
78 sub _format_html {
79   my ($self, $content, %params) = @_;
80
81   $content =~ s{ \r+ }{}gx;
82   $content =~ s{ \n+ }{ }gx;
83   $content =~ s{ (?:\&nbsp;|\s)+ }{ }gx;
84   $content =~ s{ (?:\&nbsp;|\s)+$ }{}gx;
85   $content =~ s{ (?: <br/?> )+$ }{}gx;
86   $content =~ s{ <ul>\s*</ul> | <ol>\s*</ol> }{}igx;
87   $content =~ s{ (?: <p>\s*</p>\s* )+ \Z }{}imgx;
88
89   my @parts = grep { $_ } map {
90     if (substr($_, 0, 1) eq '<') {
91       s{ +}{}g;
92       $html_replace{$_} || '';
93
94     } else {
95       $::locale->quote_special_chars('Template/LaTeX', HTML::Entities::decode_entities($_));
96     }
97   } split(m{(<.*?>)}x, $content);
98
99   $content =  join '', @parts;
100   $content =~ s{ (?: [\n\s] | \\newline )+ $ }{}gx;                                         # remove line breaks at the end of the text
101   $content =~ s{ ^ \s+ }{}gx;                                                               # remove white space at the start of the text
102   $content =~ s{ ^ ( \\newline \  )+ }{ _lb_to_space($1) }gxe;                              # convert line breaks at the start of the text to vertical space
103   $content =~ s{ ( \n\n+ ) ( \\newline \  )+ }{ $1 . _lb_to_space($2) }gxe;                 # convert line breaks at the start of a paragraph to vertical space
104   $content =~ s{ ( \\end\{ [^\}]+ \} \h* ) ( \\newline \  )+ }{ $1 . _lb_to_space($2) }gxe; # convert line breaks after LaTeX environments like lists to vertical space
105   $content =~ s{ ^ \h+ \\newline }{\\newline}gmx;
106   $content =~ s{ \n\n \h* \\newline \h* }{\n\n}gmx;
107
108   return $content;
109 }
110
111 my %formatters = (
112   html => \&_format_html,
113   text => \&_format_text,
114 );
115
116 sub new {
117   my $type = shift;
118
119   my $self = $type->SUPER::new(@_);
120
121   return $self;
122 }
123
124 sub format_string {
125   my ($self, $content, $variable) = @_;
126
127   my $formatter =
128        $formatters{ $self->{variable_content_types}->{$variable} }
129     // $formatters{ $self->{default_content_type} }
130     // $formatters{ text };
131
132   $content  =~ s{[^\p{Print}\n]|\p{Cf}}{}g;
133   $variable =~ s{[^\p{Print}\n]|\p{Cf}}{}g;
134
135   return $formatter->($self, $content, variable => $variable);
136 }
137
138 sub parse_foreach {
139   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
140
141   my ($form, $new_contents) = ($self->{"form"}, "");
142
143   my $ary = $self->_get_loop_variable($var, 1, @indices);
144
145   my $sum                          = 0;
146   my $current_page                 = 1;
147   my ($current_line, $corrent_row) = (0, 1);
148   my $description_array            = $self->_get_loop_variable("description",     1);
149   my $longdescription_array        = $self->_get_loop_variable("longdescription", 1);
150   my $linetotal_array              = $self->_get_loop_variable("linetotal",       1);
151
152   $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal} = [];
153
154   # forech block hasn't given us an array. ignore
155   return $new_contents unless ref $ary eq 'ARRAY';
156
157   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
158     # do magic markers
159     $form->{"__first__"}   = $i == 0;
160     $form->{"__last__"}    = ($i + 1) == scalar(@{$ary});
161     $form->{"__odd__"}     = (($i + 1) % 2) == 1;
162     $form->{"__counter__"} = $i + 1;
163
164   #everything from here to the next marker should be removed after the release of 2.7.0
165     if (   ref $description_array       eq 'ARRAY'
166         && scalar @{$description_array} == scalar @{$ary}
167         && $self->{"chars_per_line"}    != 0)
168     {
169       my $lines = int(length($description_array->[$i]) / $self->{"chars_per_line"});
170       my $lpp;
171
172       $description_array->[$i] =~ s/(\\newline\s?)*$//;
173       $lines++ while ($description_array->[$i] =~ m/\\newline/g);
174       $lines++;
175
176       if ($current_page == 1) {
177         $lpp = $self->{"lines_on_first_page"};
178       } else {
179         $lpp = $self->{"lines_on_second_page"};
180       }
181
182       # Yes we need a manual page break -- or the user has forced one
183       if (   (($current_line + $lines) > $lpp)
184           || ($description_array->[$i]     =~ /<pagebreak>/)
185           || (   ref $longdescription_array eq 'ARRAY'
186               && $longdescription_array->[$i] =~ /<pagebreak>/)) {
187         my $pb = $self->{"pagebreak_block"};
188
189         # replace the special variables <%sumcarriedforward%>
190         # and <%lastpage%>
191
192         my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
193         $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
194         $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
195
196         my $new_text = $self->parse_block($pb, (@indices, $i));
197         return undef unless (defined($new_text));
198         $new_contents .= $new_text;
199
200         $current_page++;
201         $current_line = 0;
202       }
203       $current_line += $lines;
204     }
205   #stop removing code here.
206
207     if (   ref $linetotal_array eq 'ARRAY'
208         && $i < scalar(@{$linetotal_array})) {
209       $sum += $form->parse_amount($self->{"myconfig"}, $linetotal_array->[$i]);
210     }
211
212     $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal}->[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
213
214     my $new_text = $self->parse_block($text, (@indices, $i));
215     return undef unless (defined($new_text));
216     $new_contents .= $start_tag . $new_text . $end_tag;
217   }
218   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
219
220   return $new_contents;
221 }
222
223 sub find_end {
224   my ($self, $text, $pos, $var, $not) = @_;
225
226   my $tag_start_len = length $self->{tag_start};
227
228   my $depth = 1;
229   $pos = 0 unless ($pos);
230
231   while ($pos < length($text)) {
232     $pos++;
233
234     next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
235
236     my $keyword_pos = $pos - 1 + $tag_start_len;
237
238     if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 7) eq 'foreach')) {
239       $depth++;
240
241     } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
242       if (!$var) {
243         $self->{"error"} =
244             "$self->{tag_start}else$self->{tag_end} outside of "
245           . "$self->{tag_start}if$self->{tag_end} / "
246           . "$self->{tag_start}ifnot$self->{tag_end}.";
247         return undef;
248       }
249
250       my $block = substr($text, 0, $pos - 1);
251       substr($text, 0, $pos - 1) = "";
252       $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
253       $text =  $self->{tag_start} . 'if' . ($not ?  " " : "not ") . $var . $self->{tag_end} . $text;
254
255       return ($block, $text);
256
257     } elsif (substr($text, $keyword_pos, 3) eq 'end') {
258       $depth--;
259       if ($depth == 0) {
260         my $block = substr($text, 0, $pos - 1);
261         substr($text, 0, $pos - 1) = "";
262         $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
263
264         return ($block, $text);
265       }
266     }
267   }
268
269   return undef;
270 }
271
272 sub parse_block {
273   $main::lxdebug->enter_sub();
274
275   my ($self, $contents, @indices) = @_;
276
277   my $new_contents = "";
278
279   while ($contents ne "") {
280     my $pos_if      = index($contents, $self->{tag_start} . 'if');
281     my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
282
283     if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
284       $new_contents .= $self->substitute_vars($contents, @indices);
285       last;
286     }
287
288     if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
289       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
290       substr($contents, 0, $pos_foreach) = "";
291
292       if ($contents !~ m|^($self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm})|) {
293         $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
294         $main::lxdebug->leave_sub();
295         return undef;
296       }
297
298       my $var = $2;
299
300       substr($contents, 0, length($1)) = "";
301
302       my $block;
303       ($block, $contents) = $self->find_end($contents);
304       if (!$block) {
305         $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
306         $main::lxdebug->leave_sub();
307         return undef;
308       }
309
310       my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
311       if (!defined($new_text)) {
312         $main::lxdebug->leave_sub();
313         return undef;
314       }
315       $new_contents .= $new_text;
316
317     } else {
318       if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
319         $main::lxdebug->leave_sub();
320         return undef;
321       }
322     }
323   }
324
325   $main::lxdebug->leave_sub();
326
327   return $new_contents;
328 }
329
330 sub parse_first_line {
331   my $self = shift;
332   my $line = shift || "";
333
334   if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
335     if ($1 eq $2) {
336       $self->{error} = "The tag start and end markers must not be equal.";
337       return 0;
338     }
339
340     $self->set_tag_style($1, $2);
341   }
342
343   return 1;
344 }
345
346 sub _parse_config_option {
347   my $self = shift;
348   my $line = shift;
349
350   $line =~ s/^\s*//;
351   $line =~ s/\s*$//;
352
353   my ($key, $value) = split m/\s*=\s*/, $line, 2;
354
355   if ($key eq 'tag-style') {
356     $self->set_tag_style(split(m/\s+/, $value, 2));
357   }
358   if ($key eq 'use-template-toolkit') {
359     $self->set_use_template_toolkit($value);
360   }
361 }
362
363 sub _parse_config_lines {
364   my $self  = shift;
365   my $lines = shift;
366
367   my ($comment_start, $comment_end) = ("", "");
368
369   if (ref $self eq 'SL::Template::LaTeX') {
370     $comment_start = '\s*%';
371   } elsif (ref $self eq 'SL::Template::HTML') {
372     $comment_start = '\s*<!--';
373     $comment_end   = '(?:--)?>\s*';
374   } else {
375     $comment_start = '\s*\#';
376   }
377
378   my $num_lines = scalar @{ $lines };
379   my $i         = 0;
380
381   while ($i < $num_lines) {
382     my $line = $lines->[$i];
383
384     if ($line !~ m/^${comment_start}\s*config\s*:(.*?)${comment_end}$/i) {
385       $i++;
386       next;
387     }
388
389     $self->_parse_config_option($1);
390     splice @{ $lines }, $i, 1;
391     $num_lines--;
392   }
393 }
394
395 sub _embed_file_directive {
396   my ($self, $file) = @_;
397
398   # { source      => $xmlfile,
399   #   name        => 'factur-x.xml',
400   #   description => $::locale->text('Factur-X/ZUGFeRD invoice'), }
401
402   my $file_name  =  blessed($file->{source}) && $file->{source}->can('filename') ? $file->{source}->filename : "" . $file->{source}->filename;
403   my $embed_name =  $file->{name} // $file_name;
404   $embed_name    =~ s{.*/}{};
405
406   my $embed_name_ascii = $::locale->quote_special_chars('filenames', $embed_name);
407   $embed_name_ascii    =~ s{[^a-z0-9!@#$%^&*(){}[\],.+'"=_-]+}{}gi;
408
409   my @options;
410
411   my $add_opt = sub {
412     my ($name, $value) = @_;
413     return if ($value // '') eq '';
414     push @options, sprintf('%s={%s}', $name, $value); # TODO: escaping
415   };
416
417   $add_opt->('filespec',       $embed_name_ascii);
418   $add_opt->('ucfilespec',     $embed_name);
419   $add_opt->('desc',           $file->{description});
420   $add_opt->('afrelationship', $file->{relationship});
421   $add_opt->('mimetype',       $file->{mime_type});
422
423   return sprintf('\embedfile[%s]{%s}', join(',', @options), $file_name);
424 }
425
426 sub _force_mandatory_packages {
427   my ($self, @lines) = @_;
428   my @new_lines;
429
430   my %used_packages;
431   my @required_packages = qw(textcomp ulem);
432   push @required_packages, 'embedfile' if $self->{pdf_a};
433
434   foreach my $line (@lines) {
435     if ($line =~ m/\\usepackage[^\{]*{(.*?)}/) {
436       $used_packages{$1} = 1;
437
438     } elsif ($line =~ m/\\begin\{document\}/) {
439       if ($self->{pdf_a} && $self->{pdf_a}->{xmp}) {
440         my $version       = $self->{pdf_a}->{version}   // '3a';
441         my $xmp_file_name = $self->{userspath} . "/pdfa.xmp";
442         my $out           = IO::File->new($xmp_file_name, ">:encoding(utf-8)") || croak "Error creating ${xmp_file_name}: $!";
443         $out->print(Encode::encode('utf-8', $self->{pdf_a}->{xmp}));
444         $out->close;
445
446         push @new_lines, (
447           "\\usepackage[a-${version},mathxmp]{pdfx}[2018/12/22]\n",
448           "\\usepackage[genericmode]{tagpdf}\n",
449           "\\tagpdfsetup{activate-all}\n",
450           "\\hypersetup{pdfstartview=}\n",
451         );
452       }
453
454       push @new_lines, map { "\\usepackage{$_}\n" } grep { !$used_packages{$_} } @required_packages;
455       push @new_lines, $line;
456       push @new_lines, map { $self->_embed_file_directive($_) } @{ $self->{pdf_attachments} // [] };
457
458       next;
459     }
460
461     push @new_lines, $line;
462   }
463
464   return @new_lines;
465 }
466
467 sub parse {
468   my $self = $_[0];
469   local *OUT = $_[1];
470   my $form = $self->{"form"};
471
472   if (!open(IN, "$form->{templates}/$form->{IN}")) {
473     $self->{"error"} = "$form->{templates}/$form->{IN}: $!";
474     return 0;
475   }
476   binmode IN, ":utf8";
477   my @lines = <IN>;
478   close(IN);
479
480   $self->_parse_config_lines(\@lines);
481   @lines = $self->_force_mandatory_packages(@lines) if (ref $self eq 'SL::Template::LaTeX');
482
483   my $contents = join("", @lines);
484
485   # detect pagebreak block and its parameters
486   if ($contents =~ /^(.*)($self->{tag_start_qm}pagebreak\s+(\d+)\s+(\d+)\s+(\d+)\s*$self->{tag_end_qm}(.*?)$self->{tag_start_qm}end(\s*pagebreak)?$self->{tag_end_qm})/s) {
487     $self->{"chars_per_line"} = $3;
488     $self->{"lines_on_first_page"} = $4;
489     $self->{"lines_on_second_page"} = $5;
490     $self->{"pagebreak_block"} = $6;
491
492     substr($contents, length($1), length($2)) = "";
493   }
494
495   $self->{"forced_pagebreaks"} = [];
496
497   my $new_contents;
498   if ($self->{use_template_toolkit}) {
499     if ($self->{custom_tag_style}) {
500       $contents = "[% TAGS $self->{tag_start} $self->{tag_end} %]\n" . $contents;
501     }
502
503     my $globals = global_vars();
504
505     $::form->template->process(\$contents, { %$form, %$globals }, \$new_contents) || die $::form->template->error;
506   } else {
507     $new_contents = $self->parse_block($contents);
508   }
509   if (!defined($new_contents)) {
510     $main::lxdebug->leave_sub();
511     return 0;
512   }
513
514   binmode OUT, ":utf8";
515   print OUT Unicode::Normalize::normalize('C', $new_contents);
516
517   if ($form->{"format"} =~ /postscript/i) {
518     return $self->convert_to_postscript();
519   } elsif ($form->{"format"} =~ /pdf/i) {
520     return $self->convert_to_pdf();
521   } else {
522     return 1;
523   }
524 }
525
526 sub _texinputs_path {
527   my ($self, $templates_path) = @_;
528
529   my $exe_dir     = SL::System::Process::exe_dir();
530   $templates_path = $exe_dir . '/' . $templates_path unless $templates_path =~ m{^/};
531
532   return join(':', grep({ $_ } ('.', $exe_dir . '/texmf', $exe_dir . '/users', $templates_path, $ENV{TEXINPUTS})), '');
533 }
534
535 sub convert_to_postscript {
536   my ($self) = @_;
537   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
538
539   # Convert the tex file to postscript
540   local $ENV{TEXINPUTS} = $self->_texinputs_path($form->{templates});
541
542   if (!chdir("$userspath")) {
543     $self->{"error"} = "chdir : $!";
544     $self->cleanup();
545     return 0;
546   }
547
548   $form->{tmpfile} =~ s/\Q$userspath\E\///g;
549
550   my $latex = $self->_get_latex_path();
551   my $old_home = $ENV{HOME};
552   my $old_openin_any = $ENV{openin_any};
553   $ENV{HOME}   = $userspath =~ m|^/| ? $userspath : getcwd();
554   $ENV{openin_any} = "r";
555
556   for (my $run = 1; $run <= 2; $run++) {
557     if (system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
558                "> $form->{tmpfile}.err") == -1) {
559       die "system call to $latex failed: $!";
560     }
561     if ($?) {
562       $ENV{HOME} = $old_home;
563       $ENV{openin_any} = $old_openin_any;
564       $self->{"error"} = $form->cleanup($latex);
565       return 0;
566     }
567   }
568
569   $form->{tmpfile} =~ s/tex$/dvi/;
570
571   if (system("dvips $form->{tmpfile} -o -q > /dev/null") == -1) {
572     die "system call to dvips failed: $!";
573   }
574   $ENV{HOME} = $old_home;
575   $ENV{openin_any} = $old_openin_any;
576
577   if ($?) {
578     $self->{"error"} = "dvips : $?";
579     $self->cleanup('dvips');
580     return 0;
581   }
582   $form->{tmpfile} =~ s/dvi$/ps/;
583
584   $self->cleanup();
585
586   return 1;
587 }
588
589 sub convert_to_pdf {
590   my ($self) = @_;
591   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
592
593   # Convert the tex file to PDF
594   local $ENV{TEXINPUTS} = $self->_texinputs_path($form->{templates});
595
596   if (!chdir("$userspath")) {
597     $self->{"error"} = "chdir : $!";
598     $self->cleanup();
599     return 0;
600   }
601
602   $form->{tmpfile} =~ s/\Q$userspath\E\///g;
603
604   my $latex = $self->_get_latex_path();
605   my $old_home = $ENV{HOME};
606   my $old_openin_any = $ENV{openin_any};
607   $ENV{HOME}   = $userspath =~ m|^/| ? $userspath : getcwd();
608   $ENV{openin_any} = "r";
609
610   for (my $run = 1; $run <= 2; $run++) {
611     if (system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
612                "> $form->{tmpfile}.err") == -1) {
613       die "system call to $latex failed: $!";
614     }
615
616     if ($?) {
617       $ENV{HOME}     = $old_home;
618       $ENV{openin_any} = $old_openin_any;
619       $self->{error} = $form->cleanup($latex);
620       return 0;
621     }
622   }
623
624   $ENV{HOME} = $old_home;
625   $ENV{openin_any} = $old_openin_any;
626   $form->{tmpfile} =~ s/tex$/pdf/;
627
628   $self->cleanup();
629
630   return 1;
631 }
632
633 sub _get_latex_path {
634   return $::lx_office_conf{applications}->{latex} || 'pdflatex';
635 }
636
637 sub get_mime_type() {
638   my ($self) = @_;
639
640   if ($self->{"form"}->{"format"} =~ /postscript/i) {
641     return "application/postscript";
642   } else {
643     return "application/pdf";
644   }
645 }
646
647 sub uses_temp_file {
648   return 1;
649 }
650
651 sub parse_and_create_pdf {
652   my ($class, $template_file_name, %params) = @_;
653
654   my $userspath                = delete($params{userspath}) || $::lx_office_conf{paths}->{userspath};
655   my $keep_temp                = $::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files};
656   my ($tex_fh, $tex_file_name) = File::Temp::tempfile(
657     'kivitendo-printXXXXXX',
658     SUFFIX => '.tex',
659     DIR    => $userspath,
660     UNLINK => $keep_temp ? 0 : 1,,
661   );
662
663   my $old_wd               = getcwd();
664
665   my $local_form           = Form->new('');
666   $local_form->{cwd}       = $old_wd;
667   $local_form->{IN}        = $template_file_name;
668   $local_form->{tmpdir}    = $userspath;
669   $local_form->{tmpfile}   = $tex_file_name;
670   $local_form->{templates} = SL::DB::Default->get->templates;
671
672   foreach (keys %params) {
673     croak "The parameter '$_' must not be used." if exists $local_form->{$_};
674     $local_form->{$_} = $params{$_};
675   }
676
677   my $error;
678   eval {
679     my $template = SL::Template::LaTeX->new(file_name => $template_file_name, form => $local_form, userspath => $userspath);
680     my $result   = $template->parse($tex_fh) && $template->convert_to_pdf;
681
682     die $template->{error} unless $result;
683
684     1;
685   } or do { $error = $EVAL_ERROR; };
686
687   chdir $old_wd;
688   close $tex_fh;
689
690   if ($keep_temp) {
691     chmod(((stat $tex_file_name)[2] & 07777) | 0660, $tex_file_name);
692   } else {
693     my $tmpfile =  $tex_file_name;
694     $tmpfile    =~ s/\.\w+$//;
695     unlink(grep { !m/\.pdf$/ } <$tmpfile.*>);
696   }
697
698   return (error     => $error) if $error;
699   return (file_name => do { $tex_file_name =~ s/tex$/pdf/; $tex_file_name });
700 }
701
702 sub global_vars {
703   {
704     AUTH            => $::auth,
705     INSTANCE_CONF   => $::instance_conf,
706     LOCALE          => $::locale,
707     LXCONFIG        => $::lx_office_conf,
708     LXDEBUG         => $::lxdebug,
709     MYCONFIG        => \%::myconfig,
710   };
711 }
712
713 1;