1 #====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #====================================================================
9 package SimpleTemplate;
12 # 1. The template's file name
13 # 2. A reference to the Form object
14 # 3. A reference to the myconfig hash
17 # A new template object
31 $self->{source} = shift;
32 $self->{form} = shift;
33 $self->{myconfig} = shift;
34 $self->{userspath} = shift;
36 $self->{error} = undef;
38 $self->set_tag_style('<%', '%>');
43 my $tag_start = shift;
46 $self->{tag_start} = $tag_start;
47 $self->{tag_end} = $tag_end;
48 $self->{tag_start_qm} = quotemeta $tag_start;
49 $self->{tag_end_qm} = quotemeta $tag_end;
57 # 1. A typeglob for the file handle. The output will be written
58 # to this file handle.
61 # 1 on success and undef or 0 if there was an error. In the latter case
62 # the calling function can retrieve the error message via $obj->get_error()
67 print(OUT "Hallo!\n");
73 return $self->{"error"};
86 package LaTeXTemplate;
90 @ISA = qw(SimpleTemplate);
95 return $type->SUPER::new(@_);
99 my ($self, $variable) = @_;
100 my $form = $self->{"form"};
103 ('order' => [quotemeta("\\"),
105 '&', quotemeta("\n"),
106 '"', '\$', '%', '_', '#', quotemeta('^'),
107 '{', '}', '<', '>', '£', "\r", '±', '\xe1',
111 quotemeta("\\") => '\\textbackslash ',
126 '\xe1' => '$\bullet$',
127 quotemeta('^') => '\^\\',
128 quotemeta("\n") => '\newline ',
133 map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
135 # Allow some HTML markup to be converted into the output format's
136 # corresponding markup code, e.g. bold or italic.
137 my %markup_replace = ('b' => 'textbf',
141 foreach my $key (keys(%markup_replace)) {
142 my $new = $markup_replace{$key};
143 $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
146 $variable =~ s/[\x00-\x1f]//g;
151 sub substitute_vars {
152 my ($self, $text, @indices) = @_;
154 my $form = $self->{"form"};
156 while ($text =~ /$self->{tag_start_qm}(.+?)$self->{tag_end_qm}/) {
157 my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]);
158 my ($var, @options) = split(/\s+/, $1);
159 my $value = $form->{$var};
161 $main::lxdebug->message(0, "REPL var: $1");
163 for (my $i = 0; $i < scalar(@indices); $i++) {
164 last unless (ref($value) eq "ARRAY");
165 $value = $value->[$indices[$i]];
167 $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
168 substr($text, $tag_pos, $tag_len) = $value;
175 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
177 my ($form, $new_contents) = ($self->{"form"}, "");
179 my $ary = $form->{$var};
180 for (my $i = 0; $i < scalar(@indices); $i++) {
181 last unless (ref($ary) eq "ARRAY");
182 $ary = $ary->[$indices[$i]];
186 my $current_page = 1;
187 my ($current_line, $corrent_row) = (0, 1);
189 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
190 $form->{"__first__"} = $i == 0;
191 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
192 $form->{"__odd__"} = (($i + 1) % 2) == 1;
193 $form->{"__counter__"} = $i + 1;
195 if ((scalar(@{$form->{"description"}}) == scalar(@{$ary})) &&
196 $self->{"chars_per_line"}) {
198 int(length($form->{"description"}->[$i]) / $self->{"chars_per_line"});
201 $form->{"description"}->[$i] =~ s/(\\newline\s?)*$//;
202 my $_description = $form->{"description"}->[$i];
203 while ($_description =~ /\\newline/) {
205 $_description =~ s/\\newline//;
209 if ($current_page == 1) {
210 $lpp = $self->{"lines_on_first_page"};
212 $lpp = $self->{"lines_on_second_page"};
215 # Yes we need a manual page break -- or the user has forced one
216 if ((($current_line + $lines) > $lpp) ||
217 ($form->{"description"}->[$i] =~ /<pagebreak>/)) {
218 my $pb = $self->{"pagebreak_block"};
220 # replace the special variables <%sumcarriedforward%>
223 my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
224 $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
225 $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
227 my $new_text = $self->parse_block($pb, (@indices, $i));
228 return undef unless (defined($new_text));
229 $new_contents .= $new_text;
234 $current_line += $lines;
236 if ($i < scalar(@{$form->{"linetotal"}})) {
237 $sum += $form->parse_amount($self->{"myconfig"},
238 $form->{"linetotal"}->[$i]);
241 $form->{"cumulatelinetotal"}[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
243 my $new_text = $self->parse_block($text, (@indices, $i));
244 return undef unless (defined($new_text));
245 $new_contents .= $start_tag . $new_text . $end_tag;
247 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
249 return $new_contents;
253 my ($self, $text, $pos, $var, $not) = @_;
255 my $tag_start_len = length $self->{tag_start};
258 $pos = 0 unless ($pos);
260 while ($pos < length($text)) {
263 next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
265 my $keyword_pos = $pos - 1 + $tag_start_len;
267 if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 3) eq 'for')) {
270 } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
273 "$self->{tag_start}else$self->{tag_end} outside of "
274 . "$self->{tag_start}if$self->{tag_end} / "
275 . "$self->{tag_start}ifnot$self->{tag_end}.";
279 my $block = substr($text, 0, $pos - 1);
280 substr($text, 0, $pos - 1) = "";
281 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
282 $text = $self->{tag_start} . 'if' . ($not ? " " : "not ") . $var . $self->{tag_end} . $text;
284 return ($block, $text);
286 } elsif (substr($text, $keyword_pos, 3) eq 'end') {
289 my $block = substr($text, 0, $pos - 1);
290 substr($text, 0, $pos - 1) = "";
291 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
293 return ($block, $text);
302 $main::lxdebug->enter_sub();
304 my ($self, $contents, @indices) = @_;
306 my $new_contents = "";
308 while ($contents ne "") {
309 my $pos_if = index($contents, $self->{tag_start} . 'if');
310 my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
312 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
313 $new_contents .= $self->substitute_vars($contents, @indices);
317 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
318 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
319 substr($contents, 0, $pos_foreach) = "";
321 if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
322 $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
323 $main::lxdebug->leave_sub();
329 substr($contents, 0, length($&)) = "";
332 ($block, $contents) = $self->find_end($contents);
334 $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
335 $main::lxdebug->leave_sub();
339 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
340 if (!defined($new_text)) {
341 $main::lxdebug->leave_sub();
344 $new_contents .= $new_text;
347 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
348 substr($contents, 0, $pos_if) = "";
350 if ($contents !~ m|^$self->{tag_start_qm}if\s*(not)?\s+(.*?)$self->{tag_end_qm}|) {
351 $self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}.";
352 $main::lxdebug->leave_sub();
356 my ($not, $var) = ($1, $2);
358 substr($contents, 0, length($&)) = "";
360 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
362 $self->{"error"} = "Unclosed $self->{tag_start}if${not}$self->{tag_end}." unless ($self->{"error"});
363 $main::lxdebug->leave_sub();
367 my $value = $self->{"form"}->{$var};
368 for (my $i = 0; $i < scalar(@indices); $i++) {
369 last unless (ref($value) eq "ARRAY");
370 $value = $value->[$indices[$i]];
373 if (($not && !$value) || (!$not && $value)) {
374 my $new_text = $self->parse_block($block, @indices);
375 if (!defined($new_text)) {
376 $main::lxdebug->leave_sub();
379 $new_contents .= $new_text;
384 $main::lxdebug->leave_sub();
386 return $new_contents;
389 sub parse_first_line {
391 my $line = shift || "";
393 if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
395 $self->{error} = "The tag start and end markers must not be equal.";
399 $self->set_tag_style($1, $2);
408 my $form = $self->{"form"};
410 if (!open(IN, "$form->{templates}/$form->{IN}")) {
411 $self->{"error"} = "$!";
417 return 0 if (!$self->parse_first_line($lines[0]));
419 my $contents = join("", @lines);
421 # detect pagebreak block and its parameters
422 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) {
423 $self->{"chars_per_line"} = $1;
424 $self->{"lines_on_first_page"} = $2;
425 $self->{"lines_on_second_page"} = $3;
426 $self->{"pagebreak_block"} = $4;
428 substr($contents, length($`), length($&)) = "";
431 $self->{"forced_pagebreaks"} = [];
433 my $new_contents = $self->parse_block($contents);
434 if (!defined($new_contents)) {
435 $main::lxdebug->leave_sub();
439 print(OUT $new_contents);
441 if ($form->{"format"} =~ /postscript/i) {
442 return $self->convert_to_postscript();
443 } elsif ($form->{"format"} =~ /pdf/i) {
444 return $self->convert_to_pdf();
450 sub convert_to_postscript {
452 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
454 # Convert the tex file to postscript
456 if (!chdir("$userspath")) {
457 $self->{"error"} = "chdir : $!";
462 $form->{tmpfile} =~ s/$userspath\///g;
464 for (my $run = 1; $run <= 2; $run++) {
465 system("latex --interaction=nonstopmode $form->{tmpfile} " .
466 "> $form->{tmpfile}.err");
468 $self->{"error"} = $form->cleanup();
474 $form->{tmpfile} =~ s/tex$/dvi/;
476 system("dvips $form->{tmpfile} -o -q > /dev/null");
478 $self->{"error"} = "dvips : $!";
482 $form->{tmpfile} =~ s/dvi$/ps/;
491 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
493 # Convert the tex file to PDF
495 if (!chdir("$userspath")) {
496 $self->{"error"} = "chdir : $!";
501 $form->{tmpfile} =~ s/$userspath\///g;
503 for (my $run = 1; $run <= 2; $run++) {
504 system("pdflatex --interaction=nonstopmode $form->{tmpfile} " .
505 "> $form->{tmpfile}.err");
507 $self->{"error"} = $form->cleanup();
513 $form->{tmpfile} =~ s/tex$/pdf/;
518 sub get_mime_type() {
521 if ($self->{"form"}->{"format"} =~ /postscript/i) {
522 return "application/postscript";
524 return "application/pdf";
537 package HTMLTemplate;
541 @ISA = qw(LaTeXTemplate);
546 return $type->SUPER::new(@_);
550 my ($self, $variable) = @_;
551 my $form = $self->{"form"};
554 ('order' => ['<', '>', quotemeta("\n")],
557 quotemeta("\n") => '<br>',
560 map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
562 # Allow some HTML markup to be converted into the output format's
563 # corresponding markup code, e.g. bold or italic.
564 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
566 foreach my $key (@markup_replace) {
567 $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
573 sub get_mime_type() {
576 if ($self->{"form"}->{"format"} =~ /postscript/i) {
577 return "application/postscript";
578 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
579 return "application/pdf";
588 if ($self->{"form"}->{"format"} =~ /postscript/i) {
590 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
597 sub convert_to_postscript {
599 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
601 # Convert the HTML file to postscript
603 if (!chdir("$userspath")) {
604 $self->{"error"} = "chdir : $!";
609 $form->{"tmpfile"} =~ s/$userspath\///g;
610 my $psfile = $form->{"tmpfile"};
611 $psfile =~ s/.html/.ps/;
612 if ($psfile eq $form->{"tmpfile"}) {
616 system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
618 $self->{"error"} = $form->cleanup();
623 $form->{"tmpfile"} = $psfile;
632 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
634 # Convert the HTML file to PDF
636 if (!chdir("$userspath")) {
637 $self->{"error"} = "chdir : $!";
642 $form->{"tmpfile"} =~ s/$userspath\///g;
643 my $pdffile = $form->{"tmpfile"};
644 $pdffile =~ s/.html/.pdf/;
645 if ($pdffile eq $form->{"tmpfile"}) {
649 system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
651 $self->{"error"} = $form->cleanup();
656 $form->{"tmpfile"} = $pdffile;
665 #### PlainTextTemplate
668 package PlainTextTemplate;
672 @ISA = qw(LaTeXTemplate);
677 return $type->SUPER::new(@_);
681 my ($self, $variable) = @_;
696 #### OpenDocumentTemplate
699 package OpenDocumentTemplate;
707 # use File::Temp qw(:mktemp);
710 @ISA = qw(SimpleTemplate);
715 $self = $type->SUPER::new(@_);
717 foreach my $module (qw(Archive::Zip Text::Iconv)) {
718 eval("use ${module};");
720 $self->{"form"}->error("The Perl module '${module}' could not be " .
721 "loaded. Support for OpenDocument templates " .
722 "does not work without it. Please install your " .
723 "distribution's package or get the module from " .
724 "CPAN ( http://www.cpan.org ).");
728 $self->{"rnd"} = int(rand(1000000));
729 $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
734 sub substitute_vars {
735 my ($self, $text, @indices) = @_;
737 my $form = $self->{"form"};
739 while ($text =~ /\<\%(.*?)\%\>/) {
740 my $value = $form->{$1};
742 for (my $i = 0; $i < scalar(@indices); $i++) {
743 last unless (ref($value) eq "ARRAY");
744 $value = $value->[$indices[$i]];
746 substr($text, $-[0], $+[0] - $-[0]) = $self->format_string($value);
753 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
755 my ($form, $new_contents) = ($self->{"form"}, "");
757 my $ary = $form->{$var};
758 for (my $i = 0; $i < scalar(@indices); $i++) {
759 last unless (ref($ary) eq "ARRAY");
760 $ary = $ary->[$indices[$i]];
763 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
764 $form->{"__first__"} = $i == 0;
765 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
766 $form->{"__odd__"} = (($i + 1) % 2) == 1;
767 $form->{"__counter__"} = $i + 1;
768 my $new_text = $self->parse_block($text, (@indices, $i));
769 return undef unless (defined($new_text));
770 $new_contents .= $start_tag . $new_text . $end_tag;
772 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
774 return $new_contents;
778 my ($self, $text, $pos, $var, $not) = @_;
781 $pos = 0 unless ($pos);
783 while ($pos < length($text)) {
786 next if (substr($text, $pos - 1, 5) ne '<%');
788 if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
791 } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
793 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
797 my $block = substr($text, 0, $pos - 1);
798 substr($text, 0, $pos - 1) = "";
799 $text =~ s!^\<\%[^\%]+\%\>!!;
800 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
802 return ($block, $text);
804 } elsif (substr($text, $pos + 4, 3) eq 'end') {
807 my $block = substr($text, 0, $pos - 1);
808 substr($text, 0, $pos - 1) = "";
809 $text =~ s!^\<\%[^\%]+\%\>!!;
811 return ($block, $text);
820 $main::lxdebug->enter_sub();
822 my ($self, $contents, @indices) = @_;
824 my $new_contents = "";
826 while ($contents ne "") {
827 if (substr($contents, 0, 1) eq "<") {
828 $contents =~ m|^<[^>]+>|;
830 substr($contents, 0, length($&)) = "";
832 if ($tag =~ m|<table:table-row|) {
833 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
836 substr($contents, 0, length($1) + length($end_tag)) = "";
838 if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
841 substr($table_row, length($`), length($&)) = "";
843 my ($t1, $t2) = $self->find_end($table_row, length($`));
845 $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
846 $main::lxdebug->leave_sub();
850 my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
851 if (!defined($new_text)) {
852 $main::lxdebug->leave_sub();
855 $new_contents .= $new_text;
858 my $new_text = $self->parse_block($table_row, @indices);
859 if (!defined($new_text)) {
860 $main::lxdebug->leave_sub();
863 $new_contents .= $tag . $new_text . $end_tag;
867 $new_contents .= $tag;
871 $contents =~ /^[^<]+/;
874 my $pos_if = index($text, '<%if');
875 my $pos_foreach = index($text, '<%foreach');
877 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
878 substr($contents, 0, length($text)) = "";
879 $new_contents .= $self->substitute_vars($text, @indices);
883 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
884 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
885 substr($contents, 0, $pos_foreach) = "";
887 if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
888 $self->{"error"} = "Malformed <\%foreach\%>.";
889 $main::lxdebug->leave_sub();
895 substr($contents, 0, length($&)) = "";
898 ($block, $contents) = $self->find_end($contents);
900 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
901 $main::lxdebug->leave_sub();
905 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
906 if (!defined($new_text)) {
907 $main::lxdebug->leave_sub();
910 $new_contents .= $new_text;
913 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
914 substr($contents, 0, $pos_if) = "";
916 if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
917 $self->{"error"} = "Malformed <\%if\%>.";
918 $main::lxdebug->leave_sub();
922 my ($not, $var) = ($1, $2);
924 substr($contents, 0, length($&)) = "";
926 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
928 $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
929 $main::lxdebug->leave_sub();
933 my $value = $self->{"form"}->{$var};
934 for (my $i = 0; $i < scalar(@indices); $i++) {
935 last unless (ref($value) eq "ARRAY");
936 $value = $value->[$indices[$i]];
939 if (($not && !$value) || (!$not && $value)) {
940 my $new_text = $self->parse_block($block, @indices);
941 if (!defined($new_text)) {
942 $main::lxdebug->leave_sub();
945 $new_contents .= $new_text;
951 $main::lxdebug->leave_sub();
953 return $new_contents;
957 $main::lxdebug->enter_sub();
961 my $form = $self->{"form"};
966 if ($form->{"IN"} =~ m|^/|) {
967 $file_name = $form->{"IN"};
969 $file_name = $form->{"templates"} . "/" . $form->{"IN"};
972 my $zip = Archive::Zip->new();
973 if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
974 $self->{"error"} = "File not found/is not a OpenDocument file.";
975 $main::lxdebug->leave_sub();
979 my $contents = $zip->contents("content.xml");
981 $self->{"error"} = "File is not a OpenDocument file.";
982 $main::lxdebug->leave_sub();
986 my $rnd = $self->{"rnd"};
987 my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
988 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
990 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
991 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
993 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
994 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
996 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
997 <style:text-properties style:text-line-through-style="solid"/>
999 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
1000 <style:text-properties style:text-position="super 58%"/>
1002 <style:style style:name="TLXO${rnd}SUB" style:family="text">
1003 <style:text-properties style:text-position="sub 58%"/>
1007 $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
1008 $contents =~ s|[\n\r]||gm;
1010 my $new_contents = $self->parse_block($contents);
1011 if (!defined($new_contents)) {
1012 $main::lxdebug->leave_sub();
1016 # $new_contents =~ s|>|>\n|g;
1018 $zip->contents("content.xml", $new_contents);
1020 my $styles = $zip->contents("styles.xml");
1022 my $new_styles = $self->parse_block($styles);
1023 if (!defined($new_contents)) {
1024 $main::lxdebug->leave_sub();
1027 $zip->contents("styles.xml", $new_styles);
1030 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
1033 if ($form->{"format"} =~ /pdf/) {
1034 $res = $self->convert_to_pdf();
1037 $main::lxdebug->leave_sub();
1041 sub is_xvfb_running {
1042 $main::lxdebug->enter_sub();
1047 my $dfname = $self->{"userspath"} . "/xvfb_display";
1050 $main::lxdebug->message(LXDebug::DEBUG2, " Looking for $dfname\n");
1051 if ((-f $dfname) && open(IN, $dfname)) {
1056 my $xauthority = <IN>;
1060 $main::lxdebug->message(LXDebug::DEBUG2, " found with $pid and $display\n");
1062 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
1063 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #1\n");
1064 unlink($dfname, $xauthority);
1065 $main::lxdebug->leave_sub();
1070 if ($line !~ /xvfb/i) {
1071 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #2\n");
1072 unlink($dfname, $xauthority);
1073 $main::lxdebug->leave_sub();
1077 $ENV{"XAUTHORITY"} = $xauthority;
1078 $ENV{"DISPLAY"} = $display;
1080 $main::lxdebug->message(LXDebug::DEBUG2, " not found\n");
1083 $main::lxdebug->leave_sub();
1089 $main::lxdebug->enter_sub();
1093 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
1095 my $display = $self->is_xvfb_running();
1098 $main::lxdebug->leave_sub();
1103 while ( -f "/tmp/.X${display}-lock") {
1106 $display = ":${display}";
1107 $main::lxdebug->message(LXDebug::DEBUG2, " display $display\n");
1109 my $mcookie = `mcookie`;
1110 die("Installation error: mcookie not found.") if ($? != 0);
1113 $main::lxdebug->message(LXDebug::DEBUG2, " mcookie $mcookie\n");
1115 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
1116 $ENV{"XAUTHORITY"} = $xauthority;
1118 $main::lxdebug->message(LXDebug::DEBUG2, " xauthority $xauthority\n");
1120 system("xauth add \"${display}\" . \"${mcookie}\"");
1122 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
1123 $main::lxdebug->leave_sub();
1127 $main::lxdebug->message(LXDebug::DEBUG2, " about to fork()\n");
1131 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1132 exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
1135 $main::lxdebug->message(LXDebug::DEBUG2, " parent dont sleeping\n");
1138 my $dfname = $self->{"userspath"} . "/xvfb_display";
1139 if (!open(OUT, ">$dfname")) {
1140 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
1141 unlink($xauthority);
1143 $main::lxdebug->leave_sub();
1146 print(OUT "$pid\n$display\n$xauthority\n");
1149 $main::lxdebug->message(LXDebug::DEBUG2, " parent re-testing\n");
1151 if (!$self->is_xvfb_running()) {
1152 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
1153 unlink($xauthority, $dfname);
1155 $main::lxdebug->leave_sub();
1159 $main::lxdebug->message(LXDebug::DEBUG2, " spawn OK\n");
1161 $main::lxdebug->leave_sub();
1166 sub is_openoffice_running {
1167 $main::lxdebug->enter_sub();
1169 system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
1170 "> /dev/null 2> /dev/null");
1172 $main::lxdebug->message(LXDebug::DEBUG2, " is_openoffice_running(): $?\n");
1174 $main::lxdebug->leave_sub();
1179 sub spawn_openoffice {
1180 $main::lxdebug->enter_sub();
1184 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
1186 my ($try, $spawned_oo, $res);
1189 for ($try = 0; $try < 15; $try++) {
1190 if ($self->is_openoffice_running()) {
1198 $main::lxdebug->message(LXDebug::DEBUG2, " Child daemonizing\n");
1200 open(STDIN, '/dev/null');
1201 open(STDOUT, '>/dev/null');
1202 my $new_pid = fork();
1204 my $ssres = setsid();
1205 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1206 my @cmdline = ($main::openofficeorg_writer_bin,
1207 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1209 "-accept=socket,host=localhost,port=" .
1210 $main::openofficeorg_daemon_port . ";urp;");
1214 $main::lxdebug->message(LXDebug::DEBUG2, " Parent after fork\n");
1219 sleep($try >= 5 ? 2 : 1);
1223 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
1224 "OpenOffice could not be started.";
1227 $main::lxdebug->leave_sub();
1232 sub convert_to_pdf {
1233 $main::lxdebug->enter_sub();
1237 my $form = $self->{"form"};
1239 my $filename = $form->{"tmpfile"};
1240 $filename =~ s/.odt$//;
1241 if (substr($filename, 0, 1) ne "/") {
1242 $filename = getcwd() . "/${filename}";
1245 if (substr($self->{"userspath"}, 0, 1) eq "/") {
1246 $ENV{'HOME'} = $self->{"userspath"};
1248 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
1251 if (!$self->spawn_xvfb()) {
1252 $main::lxdebug->leave_sub();
1257 if (!$main::openofficeorg_daemon) {
1258 @cmdline = ($main::openofficeorg_writer_bin,
1259 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1261 "file:${filename}.odt",
1262 "macro://" . (split('/', $filename))[-1] .
1263 "/Standard.Conversion.ConvertSelfToPDF()");
1265 if (!$self->spawn_openoffice()) {
1266 $main::lxdebug->leave_sub();
1270 @cmdline = ("./scripts/oo-uno-convert-pdf.py",
1271 $main::openofficeorg_daemon_port,
1279 $form->{"tmpfile"} =~ s/odt$/pdf/;
1281 unlink($filename . ".odt");
1283 $main::lxdebug->leave_sub();
1288 unlink($filename . ".odt", $filename . ".pdf");
1289 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
1292 $main::lxdebug->leave_sub();
1297 my ($self, $variable) = @_;
1298 my $form = $self->{"form"};
1299 my $iconv = $self->{"iconv"};
1302 ('order' => ['&', '<', '>', '"', "'",
1304 quotemeta("\n"), quotemeta("\r")],
1310 '\x80' => chr(0xa4), # Euro
1311 quotemeta("\n") => '<text:line-break/>',
1312 quotemeta("\r") => '',
1315 map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
1317 # Allow some HTML markup to be converted into the output format's
1318 # corresponding markup code, e.g. bold or italic.
1319 my $rnd = $self->{"rnd"};
1320 my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
1321 "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
1323 foreach my $key (keys(%markup_replace)) {
1324 my $value = $markup_replace{$key};
1325 $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi;
1326 $variable =~ s|\</${key}\>|</text:span>|gi;
1329 return $iconv->convert($variable);
1332 sub get_mime_type() {
1333 if ($self->{"form"}->{"format"} =~ /pdf/) {
1334 return "application/pdf";
1336 return "application/vnd.oasis.opendocument.text";
1340 sub uses_temp_file {
1345 ##########################################################
1349 ##########################################################
1351 package XMLTemplate;
1355 @ISA = qw(HTMLTemplate);
1358 #evtl auskommentieren
1361 return $type->SUPER::new(@_);
1365 my ($self, $variable) = @_;
1366 my $form = $self->{"form"};
1369 ('order' => ['<', '>', quotemeta("\n")],
1372 quotemeta("\n") => '<br>',
1375 map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
1377 # Allow no markup to be converted into the output format
1378 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
1380 foreach my $key (@markup_replace) {
1381 $variable =~ s/\<(\/?)${key}\>//g;
1387 sub get_mime_type() {
1390 if ($self->{"form"}->{"format"} =~ /elsterwinston/i) {
1391 return "application/xml ";
1392 } elsif ($self->{"form"}->{"format"} =~ /elstertaxbird/i) {
1393 return "application/x-taxbird";
1399 sub uses_temp_file {
1400 # tempfile needet for XML Output