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 {
85 my $get_array = shift;
88 my $form = $self->{form};
91 if (($get_array || @indices) && (ref $form->{TEMPLATE_ARRAYS} eq 'HASH') && (ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY')) {
92 $value = $form->{TEMPLATE_ARRAYS}->{$var};
94 $value = $form->{$var};
97 for (my $i = 0; $i < scalar(@indices); $i++) {
98 last unless (ref($value) eq "ARRAY");
99 $value = $value->[$indices[$i]];
105 sub substitute_vars {
106 my ($self, $text, @indices) = @_;
108 my $form = $self->{"form"};
110 while ($text =~ /$self->{substitute_vars_re}/) {
111 my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]);
112 my ($var, @options) = split(/\s+/, $1);
114 my $value = $self->_get_loop_variable($var, 0, @indices);
115 $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
117 substr($text, $tag_pos, $tag_len, $value);
129 package LaTeXTemplate;
133 @ISA = qw(SimpleTemplate);
138 my $self = $type->SUPER::new(@_);
144 my ($self, $variable) = @_;
145 my $form = $self->{"form"};
147 $variable = $main::locale->quote_special_chars('Template/LaTeX', $variable);
149 # Allow some HTML markup to be converted into the output format's
150 # corresponding markup code, e.g. bold or italic.
151 my %markup_replace = ('b' => 'textbf',
155 foreach my $key (keys(%markup_replace)) {
156 my $new = $markup_replace{$key};
157 $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
160 $variable =~ s/[\x00-\x1f]//g;
166 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
168 my ($form, $new_contents) = ($self->{"form"}, "");
170 my $ary = $self->_get_loop_variable($var, 1, @indices);
173 my $current_page = 1;
174 my ($current_line, $corrent_row) = (0, 1);
175 my $description_array = $self->_get_loop_variable("description", 1);
176 my $longdescription_array = $self->_get_loop_variable("longdescription", 1);
177 my $linetotal_array = $self->_get_loop_variable("linetotal", 1);
179 $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal} = [];
181 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
182 $form->{"__first__"} = $i == 1;
183 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
184 $form->{"__odd__"} = (($i + 1) % 2) == 1;
185 $form->{"__counter__"} = $i + 1;
187 if (scalar @{$description_array} == scalar @{$ary} && $self->{"chars_per_line"} != 0) {
188 my $lines = int(length($description_array->[$i]) / $self->{"chars_per_line"});
191 $description_array->[$i] =~ s/(\\newline\s?)*$//;
192 my $_description = $description_array->[$i];
193 while ($_description =~ /\\newline/) {
195 $_description =~ s/\\newline//;
199 if ($current_page == 1) {
200 $lpp = $self->{"lines_on_first_page"};
202 $lpp = $self->{"lines_on_second_page"};
205 # Yes we need a manual page break -- or the user has forced one
206 if ((($current_line + $lines) > $lpp) || ($description_array->[$i] =~ /<pagebreak>/) || ($longdescription_array->[$i] =~ /<pagebreak>/)) {
207 my $pb = $self->{"pagebreak_block"};
209 # replace the special variables <%sumcarriedforward%>
212 my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
213 $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
214 $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
216 my $new_text = $self->parse_block($pb, (@indices, $i));
217 return undef unless (defined($new_text));
218 $new_contents .= $new_text;
223 $current_line += $lines;
226 if ($i < scalar(@{$linetotal_array})) {
227 $sum += $form->parse_amount($self->{"myconfig"}, $linetotal_array->[$i]);
230 $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal}->[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
232 my $new_text = $self->parse_block($text, (@indices, $i));
233 return undef unless (defined($new_text));
234 $new_contents .= $start_tag . $new_text . $end_tag;
236 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
238 return $new_contents;
242 my ($self, $text, $pos, $var, $not) = @_;
244 my $tag_start_len = length $self->{tag_start};
247 $pos = 0 unless ($pos);
249 while ($pos < length($text)) {
252 next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
254 my $keyword_pos = $pos - 1 + $tag_start_len;
256 if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 3) eq 'for')) {
259 } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
262 "$self->{tag_start}else$self->{tag_end} outside of "
263 . "$self->{tag_start}if$self->{tag_end} / "
264 . "$self->{tag_start}ifnot$self->{tag_end}.";
268 my $block = substr($text, 0, $pos - 1);
269 substr($text, 0, $pos - 1) = "";
270 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
271 $text = $self->{tag_start} . 'if' . ($not ? " " : "not ") . $var . $self->{tag_end} . $text;
273 return ($block, $text);
275 } elsif (substr($text, $keyword_pos, 3) eq 'end') {
278 my $block = substr($text, 0, $pos - 1);
279 substr($text, 0, $pos - 1) = "";
280 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
282 return ($block, $text);
291 $main::lxdebug->enter_sub();
293 my ($self, $contents, @indices) = @_;
295 my $new_contents = "";
297 while ($contents ne "") {
298 my $pos_if = index($contents, $self->{tag_start} . 'if');
299 my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
301 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
302 $new_contents .= $self->substitute_vars($contents, @indices);
306 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
307 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
308 substr($contents, 0, $pos_foreach) = "";
310 if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
311 $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
312 $main::lxdebug->leave_sub();
318 substr($contents, 0, length($&)) = "";
321 ($block, $contents) = $self->find_end($contents);
323 $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
324 $main::lxdebug->leave_sub();
328 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
329 if (!defined($new_text)) {
330 $main::lxdebug->leave_sub();
333 $new_contents .= $new_text;
336 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
337 substr($contents, 0, $pos_if) = "";
339 if ($contents !~ m|^$self->{tag_start_qm}if\s*(not)?\s+(.*?)$self->{tag_end_qm}|) {
340 $self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}.";
341 $main::lxdebug->leave_sub();
345 my ($not, $var) = ($1, $2);
347 substr($contents, 0, length($&)) = "";
349 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
351 $self->{"error"} = "Unclosed $self->{tag_start}if${not}$self->{tag_end}." unless ($self->{"error"});
352 $main::lxdebug->leave_sub();
356 my $form = $self->{form};
357 $form = $form->{TEMPLATE_ARRAYS} if @indices
358 && ref $form->{TEMPLATE_ARRAYS} eq 'HASH'
359 && ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY';
360 my $value = $form->{$var};
361 for (my $i = 0; $i < scalar(@indices); $i++) {
362 last unless (ref($value) eq "ARRAY");
363 $value = $value->[$indices[$i]];
366 if (($not && !$value) || (!$not && $value)) {
367 my $new_text = $self->parse_block($block, @indices);
368 if (!defined($new_text)) {
369 $main::lxdebug->leave_sub();
372 $new_contents .= $new_text;
377 $main::lxdebug->leave_sub();
379 return $new_contents;
382 sub parse_first_line {
384 my $line = shift || "";
386 if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
388 $self->{error} = "The tag start and end markers must not be equal.";
392 $self->set_tag_style($1, $2);
398 sub _parse_config_option {
405 my ($key, $value) = split m/\s*=\s*/, $line, 2;
407 if ($key eq 'tag-style') {
408 $self->set_tag_style(split(m/\s+/, $value, 2));
412 sub _parse_config_lines {
416 my ($comment_start, $comment_end) = ("", "");
418 if (ref $self eq 'LaTeXTemplate') {
419 $comment_start = '\s*%';
420 } elsif (ref $self eq 'HTMLTemplate') {
421 $comment_start = '\s*<!--';
422 $comment_end = '>\s*';
424 $comment_start = '\s*\#';
427 my $num_lines = scalar @{ $lines };
430 while ($i < $num_lines) {
431 my $line = $lines->[$i];
433 if ($line !~ m/^${comment_start}\s*config\s*:(.*)${comment_end}$/i) {
438 $self->_parse_config_option($1);
439 splice @{ $lines }, $i, 1;
444 sub _force_mandatory_packages {
448 my (%used_packages, $document_start_line);
450 foreach my $i (0 .. scalar @{ $lines } - 1) {
451 if ($lines->[$i] =~ m/\\usepackage[^{]*{(.*?)}/) {
452 $used_packages{$1} = 1;
454 } elsif ($lines->[$i] =~ m/\\begin{document}/) {
455 $document_start_line = $i;
461 $document_start_line = scalar @{ $lines } - 1 if (!defined $document_start_line);
463 if (!$used_packages{textcomp}) {
464 splice @{ $lines }, $document_start_line, 0, "\\usepackage{textcomp}\n";
465 $document_start_line++;
472 my $form = $self->{"form"};
474 if (!open(IN, "$form->{templates}/$form->{IN}")) {
475 $self->{"error"} = "$!";
481 $self->_parse_config_lines(\@lines);
482 $self->_force_mandatory_packages(\@lines) if (ref $self eq 'LaTeXTemplate');
484 my $contents = join("", @lines);
486 # detect pagebreak block and its parameters
487 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) {
488 $self->{"chars_per_line"} = $1;
489 $self->{"lines_on_first_page"} = $2;
490 $self->{"lines_on_second_page"} = $3;
491 $self->{"pagebreak_block"} = $4;
493 substr($contents, length($`), length($&)) = "";
496 $self->{"forced_pagebreaks"} = [];
498 my $new_contents = $self->parse_block($contents);
499 if (!defined($new_contents)) {
500 $main::lxdebug->leave_sub();
504 print(OUT $new_contents);
506 if ($form->{"format"} =~ /postscript/i) {
507 return $self->convert_to_postscript();
508 } elsif ($form->{"format"} =~ /pdf/i) {
509 return $self->convert_to_pdf();
515 sub convert_to_postscript {
517 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
519 # Convert the tex file to postscript
521 if (!chdir("$userspath")) {
522 $self->{"error"} = "chdir : $!";
527 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
529 my $latex = $self->_get_latex_path();
531 for (my $run = 1; $run <= 2; $run++) {
532 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
533 "> $form->{tmpfile}.err");
535 $self->{"error"} = $form->cleanup();
541 $form->{tmpfile} =~ s/tex$/dvi/;
543 system("dvips $form->{tmpfile} -o -q > /dev/null");
545 $self->{"error"} = "dvips : $!";
549 $form->{tmpfile} =~ s/dvi$/ps/;
558 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
560 # Convert the tex file to PDF
562 if (!chdir("$userspath")) {
563 $self->{"error"} = "chdir : $!";
568 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
570 my $latex = $self->_get_latex_path();
572 for (my $run = 1; $run <= 2; $run++) {
573 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
574 "> $form->{tmpfile}.err");
576 $self->{"error"} = $form->cleanup();
582 $form->{tmpfile} =~ s/tex$/pdf/;
587 sub _get_latex_path {
588 return $main::latex_bin || 'pdflatex';
591 sub get_mime_type() {
594 if ($self->{"form"}->{"format"} =~ /postscript/i) {
595 return "application/postscript";
597 return "application/pdf";
610 package HTMLTemplate;
614 @ISA = qw(LaTeXTemplate);
619 return $type->SUPER::new(@_);
623 my ($self, $variable) = @_;
624 my $form = $self->{"form"};
626 $variable = $main::locale->quote_special_chars('Template/HTML', $variable);
628 # Allow some HTML markup to be converted into the output format's
629 # corresponding markup code, e.g. bold or italic.
630 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
632 foreach my $key (@markup_replace) {
633 $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
639 sub get_mime_type() {
642 if ($self->{"form"}->{"format"} =~ /postscript/i) {
643 return "application/postscript";
644 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
645 return "application/pdf";
654 if ($self->{"form"}->{"format"} =~ /postscript/i) {
656 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
663 sub convert_to_postscript {
665 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
667 # Convert the HTML file to postscript
669 if (!chdir("$userspath")) {
670 $self->{"error"} = "chdir : $!";
675 $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
676 my $psfile = $form->{"tmpfile"};
677 $psfile =~ s/.html/.ps/;
678 if ($psfile eq $form->{"tmpfile"}) {
682 system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
684 $self->{"error"} = $form->cleanup();
689 $form->{"tmpfile"} = $psfile;
698 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
700 # Convert the HTML file to PDF
702 if (!chdir("$userspath")) {
703 $self->{"error"} = "chdir : $!";
708 $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
709 my $pdffile = $form->{"tmpfile"};
710 $pdffile =~ s/.html/.pdf/;
711 if ($pdffile eq $form->{"tmpfile"}) {
715 system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
717 $self->{"error"} = $form->cleanup();
722 $form->{"tmpfile"} = $pdffile;
731 #### PlainTextTemplate
734 package PlainTextTemplate;
738 @ISA = qw(LaTeXTemplate);
743 return $type->SUPER::new(@_);
747 my ($self, $variable) = @_;
762 #### OpenDocumentTemplate
765 package OpenDocumentTemplate;
773 # use File::Temp qw(:mktemp);
776 @ISA = qw(SimpleTemplate);
781 $self = $type->SUPER::new(@_);
783 foreach my $module (qw(Archive::Zip Text::Iconv)) {
784 eval("use ${module};");
786 $self->{"form"}->error("The Perl module '${module}' could not be " .
787 "loaded. Support for OpenDocument templates " .
788 "does not work without it. Please install your " .
789 "distribution's package or get the module from " .
790 "CPAN ( http://www.cpan.org ).");
794 $self->{"rnd"} = int(rand(1000000));
795 $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
797 $self->set_tag_style('<%', '%>');
803 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
805 my ($form, $new_contents) = ($self->{"form"}, "");
807 my $ary = $self->_get_loop_variable($var, 1, @indices);
809 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
810 $form->{"__first__"} = $i == 0;
811 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
812 $form->{"__odd__"} = (($i + 1) % 2) == 1;
813 $form->{"__counter__"} = $i + 1;
814 my $new_text = $self->parse_block($text, (@indices, $i));
815 return undef unless (defined($new_text));
816 $new_contents .= $start_tag . $new_text . $end_tag;
818 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
820 return $new_contents;
824 my ($self, $text, $pos, $var, $not) = @_;
827 $pos = 0 unless ($pos);
829 while ($pos < length($text)) {
832 next if (substr($text, $pos - 1, 5) ne '<%');
834 if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
837 } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
839 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
843 my $block = substr($text, 0, $pos - 1);
844 substr($text, 0, $pos - 1) = "";
845 $text =~ s!^\<\%[^\%]+\%\>!!;
846 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
848 return ($block, $text);
850 } elsif (substr($text, $pos + 4, 3) eq 'end') {
853 my $block = substr($text, 0, $pos - 1);
854 substr($text, 0, $pos - 1) = "";
855 $text =~ s!^\<\%[^\%]+\%\>!!;
857 return ($block, $text);
866 $main::lxdebug->enter_sub();
868 my ($self, $contents, @indices) = @_;
870 my $new_contents = "";
872 while ($contents ne "") {
873 if (substr($contents, 0, 1) eq "<") {
874 $contents =~ m|^<[^>]+>|;
876 substr($contents, 0, length($&)) = "";
878 if ($tag =~ m|<table:table-row|) {
879 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
882 substr($contents, 0, length($1) + length($end_tag)) = "";
884 if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
887 substr($table_row, length($`), length($&)) = "";
889 my ($t1, $t2) = $self->find_end($table_row, length($`));
891 $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
892 $main::lxdebug->leave_sub();
896 my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
897 if (!defined($new_text)) {
898 $main::lxdebug->leave_sub();
901 $new_contents .= $new_text;
904 my $new_text = $self->parse_block($table_row, @indices);
905 if (!defined($new_text)) {
906 $main::lxdebug->leave_sub();
909 $new_contents .= $tag . $new_text . $end_tag;
913 $new_contents .= $tag;
917 $contents =~ /^[^<]+/;
920 my $pos_if = index($text, '<%if');
921 my $pos_foreach = index($text, '<%foreach');
923 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
924 substr($contents, 0, length($text)) = "";
925 $new_contents .= $self->substitute_vars($text, @indices);
929 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
930 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
931 substr($contents, 0, $pos_foreach) = "";
933 if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
934 $self->{"error"} = "Malformed <\%foreach\%>.";
935 $main::lxdebug->leave_sub();
941 substr($contents, 0, length($&)) = "";
944 ($block, $contents) = $self->find_end($contents);
946 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
947 $main::lxdebug->leave_sub();
951 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
952 if (!defined($new_text)) {
953 $main::lxdebug->leave_sub();
956 $new_contents .= $new_text;
959 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
960 substr($contents, 0, $pos_if) = "";
962 if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
963 $self->{"error"} = "Malformed <\%if\%>.";
964 $main::lxdebug->leave_sub();
968 my ($not, $var) = ($1, $2);
970 substr($contents, 0, length($&)) = "";
972 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
974 $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
975 $main::lxdebug->leave_sub();
979 my $value = $self->{"form"}->{$var};
980 for (my $i = 0; $i < scalar(@indices); $i++) {
981 last unless (ref($value) eq "ARRAY");
982 $value = $value->[$indices[$i]];
985 if (($not && !$value) || (!$not && $value)) {
986 my $new_text = $self->parse_block($block, @indices);
987 if (!defined($new_text)) {
988 $main::lxdebug->leave_sub();
991 $new_contents .= $new_text;
997 $main::lxdebug->leave_sub();
999 return $new_contents;
1003 $main::lxdebug->enter_sub();
1007 my $form = $self->{"form"};
1012 if ($form->{"IN"} =~ m|^/|) {
1013 $file_name = $form->{"IN"};
1015 $file_name = $form->{"templates"} . "/" . $form->{"IN"};
1018 my $zip = Archive::Zip->new();
1019 if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
1020 $self->{"error"} = "File not found/is not a OpenDocument file.";
1021 $main::lxdebug->leave_sub();
1025 my $contents = $zip->contents("content.xml");
1027 $self->{"error"} = "File is not a OpenDocument file.";
1028 $main::lxdebug->leave_sub();
1032 my $rnd = $self->{"rnd"};
1033 my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
1034 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
1036 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
1037 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
1039 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
1040 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
1042 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
1043 <style:text-properties style:text-line-through-style="solid"/>
1045 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
1046 <style:text-properties style:text-position="super 58%"/>
1048 <style:style style:name="TLXO${rnd}SUB" style:family="text">
1049 <style:text-properties style:text-position="sub 58%"/>
1053 $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
1054 $contents =~ s|[\n\r]||gm;
1056 my $new_contents = $self->parse_block($contents);
1057 if (!defined($new_contents)) {
1058 $main::lxdebug->leave_sub();
1062 # $new_contents =~ s|>|>\n|g;
1064 $zip->contents("content.xml", $new_contents);
1066 my $styles = $zip->contents("styles.xml");
1068 my $new_styles = $self->parse_block($styles);
1069 if (!defined($new_contents)) {
1070 $main::lxdebug->leave_sub();
1073 $zip->contents("styles.xml", $new_styles);
1076 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
1079 if ($form->{"format"} =~ /pdf/) {
1080 $res = $self->convert_to_pdf();
1083 $main::lxdebug->leave_sub();
1087 sub is_xvfb_running {
1088 $main::lxdebug->enter_sub();
1093 my $dfname = $self->{"userspath"} . "/xvfb_display";
1096 $main::lxdebug->message(LXDebug::DEBUG2, " Looking for $dfname\n");
1097 if ((-f $dfname) && open(IN, $dfname)) {
1102 my $xauthority = <IN>;
1106 $main::lxdebug->message(LXDebug::DEBUG2, " found with $pid and $display\n");
1108 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
1109 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #1\n");
1110 unlink($dfname, $xauthority);
1111 $main::lxdebug->leave_sub();
1116 if ($line !~ /xvfb/i) {
1117 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #2\n");
1118 unlink($dfname, $xauthority);
1119 $main::lxdebug->leave_sub();
1123 $ENV{"XAUTHORITY"} = $xauthority;
1124 $ENV{"DISPLAY"} = $display;
1126 $main::lxdebug->message(LXDebug::DEBUG2, " not found\n");
1129 $main::lxdebug->leave_sub();
1135 $main::lxdebug->enter_sub();
1139 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
1141 my $display = $self->is_xvfb_running();
1144 $main::lxdebug->leave_sub();
1149 while ( -f "/tmp/.X${display}-lock") {
1152 $display = ":${display}";
1153 $main::lxdebug->message(LXDebug::DEBUG2, " display $display\n");
1155 my $mcookie = `mcookie`;
1156 die("Installation error: mcookie not found.") if ($? != 0);
1159 $main::lxdebug->message(LXDebug::DEBUG2, " mcookie $mcookie\n");
1161 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
1162 $ENV{"XAUTHORITY"} = $xauthority;
1164 $main::lxdebug->message(LXDebug::DEBUG2, " xauthority $xauthority\n");
1166 system("xauth add \"${display}\" . \"${mcookie}\"");
1168 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
1169 $main::lxdebug->leave_sub();
1173 $main::lxdebug->message(LXDebug::DEBUG2, " about to fork()\n");
1177 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1178 exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
1181 $main::lxdebug->message(LXDebug::DEBUG2, " parent dont sleeping\n");
1184 my $dfname = $self->{"userspath"} . "/xvfb_display";
1185 if (!open(OUT, ">$dfname")) {
1186 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
1187 unlink($xauthority);
1189 $main::lxdebug->leave_sub();
1192 print(OUT "$pid\n$display\n$xauthority\n");
1195 $main::lxdebug->message(LXDebug::DEBUG2, " parent re-testing\n");
1197 if (!$self->is_xvfb_running()) {
1198 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
1199 unlink($xauthority, $dfname);
1201 $main::lxdebug->leave_sub();
1205 $main::lxdebug->message(LXDebug::DEBUG2, " spawn OK\n");
1207 $main::lxdebug->leave_sub();
1212 sub is_openoffice_running {
1213 $main::lxdebug->enter_sub();
1215 system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
1216 "> /dev/null 2> /dev/null");
1218 $main::lxdebug->message(LXDebug::DEBUG2, " is_openoffice_running(): $?\n");
1220 $main::lxdebug->leave_sub();
1225 sub spawn_openoffice {
1226 $main::lxdebug->enter_sub();
1230 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
1232 my ($try, $spawned_oo, $res);
1235 for ($try = 0; $try < 15; $try++) {
1236 if ($self->is_openoffice_running()) {
1244 $main::lxdebug->message(LXDebug::DEBUG2, " Child daemonizing\n");
1246 open(STDIN, '/dev/null');
1247 open(STDOUT, '>/dev/null');
1248 my $new_pid = fork();
1250 my $ssres = setsid();
1251 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1252 my @cmdline = ($main::openofficeorg_writer_bin,
1253 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1255 "-accept=socket,host=localhost,port=" .
1256 $main::openofficeorg_daemon_port . ";urp;");
1260 $main::lxdebug->message(LXDebug::DEBUG2, " Parent after fork\n");
1265 sleep($try >= 5 ? 2 : 1);
1269 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
1270 "OpenOffice could not be started.";
1273 $main::lxdebug->leave_sub();
1278 sub convert_to_pdf {
1279 $main::lxdebug->enter_sub();
1283 my $form = $self->{"form"};
1285 my $filename = $form->{"tmpfile"};
1286 $filename =~ s/.odt$//;
1287 if (substr($filename, 0, 1) ne "/") {
1288 $filename = getcwd() . "/${filename}";
1291 if (substr($self->{"userspath"}, 0, 1) eq "/") {
1292 $ENV{'HOME'} = $self->{"userspath"};
1294 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
1297 if (!$self->spawn_xvfb()) {
1298 $main::lxdebug->leave_sub();
1303 if (!$main::openofficeorg_daemon) {
1304 @cmdline = ($main::openofficeorg_writer_bin,
1305 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1307 "file:${filename}.odt",
1308 "macro://" . (split('/', $filename))[-1] .
1309 "/Standard.Conversion.ConvertSelfToPDF()");
1311 if (!$self->spawn_openoffice()) {
1312 $main::lxdebug->leave_sub();
1316 @cmdline = ("./scripts/oo-uno-convert-pdf.py",
1317 $main::openofficeorg_daemon_port,
1325 $form->{"tmpfile"} =~ s/odt$/pdf/;
1327 unlink($filename . ".odt");
1329 $main::lxdebug->leave_sub();
1334 unlink($filename . ".odt", $filename . ".pdf");
1335 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
1338 $main::lxdebug->leave_sub();
1343 my ($self, $variable) = @_;
1344 my $form = $self->{"form"};
1345 my $iconv = $self->{"iconv"};
1347 $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
1349 # Allow some HTML markup to be converted into the output format's
1350 # corresponding markup code, e.g. bold or italic.
1351 my $rnd = $self->{"rnd"};
1352 my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
1353 "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
1355 foreach my $key (keys(%markup_replace)) {
1356 my $value = $markup_replace{$key};
1357 $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
1358 $variable =~ s|\</${key}\>|</text:span>|gi;
1361 return $iconv->convert($variable);
1364 sub get_mime_type() {
1365 if ($self->{"form"}->{"format"} =~ /pdf/) {
1366 return "application/pdf";
1368 return "application/vnd.oasis.opendocument.text";
1372 sub uses_temp_file {
1377 ##########################################################
1381 ##########################################################
1383 package XMLTemplate;
1387 @ISA = qw(HTMLTemplate);
1390 #evtl auskommentieren
1393 return $type->SUPER::new(@_);
1397 my ($self, $variable) = @_;
1398 my $form = $self->{"form"};
1400 $variable = $main::locale->quote_special_chars('Template/XML', $variable);
1402 # Allow no markup to be converted into the output format
1403 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
1405 foreach my $key (@markup_replace) {
1406 $variable =~ s/\<(\/?)${key}\>//g;
1412 sub get_mime_type() {
1415 if ($self->{"form"}->{"format"} =~ /elsterwinston/i) {
1416 return "application/xml ";
1417 } elsif ($self->{"form"}->{"format"} =~ /elstertaxbird/i) {
1418 return "application/x-taxbird";
1424 sub uses_temp_file {
1425 # tempfile needet for XML Output