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);
178 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
179 $form->{"__first__"} = $i == 0;
180 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
181 $form->{"__odd__"} = (($i + 1) % 2) == 1;
182 $form->{"__counter__"} = $i + 1;
184 if (scalar @{$description_array} == scalar @{$ary} && $self->{"chars_per_line"} != 0) {
185 my $lines = int(length($description_array->[$i]) / $self->{"chars_per_line"});
188 $description_array->[$i] =~ s/(\\newline\s?)*$//;
189 my $_description = $description_array->[$i];
190 while ($_description =~ /\\newline/) {
192 $_description =~ s/\\newline//;
196 if ($current_page == 1) {
197 $lpp = $self->{"lines_on_first_page"};
199 $lpp = $self->{"lines_on_second_page"};
202 # Yes we need a manual page break -- or the user has forced one
203 if ((($current_line + $lines) > $lpp) || ($description_array->[$i] =~ /<pagebreak>/) || ($longdescription_array->[$i] =~ /<pagebreak>/)) {
204 my $pb = $self->{"pagebreak_block"};
206 # replace the special variables <%sumcarriedforward%>
209 my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
210 $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
211 $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
213 my $new_text = $self->parse_block($pb, (@indices, $i));
214 return undef unless (defined($new_text));
215 $new_contents .= $new_text;
220 $current_line += $lines;
222 if ($i < scalar(@{$form->{"linetotal"}})) {
223 $sum += $form->parse_amount($self->{"myconfig"},
224 $form->{"linetotal"}->[$i]);
227 $form->{"cumulatelinetotal"}[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
229 my $new_text = $self->parse_block($text, (@indices, $i));
230 return undef unless (defined($new_text));
231 $new_contents .= $start_tag . $new_text . $end_tag;
233 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
235 return $new_contents;
239 my ($self, $text, $pos, $var, $not) = @_;
241 my $tag_start_len = length $self->{tag_start};
244 $pos = 0 unless ($pos);
246 while ($pos < length($text)) {
249 next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
251 my $keyword_pos = $pos - 1 + $tag_start_len;
253 if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 3) eq 'for')) {
256 } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
259 "$self->{tag_start}else$self->{tag_end} outside of "
260 . "$self->{tag_start}if$self->{tag_end} / "
261 . "$self->{tag_start}ifnot$self->{tag_end}.";
265 my $block = substr($text, 0, $pos - 1);
266 substr($text, 0, $pos - 1) = "";
267 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
268 $text = $self->{tag_start} . 'if' . ($not ? " " : "not ") . $var . $self->{tag_end} . $text;
270 return ($block, $text);
272 } elsif (substr($text, $keyword_pos, 3) eq 'end') {
275 my $block = substr($text, 0, $pos - 1);
276 substr($text, 0, $pos - 1) = "";
277 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
279 return ($block, $text);
288 $main::lxdebug->enter_sub();
290 my ($self, $contents, @indices) = @_;
292 my $new_contents = "";
294 while ($contents ne "") {
295 my $pos_if = index($contents, $self->{tag_start} . 'if');
296 my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
298 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
299 $new_contents .= $self->substitute_vars($contents, @indices);
303 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
304 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
305 substr($contents, 0, $pos_foreach) = "";
307 if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
308 $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
309 $main::lxdebug->leave_sub();
315 substr($contents, 0, length($&)) = "";
318 ($block, $contents) = $self->find_end($contents);
320 $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
321 $main::lxdebug->leave_sub();
325 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
326 if (!defined($new_text)) {
327 $main::lxdebug->leave_sub();
330 $new_contents .= $new_text;
333 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
334 substr($contents, 0, $pos_if) = "";
336 if ($contents !~ m|^$self->{tag_start_qm}if\s*(not)?\s+(.*?)$self->{tag_end_qm}|) {
337 $self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}.";
338 $main::lxdebug->leave_sub();
342 my ($not, $var) = ($1, $2);
344 substr($contents, 0, length($&)) = "";
346 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
348 $self->{"error"} = "Unclosed $self->{tag_start}if${not}$self->{tag_end}." unless ($self->{"error"});
349 $main::lxdebug->leave_sub();
353 my $form = $self->{form};
354 $form = $form->{TEMPLATE_ARRAYS} if @indices
355 && ref $form->{TEMPLATE_ARRAYS} eq 'HASH'
356 && ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY';
357 my $value = $form->{$var};
358 for (my $i = 0; $i < scalar(@indices); $i++) {
359 last unless (ref($value) eq "ARRAY");
360 $value = $value->[$indices[$i]];
363 if (($not && !$value) || (!$not && $value)) {
364 my $new_text = $self->parse_block($block, @indices);
365 if (!defined($new_text)) {
366 $main::lxdebug->leave_sub();
369 $new_contents .= $new_text;
374 $main::lxdebug->leave_sub();
376 return $new_contents;
379 sub parse_first_line {
381 my $line = shift || "";
383 if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
385 $self->{error} = "The tag start and end markers must not be equal.";
389 $self->set_tag_style($1, $2);
395 sub _parse_config_option {
402 my ($key, $value) = split m/\s*=\s*/, $line, 2;
404 if ($key eq 'tag-style') {
405 $self->set_tag_style(split(m/\s+/, $value, 2));
409 sub _parse_config_lines {
413 my ($comment_start, $comment_end) = ("", "");
415 if (ref $self eq 'LaTeXTemplate') {
416 $comment_start = '\s*%';
417 } elsif (ref $self eq 'HTMLTemplate') {
418 $comment_start = '\s*<!--';
419 $comment_end = '>\s*';
421 $comment_start = '\s*\#';
424 my $num_lines = scalar @{ $lines };
427 while ($i < $num_lines) {
428 my $line = $lines->[$i];
430 if ($line !~ m/^${comment_start}\s*config\s*:(.*)${comment_end}$/i) {
435 $self->_parse_config_option($1);
436 splice @{ $lines }, $i, 1;
441 sub _force_mandatory_packages {
445 my (%used_packages, $document_start_line);
447 foreach my $i (0 .. scalar @{ $lines } - 1) {
448 if ($lines->[$i] =~ m/\\usepackage[^{]*{(.*?)}/) {
449 $used_packages{$1} = 1;
451 } elsif ($lines->[$i] =~ m/\\begin{document}/) {
452 $document_start_line = $i;
458 $document_start_line = scalar @{ $lines } - 1 if (!defined $document_start_line);
460 if (!$used_packages{textcomp}) {
461 splice @{ $lines }, $document_start_line, 0, "\\usepackage{textcomp}\n";
462 $document_start_line++;
469 my $form = $self->{"form"};
471 if (!open(IN, "$form->{templates}/$form->{IN}")) {
472 $self->{"error"} = "$!";
478 $self->_parse_config_lines(\@lines);
479 $self->_force_mandatory_packages(\@lines) if (ref $self eq 'LaTeXTemplate');
481 my $contents = join("", @lines);
483 # detect pagebreak block and its parameters
484 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) {
485 $self->{"chars_per_line"} = $1;
486 $self->{"lines_on_first_page"} = $2;
487 $self->{"lines_on_second_page"} = $3;
488 $self->{"pagebreak_block"} = $4;
490 substr($contents, length($`), length($&)) = "";
493 $self->{"forced_pagebreaks"} = [];
495 my $new_contents = $self->parse_block($contents);
496 if (!defined($new_contents)) {
497 $main::lxdebug->leave_sub();
501 print(OUT $new_contents);
503 if ($form->{"format"} =~ /postscript/i) {
504 return $self->convert_to_postscript();
505 } elsif ($form->{"format"} =~ /pdf/i) {
506 return $self->convert_to_pdf();
512 sub convert_to_postscript {
514 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
516 # Convert the tex file to postscript
518 if (!chdir("$userspath")) {
519 $self->{"error"} = "chdir : $!";
524 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
526 my $latex = $self->_get_latex_path();
528 for (my $run = 1; $run <= 2; $run++) {
529 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
530 "> $form->{tmpfile}.err");
532 $self->{"error"} = $form->cleanup();
538 $form->{tmpfile} =~ s/tex$/dvi/;
540 system("dvips $form->{tmpfile} -o -q > /dev/null");
542 $self->{"error"} = "dvips : $!";
546 $form->{tmpfile} =~ s/dvi$/ps/;
555 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
557 # Convert the tex file to PDF
559 if (!chdir("$userspath")) {
560 $self->{"error"} = "chdir : $!";
565 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
567 my $latex = $self->_get_latex_path();
569 for (my $run = 1; $run <= 2; $run++) {
570 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
571 "> $form->{tmpfile}.err");
573 $self->{"error"} = $form->cleanup();
579 $form->{tmpfile} =~ s/tex$/pdf/;
584 sub _get_latex_path {
585 return $main::latex_bin || 'pdflatex';
588 sub get_mime_type() {
591 if ($self->{"form"}->{"format"} =~ /postscript/i) {
592 return "application/postscript";
594 return "application/pdf";
607 package HTMLTemplate;
611 @ISA = qw(LaTeXTemplate);
616 return $type->SUPER::new(@_);
620 my ($self, $variable) = @_;
621 my $form = $self->{"form"};
623 $variable = $main::locale->quote_special_chars('Template/HTML', $variable);
625 # Allow some HTML markup to be converted into the output format's
626 # corresponding markup code, e.g. bold or italic.
627 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
629 foreach my $key (@markup_replace) {
630 $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
636 sub get_mime_type() {
639 if ($self->{"form"}->{"format"} =~ /postscript/i) {
640 return "application/postscript";
641 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
642 return "application/pdf";
651 if ($self->{"form"}->{"format"} =~ /postscript/i) {
653 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
660 sub convert_to_postscript {
662 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
664 # Convert the HTML file to postscript
666 if (!chdir("$userspath")) {
667 $self->{"error"} = "chdir : $!";
672 $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
673 my $psfile = $form->{"tmpfile"};
674 $psfile =~ s/.html/.ps/;
675 if ($psfile eq $form->{"tmpfile"}) {
679 system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
681 $self->{"error"} = $form->cleanup();
686 $form->{"tmpfile"} = $psfile;
695 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
697 # Convert the HTML file to PDF
699 if (!chdir("$userspath")) {
700 $self->{"error"} = "chdir : $!";
705 $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
706 my $pdffile = $form->{"tmpfile"};
707 $pdffile =~ s/.html/.pdf/;
708 if ($pdffile eq $form->{"tmpfile"}) {
712 system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
714 $self->{"error"} = $form->cleanup();
719 $form->{"tmpfile"} = $pdffile;
728 #### PlainTextTemplate
731 package PlainTextTemplate;
735 @ISA = qw(LaTeXTemplate);
740 return $type->SUPER::new(@_);
744 my ($self, $variable) = @_;
759 #### OpenDocumentTemplate
762 package OpenDocumentTemplate;
770 # use File::Temp qw(:mktemp);
773 @ISA = qw(SimpleTemplate);
778 $self = $type->SUPER::new(@_);
780 foreach my $module (qw(Archive::Zip Text::Iconv)) {
781 eval("use ${module};");
783 $self->{"form"}->error("The Perl module '${module}' could not be " .
784 "loaded. Support for OpenDocument templates " .
785 "does not work without it. Please install your " .
786 "distribution's package or get the module from " .
787 "CPAN ( http://www.cpan.org ).");
791 $self->{"rnd"} = int(rand(1000000));
792 $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
794 $self->set_tag_style('<%', '%>');
800 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
802 my ($form, $new_contents) = ($self->{"form"}, "");
804 my $ary = $self->_get_loop_variable($var, 1, @indices);
806 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
807 $form->{"__first__"} = $i == 0;
808 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
809 $form->{"__odd__"} = (($i + 1) % 2) == 1;
810 $form->{"__counter__"} = $i + 1;
811 my $new_text = $self->parse_block($text, (@indices, $i));
812 return undef unless (defined($new_text));
813 $new_contents .= $start_tag . $new_text . $end_tag;
815 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
817 return $new_contents;
821 my ($self, $text, $pos, $var, $not) = @_;
824 $pos = 0 unless ($pos);
826 while ($pos < length($text)) {
829 next if (substr($text, $pos - 1, 5) ne '<%');
831 if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
834 } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
836 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
840 my $block = substr($text, 0, $pos - 1);
841 substr($text, 0, $pos - 1) = "";
842 $text =~ s!^\<\%[^\%]+\%\>!!;
843 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
845 return ($block, $text);
847 } elsif (substr($text, $pos + 4, 3) eq 'end') {
850 my $block = substr($text, 0, $pos - 1);
851 substr($text, 0, $pos - 1) = "";
852 $text =~ s!^\<\%[^\%]+\%\>!!;
854 return ($block, $text);
863 $main::lxdebug->enter_sub();
865 my ($self, $contents, @indices) = @_;
867 my $new_contents = "";
869 while ($contents ne "") {
870 if (substr($contents, 0, 1) eq "<") {
871 $contents =~ m|^<[^>]+>|;
873 substr($contents, 0, length($&)) = "";
875 if ($tag =~ m|<table:table-row|) {
876 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
879 substr($contents, 0, length($1) + length($end_tag)) = "";
881 if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
884 substr($table_row, length($`), length($&)) = "";
886 my ($t1, $t2) = $self->find_end($table_row, length($`));
888 $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
889 $main::lxdebug->leave_sub();
893 my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
894 if (!defined($new_text)) {
895 $main::lxdebug->leave_sub();
898 $new_contents .= $new_text;
901 my $new_text = $self->parse_block($table_row, @indices);
902 if (!defined($new_text)) {
903 $main::lxdebug->leave_sub();
906 $new_contents .= $tag . $new_text . $end_tag;
910 $new_contents .= $tag;
914 $contents =~ /^[^<]+/;
917 my $pos_if = index($text, '<%if');
918 my $pos_foreach = index($text, '<%foreach');
920 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
921 substr($contents, 0, length($text)) = "";
922 $new_contents .= $self->substitute_vars($text, @indices);
926 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
927 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
928 substr($contents, 0, $pos_foreach) = "";
930 if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
931 $self->{"error"} = "Malformed <\%foreach\%>.";
932 $main::lxdebug->leave_sub();
938 substr($contents, 0, length($&)) = "";
941 ($block, $contents) = $self->find_end($contents);
943 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
944 $main::lxdebug->leave_sub();
948 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
949 if (!defined($new_text)) {
950 $main::lxdebug->leave_sub();
953 $new_contents .= $new_text;
956 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
957 substr($contents, 0, $pos_if) = "";
959 if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
960 $self->{"error"} = "Malformed <\%if\%>.";
961 $main::lxdebug->leave_sub();
965 my ($not, $var) = ($1, $2);
967 substr($contents, 0, length($&)) = "";
969 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
971 $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
972 $main::lxdebug->leave_sub();
976 my $value = $self->{"form"}->{$var};
977 for (my $i = 0; $i < scalar(@indices); $i++) {
978 last unless (ref($value) eq "ARRAY");
979 $value = $value->[$indices[$i]];
982 if (($not && !$value) || (!$not && $value)) {
983 my $new_text = $self->parse_block($block, @indices);
984 if (!defined($new_text)) {
985 $main::lxdebug->leave_sub();
988 $new_contents .= $new_text;
994 $main::lxdebug->leave_sub();
996 return $new_contents;
1000 $main::lxdebug->enter_sub();
1004 my $form = $self->{"form"};
1009 if ($form->{"IN"} =~ m|^/|) {
1010 $file_name = $form->{"IN"};
1012 $file_name = $form->{"templates"} . "/" . $form->{"IN"};
1015 my $zip = Archive::Zip->new();
1016 if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
1017 $self->{"error"} = "File not found/is not a OpenDocument file.";
1018 $main::lxdebug->leave_sub();
1022 my $contents = $zip->contents("content.xml");
1024 $self->{"error"} = "File is not a OpenDocument file.";
1025 $main::lxdebug->leave_sub();
1029 my $rnd = $self->{"rnd"};
1030 my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
1031 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
1033 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
1034 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
1036 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
1037 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
1039 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
1040 <style:text-properties style:text-line-through-style="solid"/>
1042 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
1043 <style:text-properties style:text-position="super 58%"/>
1045 <style:style style:name="TLXO${rnd}SUB" style:family="text">
1046 <style:text-properties style:text-position="sub 58%"/>
1050 $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
1051 $contents =~ s|[\n\r]||gm;
1053 my $new_contents = $self->parse_block($contents);
1054 if (!defined($new_contents)) {
1055 $main::lxdebug->leave_sub();
1059 # $new_contents =~ s|>|>\n|g;
1061 $zip->contents("content.xml", $new_contents);
1063 my $styles = $zip->contents("styles.xml");
1065 my $new_styles = $self->parse_block($styles);
1066 if (!defined($new_contents)) {
1067 $main::lxdebug->leave_sub();
1070 $zip->contents("styles.xml", $new_styles);
1073 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
1076 if ($form->{"format"} =~ /pdf/) {
1077 $res = $self->convert_to_pdf();
1080 $main::lxdebug->leave_sub();
1084 sub is_xvfb_running {
1085 $main::lxdebug->enter_sub();
1090 my $dfname = $self->{"userspath"} . "/xvfb_display";
1093 $main::lxdebug->message(LXDebug::DEBUG2, " Looking for $dfname\n");
1094 if ((-f $dfname) && open(IN, $dfname)) {
1099 my $xauthority = <IN>;
1103 $main::lxdebug->message(LXDebug::DEBUG2, " found with $pid and $display\n");
1105 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
1106 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #1\n");
1107 unlink($dfname, $xauthority);
1108 $main::lxdebug->leave_sub();
1113 if ($line !~ /xvfb/i) {
1114 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #2\n");
1115 unlink($dfname, $xauthority);
1116 $main::lxdebug->leave_sub();
1120 $ENV{"XAUTHORITY"} = $xauthority;
1121 $ENV{"DISPLAY"} = $display;
1123 $main::lxdebug->message(LXDebug::DEBUG2, " not found\n");
1126 $main::lxdebug->leave_sub();
1132 $main::lxdebug->enter_sub();
1136 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
1138 my $display = $self->is_xvfb_running();
1141 $main::lxdebug->leave_sub();
1146 while ( -f "/tmp/.X${display}-lock") {
1149 $display = ":${display}";
1150 $main::lxdebug->message(LXDebug::DEBUG2, " display $display\n");
1152 my $mcookie = `mcookie`;
1153 die("Installation error: mcookie not found.") if ($? != 0);
1156 $main::lxdebug->message(LXDebug::DEBUG2, " mcookie $mcookie\n");
1158 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
1159 $ENV{"XAUTHORITY"} = $xauthority;
1161 $main::lxdebug->message(LXDebug::DEBUG2, " xauthority $xauthority\n");
1163 system("xauth add \"${display}\" . \"${mcookie}\"");
1165 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
1166 $main::lxdebug->leave_sub();
1170 $main::lxdebug->message(LXDebug::DEBUG2, " about to fork()\n");
1174 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1175 exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
1178 $main::lxdebug->message(LXDebug::DEBUG2, " parent dont sleeping\n");
1181 my $dfname = $self->{"userspath"} . "/xvfb_display";
1182 if (!open(OUT, ">$dfname")) {
1183 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
1184 unlink($xauthority);
1186 $main::lxdebug->leave_sub();
1189 print(OUT "$pid\n$display\n$xauthority\n");
1192 $main::lxdebug->message(LXDebug::DEBUG2, " parent re-testing\n");
1194 if (!$self->is_xvfb_running()) {
1195 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
1196 unlink($xauthority, $dfname);
1198 $main::lxdebug->leave_sub();
1202 $main::lxdebug->message(LXDebug::DEBUG2, " spawn OK\n");
1204 $main::lxdebug->leave_sub();
1209 sub is_openoffice_running {
1210 $main::lxdebug->enter_sub();
1212 system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
1213 "> /dev/null 2> /dev/null");
1215 $main::lxdebug->message(LXDebug::DEBUG2, " is_openoffice_running(): $?\n");
1217 $main::lxdebug->leave_sub();
1222 sub spawn_openoffice {
1223 $main::lxdebug->enter_sub();
1227 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
1229 my ($try, $spawned_oo, $res);
1232 for ($try = 0; $try < 15; $try++) {
1233 if ($self->is_openoffice_running()) {
1241 $main::lxdebug->message(LXDebug::DEBUG2, " Child daemonizing\n");
1243 open(STDIN, '/dev/null');
1244 open(STDOUT, '>/dev/null');
1245 my $new_pid = fork();
1247 my $ssres = setsid();
1248 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1249 my @cmdline = ($main::openofficeorg_writer_bin,
1250 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1252 "-accept=socket,host=localhost,port=" .
1253 $main::openofficeorg_daemon_port . ";urp;");
1257 $main::lxdebug->message(LXDebug::DEBUG2, " Parent after fork\n");
1262 sleep($try >= 5 ? 2 : 1);
1266 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
1267 "OpenOffice could not be started.";
1270 $main::lxdebug->leave_sub();
1275 sub convert_to_pdf {
1276 $main::lxdebug->enter_sub();
1280 my $form = $self->{"form"};
1282 my $filename = $form->{"tmpfile"};
1283 $filename =~ s/.odt$//;
1284 if (substr($filename, 0, 1) ne "/") {
1285 $filename = getcwd() . "/${filename}";
1288 if (substr($self->{"userspath"}, 0, 1) eq "/") {
1289 $ENV{'HOME'} = $self->{"userspath"};
1291 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
1294 if (!$self->spawn_xvfb()) {
1295 $main::lxdebug->leave_sub();
1300 if (!$main::openofficeorg_daemon) {
1301 @cmdline = ($main::openofficeorg_writer_bin,
1302 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1304 "file:${filename}.odt",
1305 "macro://" . (split('/', $filename))[-1] .
1306 "/Standard.Conversion.ConvertSelfToPDF()");
1308 if (!$self->spawn_openoffice()) {
1309 $main::lxdebug->leave_sub();
1313 @cmdline = ("./scripts/oo-uno-convert-pdf.py",
1314 $main::openofficeorg_daemon_port,
1322 $form->{"tmpfile"} =~ s/odt$/pdf/;
1324 unlink($filename . ".odt");
1326 $main::lxdebug->leave_sub();
1331 unlink($filename . ".odt", $filename . ".pdf");
1332 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
1335 $main::lxdebug->leave_sub();
1340 my ($self, $variable) = @_;
1341 my $form = $self->{"form"};
1342 my $iconv = $self->{"iconv"};
1344 $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
1346 # Allow some HTML markup to be converted into the output format's
1347 # corresponding markup code, e.g. bold or italic.
1348 my $rnd = $self->{"rnd"};
1349 my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
1350 "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
1352 foreach my $key (keys(%markup_replace)) {
1353 my $value = $markup_replace{$key};
1354 $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
1355 $variable =~ s|\</${key}\>|</text:span>|gi;
1358 return $iconv->convert($variable);
1361 sub get_mime_type() {
1362 if ($self->{"form"}->{"format"} =~ /pdf/) {
1363 return "application/pdf";
1365 return "application/vnd.oasis.opendocument.text";
1369 sub uses_temp_file {
1374 ##########################################################
1378 ##########################################################
1380 package XMLTemplate;
1384 @ISA = qw(HTMLTemplate);
1387 #evtl auskommentieren
1390 return $type->SUPER::new(@_);
1394 my ($self, $variable) = @_;
1395 my $form = $self->{"form"};
1397 $variable = $main::locale->quote_special_chars('Template/XML', $variable);
1399 # Allow no markup to be converted into the output format
1400 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
1402 foreach my $key (@markup_replace) {
1403 $variable =~ s/\<(\/?)${key}\>//g;
1409 sub get_mime_type() {
1412 if ($self->{"form"}->{"format"} =~ /elsterwinston/i) {
1413 return "application/xml ";
1414 } elsif ($self->{"form"}->{"format"} =~ /elstertaxbird/i) {
1415 return "application/x-taxbird";
1421 sub uses_temp_file {
1422 # tempfile needet for XML Output