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;
51 $self->{substitute_vars_re} = "$self->{tag_start_qm}(.+?)$self->{tag_end_qm}";
59 # 1. A typeglob for the file handle. The output will be written
60 # to this file handle.
63 # 1 on success and undef or 0 if there was an error. In the latter case
64 # the calling function can retrieve the error message via $obj->get_error()
69 print(OUT "Hallo!\n");
75 return $self->{"error"};
82 sub _get_loop_variable_value {
87 my $form = $self->{form};
88 my $value = $self->{form}->{$var};
90 for (my $i = 0; $i < scalar(@indices); $i++) {
91 last unless (ref($value) eq "ARRAY");
92 $value = $value->[$indices[$i]];
99 my ($self, $text, @indices) = @_;
101 my $form = $self->{"form"};
103 while ($text =~ /$self->{substitute_vars_re}/) {
104 my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]);
105 my ($var, @options) = split(/\s+/, $1);
107 my $value = $self->_get_loop_variable_value($var, @indices);
108 $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
110 substr($text, $tag_pos, $tag_len, $value);
122 package LaTeXTemplate;
126 @ISA = qw(SimpleTemplate);
131 my $self = $type->SUPER::new(@_);
137 my ($self, $variable) = @_;
138 my $form = $self->{"form"};
140 $variable = $main::locale->quote_special_chars('Template/LaTeX', $variable);
142 # Allow some HTML markup to be converted into the output format's
143 # corresponding markup code, e.g. bold or italic.
144 my %markup_replace = ('b' => 'textbf',
148 foreach my $key (keys(%markup_replace)) {
149 my $new = $markup_replace{$key};
150 $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
153 $variable =~ s/[\x00-\x1f]//g;
159 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
161 my ($form, $new_contents) = ($self->{"form"}, "");
163 my $ary = $form->{$var};
164 for (my $i = 0; $i < scalar(@indices); $i++) {
165 last unless (ref($ary) eq "ARRAY");
166 $ary = $ary->[$indices[$i]];
170 my $current_page = 1;
171 my ($current_line, $corrent_row) = (0, 1);
173 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
174 $form->{"__first__"} = $i == 0;
175 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
176 $form->{"__odd__"} = (($i + 1) % 2) == 1;
177 $form->{"__counter__"} = $i + 1;
179 if ((scalar(@{$form->{"description"}}) == scalar(@{$ary})) &&
180 $self->{"chars_per_line"}) {
182 int(length($form->{"description"}->[$i]) / $self->{"chars_per_line"});
185 $form->{"description"}->[$i] =~ s/(\\newline\s?)*$//;
186 my $_description = $form->{"description"}->[$i];
187 while ($_description =~ /\\newline/) {
189 $_description =~ s/\\newline//;
193 if ($current_page == 1) {
194 $lpp = $self->{"lines_on_first_page"};
196 $lpp = $self->{"lines_on_second_page"};
199 # Yes we need a manual page break -- or the user has forced one
200 if ((($current_line + $lines) > $lpp) || ($form->{"description"}->[$i] =~ /<pagebreak>/) || ($form->{"longdescription"}->[$i] =~ /<pagebreak>/)) {
201 my $pb = $self->{"pagebreak_block"};
203 # replace the special variables <%sumcarriedforward%>
206 my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
207 $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
208 $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
210 my $new_text = $self->parse_block($pb, (@indices, $i));
211 return undef unless (defined($new_text));
212 $new_contents .= $new_text;
217 $current_line += $lines;
219 if ($i < scalar(@{$form->{"linetotal"}})) {
220 $sum += $form->parse_amount($self->{"myconfig"},
221 $form->{"linetotal"}->[$i]);
224 $form->{"cumulatelinetotal"}[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
226 my $new_text = $self->parse_block($text, (@indices, $i));
227 return undef unless (defined($new_text));
228 $new_contents .= $start_tag . $new_text . $end_tag;
230 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
232 return $new_contents;
236 my ($self, $text, $pos, $var, $not) = @_;
238 my $tag_start_len = length $self->{tag_start};
241 $pos = 0 unless ($pos);
243 while ($pos < length($text)) {
246 next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
248 my $keyword_pos = $pos - 1 + $tag_start_len;
250 if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 3) eq 'for')) {
253 } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
256 "$self->{tag_start}else$self->{tag_end} outside of "
257 . "$self->{tag_start}if$self->{tag_end} / "
258 . "$self->{tag_start}ifnot$self->{tag_end}.";
262 my $block = substr($text, 0, $pos - 1);
263 substr($text, 0, $pos - 1) = "";
264 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
265 $text = $self->{tag_start} . 'if' . ($not ? " " : "not ") . $var . $self->{tag_end} . $text;
267 return ($block, $text);
269 } elsif (substr($text, $keyword_pos, 3) eq 'end') {
272 my $block = substr($text, 0, $pos - 1);
273 substr($text, 0, $pos - 1) = "";
274 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
276 return ($block, $text);
285 $main::lxdebug->enter_sub();
287 my ($self, $contents, @indices) = @_;
289 my $new_contents = "";
291 while ($contents ne "") {
292 my $pos_if = index($contents, $self->{tag_start} . 'if');
293 my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
295 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
296 $new_contents .= $self->substitute_vars($contents, @indices);
300 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
301 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
302 substr($contents, 0, $pos_foreach) = "";
304 if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
305 $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
306 $main::lxdebug->leave_sub();
312 substr($contents, 0, length($&)) = "";
315 ($block, $contents) = $self->find_end($contents);
317 $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
318 $main::lxdebug->leave_sub();
322 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
323 if (!defined($new_text)) {
324 $main::lxdebug->leave_sub();
327 $new_contents .= $new_text;
330 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
331 substr($contents, 0, $pos_if) = "";
333 if ($contents !~ m|^$self->{tag_start_qm}if\s*(not)?\s+(.*?)$self->{tag_end_qm}|) {
334 $self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}.";
335 $main::lxdebug->leave_sub();
339 my ($not, $var) = ($1, $2);
341 substr($contents, 0, length($&)) = "";
343 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
345 $self->{"error"} = "Unclosed $self->{tag_start}if${not}$self->{tag_end}." unless ($self->{"error"});
346 $main::lxdebug->leave_sub();
350 my $value = $self->{"form"}->{$var};
351 for (my $i = 0; $i < scalar(@indices); $i++) {
352 last unless (ref($value) eq "ARRAY");
353 $value = $value->[$indices[$i]];
356 if (($not && !$value) || (!$not && $value)) {
357 my $new_text = $self->parse_block($block, @indices);
358 if (!defined($new_text)) {
359 $main::lxdebug->leave_sub();
362 $new_contents .= $new_text;
367 $main::lxdebug->leave_sub();
369 return $new_contents;
372 sub parse_first_line {
374 my $line = shift || "";
376 if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
378 $self->{error} = "The tag start and end markers must not be equal.";
382 $self->set_tag_style($1, $2);
388 sub _parse_config_option {
395 my ($key, $value) = split m/\s*=\s*/, $line, 2;
397 if ($key eq 'tag-style') {
398 $self->set_tag_style(split(m/\s+/, $value, 2));
402 sub _parse_config_lines {
406 my ($comment_start, $comment_end) = ("", "");
408 if (ref $self eq 'LaTeXTemplate') {
409 $comment_start = '\s*%';
410 } elsif (ref $self eq 'HTMLTemplate') {
411 $comment_start = '\s*<!--';
412 $comment_end = '>\s*';
414 $comment_start = '\s*\#';
417 my $num_lines = scalar @{ $lines };
420 while ($i < $num_lines) {
421 my $line = $lines->[$i];
423 if ($line !~ m/^${comment_start}\s*config\s*:(.*)${comment_end}$/i) {
428 $self->_parse_config_option($1);
429 splice @{ $lines }, $i, 1;
434 sub _force_mandatory_packages {
438 my (%used_packages, $document_start_line);
440 foreach my $i (0 .. scalar @{ $lines } - 1) {
441 if ($lines->[$i] =~ m/\\usepackage[^{]*{(.*?)}/) {
442 $used_packages{$1} = 1;
444 } elsif ($lines->[$i] =~ m/\\begin{document}/) {
445 $document_start_line = $i;
451 $document_start_line = scalar @{ $lines } - 1 if (!defined $document_start_line);
453 if (!$used_packages{textcomp}) {
454 splice @{ $lines }, $document_start_line, 0, "\\usepackage{textcomp}\n";
455 $document_start_line++;
462 my $form = $self->{"form"};
464 if (!open(IN, "$form->{templates}/$form->{IN}")) {
465 $self->{"error"} = "$!";
471 $self->_parse_config_lines(\@lines);
472 $self->_force_mandatory_packages(\@lines) if (ref $self eq 'LaTeXTemplate');
474 my $contents = join("", @lines);
476 # detect pagebreak block and its parameters
477 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) {
478 $self->{"chars_per_line"} = $1;
479 $self->{"lines_on_first_page"} = $2;
480 $self->{"lines_on_second_page"} = $3;
481 $self->{"pagebreak_block"} = $4;
483 substr($contents, length($`), length($&)) = "";
486 $self->{"forced_pagebreaks"} = [];
488 my $new_contents = $self->parse_block($contents);
489 if (!defined($new_contents)) {
490 $main::lxdebug->leave_sub();
494 print(OUT $new_contents);
496 if ($form->{"format"} =~ /postscript/i) {
497 return $self->convert_to_postscript();
498 } elsif ($form->{"format"} =~ /pdf/i) {
499 return $self->convert_to_pdf();
505 sub convert_to_postscript {
507 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
509 # Convert the tex file to postscript
511 if (!chdir("$userspath")) {
512 $self->{"error"} = "chdir : $!";
517 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
519 for (my $run = 1; $run <= 2; $run++) {
520 system("latex --interaction=nonstopmode $form->{tmpfile} " .
521 "> $form->{tmpfile}.err");
523 $self->{"error"} = $form->cleanup();
529 $form->{tmpfile} =~ s/tex$/dvi/;
531 system("dvips $form->{tmpfile} -o -q > /dev/null");
533 $self->{"error"} = "dvips : $!";
537 $form->{tmpfile} =~ s/dvi$/ps/;
546 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
548 # Convert the tex file to PDF
550 if (!chdir("$userspath")) {
551 $self->{"error"} = "chdir : $!";
556 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
558 for (my $run = 1; $run <= 2; $run++) {
559 system("pdflatex --interaction=nonstopmode $form->{tmpfile} " .
560 "> $form->{tmpfile}.err");
562 $self->{"error"} = $form->cleanup();
568 $form->{tmpfile} =~ s/tex$/pdf/;
573 sub get_mime_type() {
576 if ($self->{"form"}->{"format"} =~ /postscript/i) {
577 return "application/postscript";
579 return "application/pdf";
592 package HTMLTemplate;
596 @ISA = qw(LaTeXTemplate);
601 return $type->SUPER::new(@_);
605 my ($self, $variable) = @_;
606 my $form = $self->{"form"};
608 $variable = $main::locale->quote_special_chars('Template/HTML', $variable);
610 # Allow some HTML markup to be converted into the output format's
611 # corresponding markup code, e.g. bold or italic.
612 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
614 foreach my $key (@markup_replace) {
615 $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
621 sub get_mime_type() {
624 if ($self->{"form"}->{"format"} =~ /postscript/i) {
625 return "application/postscript";
626 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
627 return "application/pdf";
636 if ($self->{"form"}->{"format"} =~ /postscript/i) {
638 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
645 sub convert_to_postscript {
647 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
649 # Convert the HTML file to postscript
651 if (!chdir("$userspath")) {
652 $self->{"error"} = "chdir : $!";
657 $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
658 my $psfile = $form->{"tmpfile"};
659 $psfile =~ s/.html/.ps/;
660 if ($psfile eq $form->{"tmpfile"}) {
664 system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
666 $self->{"error"} = $form->cleanup();
671 $form->{"tmpfile"} = $psfile;
680 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
682 # Convert the HTML file to PDF
684 if (!chdir("$userspath")) {
685 $self->{"error"} = "chdir : $!";
690 $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
691 my $pdffile = $form->{"tmpfile"};
692 $pdffile =~ s/.html/.pdf/;
693 if ($pdffile eq $form->{"tmpfile"}) {
697 system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
699 $self->{"error"} = $form->cleanup();
704 $form->{"tmpfile"} = $pdffile;
713 #### PlainTextTemplate
716 package PlainTextTemplate;
720 @ISA = qw(LaTeXTemplate);
725 return $type->SUPER::new(@_);
729 my ($self, $variable) = @_;
744 #### OpenDocumentTemplate
747 package OpenDocumentTemplate;
755 # use File::Temp qw(:mktemp);
758 @ISA = qw(SimpleTemplate);
763 $self = $type->SUPER::new(@_);
765 foreach my $module (qw(Archive::Zip Text::Iconv)) {
766 eval("use ${module};");
768 $self->{"form"}->error("The Perl module '${module}' could not be " .
769 "loaded. Support for OpenDocument templates " .
770 "does not work without it. Please install your " .
771 "distribution's package or get the module from " .
772 "CPAN ( http://www.cpan.org ).");
776 $self->{"rnd"} = int(rand(1000000));
777 $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
779 $self->set_tag_style('<%', '%>');
785 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
787 my ($form, $new_contents) = ($self->{"form"}, "");
789 my $ary = $form->{$var};
790 for (my $i = 0; $i < scalar(@indices); $i++) {
791 last unless (ref($ary) eq "ARRAY");
792 $ary = $ary->[$indices[$i]];
795 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
796 $form->{"__first__"} = $i == 0;
797 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
798 $form->{"__odd__"} = (($i + 1) % 2) == 1;
799 $form->{"__counter__"} = $i + 1;
800 my $new_text = $self->parse_block($text, (@indices, $i));
801 return undef unless (defined($new_text));
802 $new_contents .= $start_tag . $new_text . $end_tag;
804 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
806 return $new_contents;
810 my ($self, $text, $pos, $var, $not) = @_;
813 $pos = 0 unless ($pos);
815 while ($pos < length($text)) {
818 next if (substr($text, $pos - 1, 5) ne '<%');
820 if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
823 } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
825 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
829 my $block = substr($text, 0, $pos - 1);
830 substr($text, 0, $pos - 1) = "";
831 $text =~ s!^\<\%[^\%]+\%\>!!;
832 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
834 return ($block, $text);
836 } elsif (substr($text, $pos + 4, 3) eq 'end') {
839 my $block = substr($text, 0, $pos - 1);
840 substr($text, 0, $pos - 1) = "";
841 $text =~ s!^\<\%[^\%]+\%\>!!;
843 return ($block, $text);
852 $main::lxdebug->enter_sub();
854 my ($self, $contents, @indices) = @_;
856 my $new_contents = "";
858 while ($contents ne "") {
859 if (substr($contents, 0, 1) eq "<") {
860 $contents =~ m|^<[^>]+>|;
862 substr($contents, 0, length($&)) = "";
864 if ($tag =~ m|<table:table-row|) {
865 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
868 substr($contents, 0, length($1) + length($end_tag)) = "";
870 if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
873 substr($table_row, length($`), length($&)) = "";
875 my ($t1, $t2) = $self->find_end($table_row, length($`));
877 $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
878 $main::lxdebug->leave_sub();
882 my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
883 if (!defined($new_text)) {
884 $main::lxdebug->leave_sub();
887 $new_contents .= $new_text;
890 my $new_text = $self->parse_block($table_row, @indices);
891 if (!defined($new_text)) {
892 $main::lxdebug->leave_sub();
895 $new_contents .= $tag . $new_text . $end_tag;
899 $new_contents .= $tag;
903 $contents =~ /^[^<]+/;
906 my $pos_if = index($text, '<%if');
907 my $pos_foreach = index($text, '<%foreach');
909 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
910 substr($contents, 0, length($text)) = "";
911 $new_contents .= $self->substitute_vars($text, @indices);
915 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
916 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
917 substr($contents, 0, $pos_foreach) = "";
919 if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
920 $self->{"error"} = "Malformed <\%foreach\%>.";
921 $main::lxdebug->leave_sub();
927 substr($contents, 0, length($&)) = "";
930 ($block, $contents) = $self->find_end($contents);
932 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
933 $main::lxdebug->leave_sub();
937 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
938 if (!defined($new_text)) {
939 $main::lxdebug->leave_sub();
942 $new_contents .= $new_text;
945 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
946 substr($contents, 0, $pos_if) = "";
948 if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
949 $self->{"error"} = "Malformed <\%if\%>.";
950 $main::lxdebug->leave_sub();
954 my ($not, $var) = ($1, $2);
956 substr($contents, 0, length($&)) = "";
958 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
960 $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
961 $main::lxdebug->leave_sub();
965 my $value = $self->{"form"}->{$var};
966 for (my $i = 0; $i < scalar(@indices); $i++) {
967 last unless (ref($value) eq "ARRAY");
968 $value = $value->[$indices[$i]];
971 if (($not && !$value) || (!$not && $value)) {
972 my $new_text = $self->parse_block($block, @indices);
973 if (!defined($new_text)) {
974 $main::lxdebug->leave_sub();
977 $new_contents .= $new_text;
983 $main::lxdebug->leave_sub();
985 return $new_contents;
989 $main::lxdebug->enter_sub();
993 my $form = $self->{"form"};
998 if ($form->{"IN"} =~ m|^/|) {
999 $file_name = $form->{"IN"};
1001 $file_name = $form->{"templates"} . "/" . $form->{"IN"};
1004 my $zip = Archive::Zip->new();
1005 if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
1006 $self->{"error"} = "File not found/is not a OpenDocument file.";
1007 $main::lxdebug->leave_sub();
1011 my $contents = $zip->contents("content.xml");
1013 $self->{"error"} = "File is not a OpenDocument file.";
1014 $main::lxdebug->leave_sub();
1018 my $rnd = $self->{"rnd"};
1019 my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
1020 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
1022 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
1023 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
1025 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
1026 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
1028 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
1029 <style:text-properties style:text-line-through-style="solid"/>
1031 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
1032 <style:text-properties style:text-position="super 58%"/>
1034 <style:style style:name="TLXO${rnd}SUB" style:family="text">
1035 <style:text-properties style:text-position="sub 58%"/>
1039 $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
1040 $contents =~ s|[\n\r]||gm;
1042 my $new_contents = $self->parse_block($contents);
1043 if (!defined($new_contents)) {
1044 $main::lxdebug->leave_sub();
1048 # $new_contents =~ s|>|>\n|g;
1050 $zip->contents("content.xml", $new_contents);
1052 my $styles = $zip->contents("styles.xml");
1054 my $new_styles = $self->parse_block($styles);
1055 if (!defined($new_contents)) {
1056 $main::lxdebug->leave_sub();
1059 $zip->contents("styles.xml", $new_styles);
1062 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
1065 if ($form->{"format"} =~ /pdf/) {
1066 $res = $self->convert_to_pdf();
1069 $main::lxdebug->leave_sub();
1073 sub is_xvfb_running {
1074 $main::lxdebug->enter_sub();
1079 my $dfname = $self->{"userspath"} . "/xvfb_display";
1082 $main::lxdebug->message(LXDebug::DEBUG2, " Looking for $dfname\n");
1083 if ((-f $dfname) && open(IN, $dfname)) {
1088 my $xauthority = <IN>;
1092 $main::lxdebug->message(LXDebug::DEBUG2, " found with $pid and $display\n");
1094 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
1095 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #1\n");
1096 unlink($dfname, $xauthority);
1097 $main::lxdebug->leave_sub();
1102 if ($line !~ /xvfb/i) {
1103 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #2\n");
1104 unlink($dfname, $xauthority);
1105 $main::lxdebug->leave_sub();
1109 $ENV{"XAUTHORITY"} = $xauthority;
1110 $ENV{"DISPLAY"} = $display;
1112 $main::lxdebug->message(LXDebug::DEBUG2, " not found\n");
1115 $main::lxdebug->leave_sub();
1121 $main::lxdebug->enter_sub();
1125 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
1127 my $display = $self->is_xvfb_running();
1130 $main::lxdebug->leave_sub();
1135 while ( -f "/tmp/.X${display}-lock") {
1138 $display = ":${display}";
1139 $main::lxdebug->message(LXDebug::DEBUG2, " display $display\n");
1141 my $mcookie = `mcookie`;
1142 die("Installation error: mcookie not found.") if ($? != 0);
1145 $main::lxdebug->message(LXDebug::DEBUG2, " mcookie $mcookie\n");
1147 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
1148 $ENV{"XAUTHORITY"} = $xauthority;
1150 $main::lxdebug->message(LXDebug::DEBUG2, " xauthority $xauthority\n");
1152 system("xauth add \"${display}\" . \"${mcookie}\"");
1154 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
1155 $main::lxdebug->leave_sub();
1159 $main::lxdebug->message(LXDebug::DEBUG2, " about to fork()\n");
1163 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1164 exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
1167 $main::lxdebug->message(LXDebug::DEBUG2, " parent dont sleeping\n");
1170 my $dfname = $self->{"userspath"} . "/xvfb_display";
1171 if (!open(OUT, ">$dfname")) {
1172 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
1173 unlink($xauthority);
1175 $main::lxdebug->leave_sub();
1178 print(OUT "$pid\n$display\n$xauthority\n");
1181 $main::lxdebug->message(LXDebug::DEBUG2, " parent re-testing\n");
1183 if (!$self->is_xvfb_running()) {
1184 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
1185 unlink($xauthority, $dfname);
1187 $main::lxdebug->leave_sub();
1191 $main::lxdebug->message(LXDebug::DEBUG2, " spawn OK\n");
1193 $main::lxdebug->leave_sub();
1198 sub is_openoffice_running {
1199 $main::lxdebug->enter_sub();
1201 system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
1202 "> /dev/null 2> /dev/null");
1204 $main::lxdebug->message(LXDebug::DEBUG2, " is_openoffice_running(): $?\n");
1206 $main::lxdebug->leave_sub();
1211 sub spawn_openoffice {
1212 $main::lxdebug->enter_sub();
1216 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
1218 my ($try, $spawned_oo, $res);
1221 for ($try = 0; $try < 15; $try++) {
1222 if ($self->is_openoffice_running()) {
1230 $main::lxdebug->message(LXDebug::DEBUG2, " Child daemonizing\n");
1232 open(STDIN, '/dev/null');
1233 open(STDOUT, '>/dev/null');
1234 my $new_pid = fork();
1236 my $ssres = setsid();
1237 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1238 my @cmdline = ($main::openofficeorg_writer_bin,
1239 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1241 "-accept=socket,host=localhost,port=" .
1242 $main::openofficeorg_daemon_port . ";urp;");
1246 $main::lxdebug->message(LXDebug::DEBUG2, " Parent after fork\n");
1251 sleep($try >= 5 ? 2 : 1);
1255 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
1256 "OpenOffice could not be started.";
1259 $main::lxdebug->leave_sub();
1264 sub convert_to_pdf {
1265 $main::lxdebug->enter_sub();
1269 my $form = $self->{"form"};
1271 my $filename = $form->{"tmpfile"};
1272 $filename =~ s/.odt$//;
1273 if (substr($filename, 0, 1) ne "/") {
1274 $filename = getcwd() . "/${filename}";
1277 if (substr($self->{"userspath"}, 0, 1) eq "/") {
1278 $ENV{'HOME'} = $self->{"userspath"};
1280 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
1283 if (!$self->spawn_xvfb()) {
1284 $main::lxdebug->leave_sub();
1289 if (!$main::openofficeorg_daemon) {
1290 @cmdline = ($main::openofficeorg_writer_bin,
1291 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1293 "file:${filename}.odt",
1294 "macro://" . (split('/', $filename))[-1] .
1295 "/Standard.Conversion.ConvertSelfToPDF()");
1297 if (!$self->spawn_openoffice()) {
1298 $main::lxdebug->leave_sub();
1302 @cmdline = ("./scripts/oo-uno-convert-pdf.py",
1303 $main::openofficeorg_daemon_port,
1311 $form->{"tmpfile"} =~ s/odt$/pdf/;
1313 unlink($filename . ".odt");
1315 $main::lxdebug->leave_sub();
1320 unlink($filename . ".odt", $filename . ".pdf");
1321 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
1324 $main::lxdebug->leave_sub();
1329 my ($self, $variable) = @_;
1330 my $form = $self->{"form"};
1331 my $iconv = $self->{"iconv"};
1333 $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
1335 # Allow some HTML markup to be converted into the output format's
1336 # corresponding markup code, e.g. bold or italic.
1337 my $rnd = $self->{"rnd"};
1338 my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
1339 "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
1341 foreach my $key (keys(%markup_replace)) {
1342 my $value = $markup_replace{$key};
1343 $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
1344 $variable =~ s|\</${key}\>|</text:span>|gi;
1347 return $iconv->convert($variable);
1350 sub get_mime_type() {
1351 if ($self->{"form"}->{"format"} =~ /pdf/) {
1352 return "application/pdf";
1354 return "application/vnd.oasis.opendocument.text";
1358 sub uses_temp_file {
1363 ##########################################################
1367 ##########################################################
1369 package XMLTemplate;
1373 @ISA = qw(HTMLTemplate);
1376 #evtl auskommentieren
1379 return $type->SUPER::new(@_);
1383 my ($self, $variable) = @_;
1384 my $form = $self->{"form"};
1386 $variable = $main::locale->quote_special_chars('Template/XML', $variable);
1388 # Allow no markup to be converted into the output format
1389 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
1391 foreach my $key (@markup_replace) {
1392 $variable =~ s/\<(\/?)${key}\>//g;
1398 sub get_mime_type() {
1401 if ($self->{"form"}->{"format"} =~ /elsterwinston/i) {
1402 return "application/xml ";
1403 } elsif ($self->{"form"}->{"format"} =~ /elstertaxbird/i) {
1404 return "application/x-taxbird";
1410 sub uses_temp_file {
1411 # tempfile needet for XML Output