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