e57032189d597125c6f0c91f427848b9468c7026
[kivitendo-erp.git] / SL / Template / OpenDocument.pm
1 package SL::Template::OpenDocument;
2
3 use parent qw(SL::Template::Simple);
4
5 use Archive::Zip;
6 use Encode;
7 use HTML::Entities;
8 use POSIX 'setsid';
9
10 use SL::Iconv;
11 use SL::Template::OpenDocument::Styles;
12
13 use Cwd;
14 # use File::Copy;
15 # use File::Spec;
16 # use File::Temp qw(:mktemp);
17 use IO::File;
18
19 use strict;
20
21 my %text_markup_replace = (
22   b   => "BOLD",
23   i   => "ITALIC",
24   s   => "STRIKETHROUGH",
25   u   => "UNDERLINE",
26   sup => "SUPER",
27   sub => "SUB",
28 );
29
30 sub _format_text {
31   my ($self, $content, %params) = @_;
32
33   $content = $::locale->quote_special_chars('Template/OpenDocument', $content);
34
35   # Allow some HTML markup to be converted into the output format's
36   # corresponding markup code, e.g. bold or italic.
37   foreach my $key (keys(%text_markup_replace)) {
38     my $value = $text_markup_replace{$key};
39     $content =~ s|\&lt;${key}\&gt;|<text:span text:style-name=\"TKIVITENDO${value}\">|gi; #"
40     $content =~ s|\&lt;/${key}\&gt;|</text:span>|gi;
41   }
42
43   return $content;
44 }
45
46 my %html_replace = (
47   '</ul>'     => '</text:list>',
48   '</ol>'     => '</text:list>',
49   '</li>'     => '</text:p></text:list-item>',
50   '<b>'       => '<text:span text:style-name="TKIVITENDOBOLD">',
51   '</b>'      => '</text:span>',
52   '<strong>'  => '<text:span text:style-name="TKIVITENDOBOLD">',
53   '</strong>' => '</text:span>',
54   '<i>'       => '<text:span text:style-name="TKIVITENDOITALIC">',
55   '</i>'      => '</text:span>',
56   '<em>'      => '<text:span text:style-name="TKIVITENDOITALIC">',
57   '</em>'     => '</text:span>',
58   '<u>'       => '<text:span text:style-name="TKIVITENDOUNDERLINE">',
59   '</u>'      => '</text:span>',
60   '<s>'       => '<text:span text:style-name="TKIVITENDOSTRIKETHROUGH">',
61   '</s>'      => '</text:span>',
62   '<sub>'     => '<text:span text:style-name="TKIVITENDOSUB">',
63   '</sub>'    => '</text:span>',
64   '<sup>'     => '<text:span text:style-name="TKIVITENDOSUPER">',
65   '</sup>'    => '</text:span>',
66   '<br/>'     => '<text:line-break/>',
67   '<br>'      => '<text:line-break/>',
68 );
69
70 sub _format_html {
71   my ($self, $content, %params) = @_;
72
73   my $in_p        = 0;
74   my $p_start_tag = qq|<text:p text:style-name="@{[ $self->{current_text_style} ]}">|;
75   my $prefix      = '';
76   my $suffix      = '';
77
78   my (@tags_to_open, @tags_to_close);
79   for (my $idx = scalar(@{ $self->{tag_stack} }) - 1; $idx >= 0; --$idx) {
80     my $tag = $self->{tag_stack}->[$idx];
81
82     next if $tag =~ m{/>$};
83     last if $tag =~ m{^<table};
84
85     if ($tag =~ m{^<text:p}) {
86       $in_p        = 1;
87       $p_start_tag = $tag;
88       last;
89
90     } else {
91       $suffix  =  "${tag}${suffix}";
92       $tag     =~ s{ .*>}{>};
93       $prefix .=  '</' . substr($tag, 1);
94     }
95   }
96
97   $content            =~ s{ ^<p> | </p>$ }{}gx if $in_p;
98   $content            =~ s{ \r+ }{}gx;
99   $content            =~ s{ \n+ }{ }gx;
100   $content            =~ s{ (?:\&nbsp;|\s)+ }{ }gx;
101
102   my $ul_start_tag    = qq|<text:list xml:id="list@{[ int rand(9999999999999999) ]}" text:style-name="LKIVITENDOitemize@{[ $self->{current_text_style} ]}">|;
103   my $ol_start_tag    = qq|<text:list xml:id="list@{[ int rand(9999999999999999) ]}" text:style-name="LKIVITENDOenumerate@{[ $self->{current_text_style} ]}">|;
104   my $ul_li_start_tag = qq|<text:list-item><text:p text:style-name="PKIVITENDOitemize@{[ $self->{current_text_style} ]}">|;
105   my $ol_li_start_tag = qq|<text:list-item><text:p text:style-name="PKIVITENDOenumerate@{[ $self->{current_text_style} ]}">|;
106
107   my @parts = map {
108     if (substr($_, 0, 1) eq '<') {
109       s{ +}{}g;
110       if ($_ eq '</p>') {
111         $in_p--;
112         $in_p == 0 ? '</text:p>' : '';
113
114       } elsif ($_ eq '<p>') {
115         $in_p++;
116         $in_p == 1 ? $p_start_tag : '';
117
118       } elsif ($_ eq '<ul>') {
119         $self->{used_list_styles}->{itemize}->{$self->{current_text_style}}   = 1;
120         $html_replace{'<li>'}                                                 = $ul_li_start_tag;
121         $ul_start_tag;
122
123       } elsif ($_ eq '<ol>') {
124         $self->{used_list_styles}->{enumerate}->{$self->{current_text_style}} = 1;
125         $html_replace{'<li>'}                                                 = $ol_li_start_tag;
126         $ol_start_tag;
127
128       } else {
129         $html_replace{$_} || '';
130       }
131
132     } else {
133       $::locale->quote_special_chars('Template/OpenDocument', HTML::Entities::decode_entities($_));
134     }
135   } split(m{(<.*?>)}x, $content);
136
137   my $out  = join('', $prefix, @parts, $suffix);
138
139   # $::lxdebug->dump(0, "prefix parts suffix", [ $prefix, join('', @parts), $suffix ]);
140
141   return $out;
142 }
143
144 my %formatters = (
145   html => \&_format_html,
146   text => \&_format_text,
147 );
148
149 sub new {
150   my $type = shift;
151
152   my $self = $type->SUPER::new(@_);
153
154   $self->set_tag_style('&lt;%', '%&gt;');
155   $self->{quot_re} = '&quot;';
156
157   return $self;
158 }
159
160 sub parse_foreach {
161   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
162
163   my ($form, $new_contents) = ($self->{"form"}, "");
164
165   my $ary = $self->_get_loop_variable($var, 1, @indices);
166
167   for (my $i = 0; $i < scalar(@{$ary || []}); $i++) {
168     $form->{"__first__"} = $i == 0;
169     $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
170     $form->{"__odd__"} = (($i + 1) % 2) == 1;
171     $form->{"__counter__"} = $i + 1;
172     my $new_text = $self->parse_block($text, (@indices, $i));
173     return undef unless (defined($new_text));
174     $new_contents .= $start_tag . $new_text . $end_tag;
175   }
176   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
177
178   return $new_contents;
179 }
180
181 sub find_end {
182   my ($self, $text, $pos, $var, $not) = @_;
183
184   my $depth = 1;
185   $pos = 0 unless ($pos);
186
187   while ($pos < length($text)) {
188     $pos++;
189
190     next if (substr($text, $pos - 1, 5) ne '&lt;%');
191
192     if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
193       $depth++;
194
195     } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
196       if (!$var) {
197         $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
198         return undef;
199       }
200
201       my $block = substr($text, 0, $pos - 1);
202       substr($text, 0, $pos - 1) = "";
203       $text =~ s!^\&lt;\%[^\%]+\%\&gt;!!;
204       $text = '&lt;%if' . ($not ?  " " : "not ") . $var . '%&gt;' . $text;
205
206       return ($block, $text);
207
208     } elsif (substr($text, $pos + 4, 3) eq 'end') {
209       $depth--;
210       if ($depth == 0) {
211         my $block = substr($text, 0, $pos - 1);
212         substr($text, 0, $pos - 1) = "";
213         $text =~ s!^\&lt;\%[^\%]+\%\&gt;!!;
214
215         return ($block, $text);
216       }
217     }
218   }
219
220   return undef;
221 }
222
223 sub parse_block {
224   $main::lxdebug->enter_sub();
225
226   my ($self, $contents, @indices) = @_;
227
228   my $new_contents = "";
229
230   while ($contents ne "") {
231     if (substr($contents, 0, 1) eq "<") {
232       $contents =~ m|^(<[^>]+>)|;
233       my $tag = $1;
234       substr($contents, 0, length($1)) = "";
235
236       $self->{current_text_style} = $1 if $tag =~ m|text:style-name\s*=\s*"([^"]+)"|;
237
238       push @{ $self->{tag_stack} }, $tag;
239
240       if ($tag =~ m|<table:table-row|) {
241         $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
242         my $table_row = $1;
243         my $end_tag = $2;
244
245         if ($table_row =~ m|\&lt;\%foreachrow\s+(.*?)\%\&gt;|) {
246           my $var = $1;
247
248           $contents =~ m|^(.*?)(\&lt;\%foreachrow\s+.*?\%\&gt;)|;
249           substr($contents, length($1), length($2)) = "";
250
251           ($table_row, $contents) = $self->find_end($contents, length($1));
252           if (!$table_row) {
253             $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
254             $main::lxdebug->leave_sub();
255             return undef;
256           }
257
258           $contents   =~ m|^(.*?)(</table:table-row[^>]*>)|;
259           $table_row .=  $1;
260           $end_tag    =  $2;
261
262           substr $contents, 0, length($1) + length($2), '';
263
264           my $new_text = $self->parse_foreach($var, $table_row, $tag, $end_tag, @indices);
265           if (!defined($new_text)) {
266             $main::lxdebug->leave_sub();
267             return undef;
268           }
269           $new_contents .= $new_text;
270
271         } else {
272           substr($contents, 0, length($table_row) + length($end_tag)) = "";
273           my $new_text = $self->parse_block($table_row, @indices);
274           if (!defined($new_text)) {
275             $main::lxdebug->leave_sub();
276             return undef;
277           }
278           $new_contents .= $tag . $new_text . $end_tag;
279         }
280
281       } else {
282         $new_contents .= $tag;
283       }
284
285       if ($tag =~ m{^</ | />$}x) {
286         # $::lxdebug->message(0, "popping top tag is $tag top " . $self->{tag_stack}->[-1]);
287         pop @{ $self->{tag_stack} };
288       }
289
290     } else {
291       $contents =~ /^([^<]+)/;
292       my $text = $1;
293
294       my $pos_if = index($text, '&lt;%if');
295       my $pos_foreach = index($text, '&lt;%foreach');
296
297       if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
298         substr($contents, 0, length($text)) = "";
299         $new_contents .= $self->substitute_vars($text, @indices);
300         next;
301       }
302
303       if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
304         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
305         substr($contents, 0, $pos_foreach) = "";
306
307         if ($contents !~ m|^(\&lt;\%foreach (.*?)\%\&gt;)|) {
308           $self->{"error"} = "Malformed <\%foreach\%>.";
309           $main::lxdebug->leave_sub();
310           return undef;
311         }
312
313         my $var = $2;
314
315         substr($contents, 0, length($1)) = "";
316
317         my $block;
318         ($block, $contents) = $self->find_end($contents);
319         if (!$block) {
320           $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
321           $main::lxdebug->leave_sub();
322           return undef;
323         }
324
325         my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
326         if (!defined($new_text)) {
327           $main::lxdebug->leave_sub();
328           return undef;
329         }
330         $new_contents .= $new_text;
331
332       } else {
333         if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
334           $main::lxdebug->leave_sub();
335           return undef;
336         }
337       }
338     }
339   }
340
341   $main::lxdebug->leave_sub();
342
343   return $new_contents;
344 }
345
346 sub parse {
347   $main::lxdebug->enter_sub();
348   my $self = $_[0];
349   local *OUT = $_[1];
350   my $form = $self->{"form"};
351
352   close(OUT);
353
354   my $file_name;
355   if ($form->{"IN"} =~ m|^/|) {
356     $file_name = $form->{"IN"};
357   } else {
358     $file_name = $form->{"templates"} . "/" . $form->{"IN"};
359   }
360
361   my $zip = Archive::Zip->new();
362   if (Archive::Zip->AZ_OK != $zip->read($file_name)) {
363     $self->{"error"} = "File not found/is not a OpenDocument file.";
364     $main::lxdebug->leave_sub();
365     return 0;
366   }
367
368   my $contents = Encode::decode('utf-8-strict', $zip->contents("content.xml"));
369   if (!$contents) {
370     $self->{"error"} = "File is not a OpenDocument file.";
371     $main::lxdebug->leave_sub();
372     return 0;
373   }
374
375   $self->{current_text_style} =  '';
376   $self->{used_list_styles}   =  {
377     itemize                   => {},
378     enumerate                 => {},
379   };
380
381   my $new_contents;
382   if ($self->{use_template_toolkit}) {
383     my $additional_params = $::form;
384
385     $::form->template->process(\$contents, $additional_params, \$new_contents) || die $::form->template->error;
386   } else {
387     $self->{tag_stack} = [];
388     $new_contents = $self->parse_block($contents);
389   }
390   if (!defined($new_contents)) {
391     $main::lxdebug->leave_sub();
392     return 0;
393   }
394
395   my $new_styles = SL::Template::OpenDocument::Styles->get_style('text_basic');
396
397   foreach my $type (qw(itemize enumerate)) {
398     foreach my $parent (sort { $a cmp $b } keys %{ $self->{used_list_styles}->{$type} }) {
399       $new_styles .= SL::Template::OpenDocument::Styles->get_style('text_list_item', TYPE => $type, PARENT => $parent)
400                    .  SL::Template::OpenDocument::Styles->get_style("list_${type}",  TYPE => $type, PARENT => $parent);
401     }
402   }
403
404   # $::lxdebug->dump(0, "new_Styles", $new_styles);
405
406   $new_contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
407   $new_contents =~ s|[\n\r]||gm;
408
409 #   $new_contents =~ s|>|>\n|g;
410
411   $zip->contents("content.xml", Encode::encode('utf-8-strict', $new_contents));
412
413   my $styles = Encode::decode('utf-8-strict', $zip->contents("styles.xml"));
414   if ($contents) {
415     my $new_styles = $self->parse_block($styles);
416     if (!defined($new_contents)) {
417       $main::lxdebug->leave_sub();
418       return 0;
419     }
420     $zip->contents("styles.xml", Encode::encode('utf-8-strict', $new_styles));
421   }
422
423   $zip->writeToFileNamed($form->{"tmpfile"}, 1);
424
425   my $res = 1;
426   if ($form->{"format"} =~ /pdf/) {
427     $res = $self->convert_to_pdf();
428   }
429
430   $main::lxdebug->leave_sub();
431   return $res;
432 }
433
434 sub is_xvfb_running {
435   $main::lxdebug->enter_sub();
436
437   my ($self) = @_;
438
439   local *IN;
440   my $dfname = $self->{"userspath"} . "/xvfb_display";
441   my $display;
442
443   $main::lxdebug->message(LXDebug->DEBUG2(), "    Looking for $dfname\n");
444   if ((-f $dfname) && open(IN, $dfname)) {
445     my $pid = <IN>;
446     chomp($pid);
447     $display = <IN>;
448     chomp($display);
449     my $xauthority = <IN>;
450     chomp($xauthority);
451     close(IN);
452
453     $main::lxdebug->message(LXDebug->DEBUG2(), "      found with $pid and $display\n");
454
455     if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
456       $main::lxdebug->message(LXDebug->DEBUG2(), "  no/wrong process #1\n");
457       unlink($dfname, $xauthority);
458       $main::lxdebug->leave_sub();
459       return undef;
460     }
461     my $line = <IN>;
462     close(IN);
463     if ($line !~ /xvfb/i) {
464       $main::lxdebug->message(LXDebug->DEBUG2(), "      no/wrong process #2\n");
465       unlink($dfname, $xauthority);
466       $main::lxdebug->leave_sub();
467       return undef;
468     }
469
470     $ENV{"XAUTHORITY"} = $xauthority;
471     $ENV{"DISPLAY"} = $display;
472   } else {
473     $main::lxdebug->message(LXDebug->DEBUG2(), "      not found\n");
474   }
475
476   $main::lxdebug->leave_sub();
477
478   return $display;
479 }
480
481 sub spawn_xvfb {
482   $main::lxdebug->enter_sub();
483
484   my ($self) = @_;
485
486   $main::lxdebug->message(LXDebug->DEBUG2, "spawn_xvfb()\n");
487
488   my $display = $self->is_xvfb_running();
489
490   if ($display) {
491     $main::lxdebug->leave_sub();
492     return $display;
493   }
494
495   $display = 99;
496   while ( -f "/tmp/.X${display}-lock") {
497     $display++;
498   }
499   $display = ":${display}";
500   $main::lxdebug->message(LXDebug->DEBUG2(), "  display $display\n");
501
502   my $mcookie = `mcookie`;
503   die("Installation error: mcookie not found.") if ($? != 0);
504   chomp($mcookie);
505
506   $main::lxdebug->message(LXDebug->DEBUG2(), "  mcookie $mcookie\n");
507
508   my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
509   $ENV{"XAUTHORITY"} = $xauthority;
510
511   $main::lxdebug->message(LXDebug->DEBUG2(), "  xauthority $xauthority\n");
512
513   if (system("xauth add \"${display}\" . \"${mcookie}\"") == -1) {
514     die "system call to xauth failed: $!";
515   }
516   if ($? != 0) {
517     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
518     $main::lxdebug->leave_sub();
519     return undef;
520   }
521
522   $main::lxdebug->message(LXDebug->DEBUG2(), "  about to fork()\n");
523
524   my $pid = fork();
525   if (0 == $pid) {
526     $main::lxdebug->message(LXDebug->DEBUG2(), "  Child execing\n");
527     exec($::lx_office_conf{applications}->{xvfb}, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
528   }
529   sleep(3);
530   $main::lxdebug->message(LXDebug->DEBUG2(), "  parent dont sleeping\n");
531
532   local *OUT;
533   my $dfname = $self->{"userspath"} . "/xvfb_display";
534   if (!open(OUT, ">", $dfname)) {
535     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
536     unlink($xauthority);
537     kill($pid);
538     $main::lxdebug->leave_sub();
539     return undef;
540   }
541   print(OUT "$pid\n$display\n$xauthority\n");
542   close(OUT);
543
544   $main::lxdebug->message(LXDebug->DEBUG2(), "  parent re-testing\n");
545
546   if (!$self->is_xvfb_running()) {
547     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
548     unlink($xauthority, $dfname);
549     kill($pid);
550     $main::lxdebug->leave_sub();
551     return undef;
552   }
553
554   $main::lxdebug->message(LXDebug->DEBUG2(), "  spawn OK\n");
555
556   $main::lxdebug->leave_sub();
557
558   return $display;
559 }
560
561 sub _run_python_uno {
562   my ($self, @args) = @_;
563
564   local $ENV{PYTHONPATH};
565   $ENV{PYTHONPATH} = $::lx_office_conf{environment}->{python_uno_path} . ':' . $ENV{PYTHONPATH} if $::lx_office_conf{environment}->{python_uno_path};
566   my $cmd          = $::lx_office_conf{applications}->{python_uno} . ' ' . join(' ', @args);
567   return `$cmd`;
568 }
569
570 sub is_openoffice_running {
571   my ($self) = @_;
572
573   $main::lxdebug->enter_sub();
574
575   my $output = $self->_run_python_uno('./scripts/oo-uno-test-conn.py', $::lx_office_conf{print_templates}->{openofficeorg_daemon_port}, ' 2> /dev/null');
576   chomp $output;
577
578   my $res = ($? == 0) || $output;
579   $main::lxdebug->message(LXDebug->DEBUG2(), "  is_openoffice_running(): res $res\n");
580
581   $main::lxdebug->leave_sub();
582
583   return $res;
584 }
585
586 sub spawn_openoffice {
587   $main::lxdebug->enter_sub();
588
589   my ($self) = @_;
590
591   $main::lxdebug->message(LXDebug->DEBUG2(), "spawn_openoffice()\n");
592
593   my ($try, $spawned_oo, $res);
594
595   $res = 0;
596   for ($try = 0; $try < 15; $try++) {
597     if ($self->is_openoffice_running()) {
598       $res = 1;
599       last;
600     }
601
602     if ($::dispatcher->interface_type eq 'FastCGI') {
603       $::dispatcher->{request}->Detach;
604     }
605
606     if (!$spawned_oo) {
607       my $pid = fork();
608       if (0 == $pid) {
609         $main::lxdebug->message(LXDebug->DEBUG2(), "  Child daemonizing\n");
610
611         if ($::dispatcher->interface_type eq 'FastCGI') {
612           $::dispatcher->{request}->Finish;
613           $::dispatcher->{request}->LastCall;
614         }
615         chdir('/');
616         open(STDIN, '/dev/null');
617         open(STDOUT, '>/dev/null');
618         my $new_pid = fork();
619         exit if ($new_pid);
620         my $ssres = setsid();
621         $main::lxdebug->message(LXDebug->DEBUG2(), "  Child execing\n");
622         my @cmdline = ($::lx_office_conf{applications}->{openofficeorg_writer},
623                        "-minimized", "-norestore", "-nologo", "-nolockcheck",
624                        "-headless",
625                        "-accept=socket,host=localhost,port=" .
626                        $::lx_office_conf{print_templates}->{openofficeorg_daemon_port} . ";urp;");
627         exec(@cmdline);
628       } else {
629         # parent
630         if ($::dispatcher->interface_type eq 'FastCGI') {
631           $::dispatcher->{request}->Attach;
632         }
633       }
634
635       $main::lxdebug->message(LXDebug->DEBUG2(), "  Parent after fork\n");
636       $spawned_oo = 1;
637       sleep(3);
638     }
639
640     sleep($try >= 5 ? 2 : 1);
641   }
642
643   if (!$res) {
644     $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
645       "OpenOffice could not be started.";
646   }
647
648   $main::lxdebug->leave_sub();
649
650   return $res;
651 }
652
653 sub convert_to_pdf {
654   $main::lxdebug->enter_sub();
655
656   my ($self) = @_;
657
658   my $form = $self->{"form"};
659
660   my $filename = $form->{"tmpfile"};
661   $filename =~ s/.odt$//;
662   if (substr($filename, 0, 1) ne "/") {
663     $filename = getcwd() . "/${filename}";
664   }
665
666   if (substr($self->{"userspath"}, 0, 1) eq "/") {
667     $ENV{'HOME'} = $self->{"userspath"};
668   } else {
669     $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
670   }
671
672   if (!$self->spawn_xvfb()) {
673     $main::lxdebug->leave_sub();
674     return 0;
675   }
676
677   if (!$::lx_office_conf{print_templates}->{openofficeorg_daemon}) {
678     if (system($::lx_office_conf{applications}->{openofficeorg_writer},
679                "-minimized", "-norestore", "-nologo", "-nolockcheck", "-headless",
680                "file:${filename}.odt",
681                "macro://" . (split('/', $filename))[-1] . "/Standard.Conversion.ConvertSelfToPDF()") == -1) {
682       die "system call to $::lx_office_conf{applications}->{openofficeorg_writer} failed: $!";
683     }
684   } else {
685     if (!$self->spawn_openoffice()) {
686       $main::lxdebug->leave_sub();
687       return 0;
688     }
689
690     $self->_run_python_uno('./scripts/oo-uno-convert-pdf.py', $::lx_office_conf{print_templates}->{openofficeorg_daemon_port}, "${filename}.odt");
691   }
692
693   my $res = $?;
694   if ((0 == $?) || (-f "${filename}.pdf" && -s "${filename}.pdf")) {
695     $form->{"tmpfile"} =~ s/odt$/pdf/;
696
697     unlink($filename . ".odt");
698
699     $main::lxdebug->leave_sub();
700     return 1;
701
702   }
703
704   unlink($filename . ".odt", $filename . ".pdf");
705   $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
706     "Exit code: $res";
707
708   $main::lxdebug->leave_sub();
709   return 0;
710 }
711
712 sub format_string {
713   my ($self, $content, $variable) = @_;
714
715   my $formatter =
716        $formatters{ $self->{variable_content_types}->{$variable} }
717     // $formatters{ $self->{default_content_type} }
718     // $formatters{ text };
719
720   return $formatter->($self, $content, variable => $variable);
721 }
722
723 sub get_mime_type() {
724   my ($self) = @_;
725
726   if ($self->{"form"}->{"format"} =~ /pdf/) {
727     return "application/pdf";
728   } else {
729     return "application/vnd.oasis.opendocument.text";
730   }
731 }
732
733 sub uses_temp_file {
734   return 1;
735 }
736
737 1;