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);
176 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
177 $form->{"__first__"} = $i == 0;
178 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
179 $form->{"__odd__"} = (($i + 1) % 2) == 1;
180 $form->{"__counter__"} = $i + 1;
182 if ((scalar(@{$form->{"description"}}) == scalar(@{$ary})) &&
183 $self->{"chars_per_line"}) {
185 int(length($form->{"description"}->[$i]) / $self->{"chars_per_line"});
188 $form->{"description"}->[$i] =~ s/(\\newline\s?)*$//;
189 my $_description = $form->{"description"}->[$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) || ($form->{"description"}->[$i] =~ /<pagebreak>/) || ($form->{"longdescription"}->[$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 for (my $run = 1; $run <= 2; $run++) {
527 system("latex --interaction=nonstopmode $form->{tmpfile} " .
528 "> $form->{tmpfile}.err");
530 $self->{"error"} = $form->cleanup();
536 $form->{tmpfile} =~ s/tex$/dvi/;
538 system("dvips $form->{tmpfile} -o -q > /dev/null");
540 $self->{"error"} = "dvips : $!";
544 $form->{tmpfile} =~ s/dvi$/ps/;
553 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
555 # Convert the tex file to PDF
557 if (!chdir("$userspath")) {
558 $self->{"error"} = "chdir : $!";
563 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
565 for (my $run = 1; $run <= 2; $run++) {
566 system("pdflatex --interaction=nonstopmode $form->{tmpfile} " .
567 "> $form->{tmpfile}.err");
569 $self->{"error"} = $form->cleanup();
575 $form->{tmpfile} =~ s/tex$/pdf/;
580 sub get_mime_type() {
583 if ($self->{"form"}->{"format"} =~ /postscript/i) {
584 return "application/postscript";
586 return "application/pdf";
599 package HTMLTemplate;
603 @ISA = qw(LaTeXTemplate);
608 return $type->SUPER::new(@_);
612 my ($self, $variable) = @_;
613 my $form = $self->{"form"};
615 $variable = $main::locale->quote_special_chars('Template/HTML', $variable);
617 # Allow some HTML markup to be converted into the output format's
618 # corresponding markup code, e.g. bold or italic.
619 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
621 foreach my $key (@markup_replace) {
622 $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
628 sub get_mime_type() {
631 if ($self->{"form"}->{"format"} =~ /postscript/i) {
632 return "application/postscript";
633 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
634 return "application/pdf";
643 if ($self->{"form"}->{"format"} =~ /postscript/i) {
645 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
652 sub convert_to_postscript {
654 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
656 # Convert the HTML file to postscript
658 if (!chdir("$userspath")) {
659 $self->{"error"} = "chdir : $!";
664 $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
665 my $psfile = $form->{"tmpfile"};
666 $psfile =~ s/.html/.ps/;
667 if ($psfile eq $form->{"tmpfile"}) {
671 system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
673 $self->{"error"} = $form->cleanup();
678 $form->{"tmpfile"} = $psfile;
687 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
689 # Convert the HTML file to PDF
691 if (!chdir("$userspath")) {
692 $self->{"error"} = "chdir : $!";
697 $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
698 my $pdffile = $form->{"tmpfile"};
699 $pdffile =~ s/.html/.pdf/;
700 if ($pdffile eq $form->{"tmpfile"}) {
704 system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
706 $self->{"error"} = $form->cleanup();
711 $form->{"tmpfile"} = $pdffile;
720 #### PlainTextTemplate
723 package PlainTextTemplate;
727 @ISA = qw(LaTeXTemplate);
732 return $type->SUPER::new(@_);
736 my ($self, $variable) = @_;
751 #### OpenDocumentTemplate
754 package OpenDocumentTemplate;
762 # use File::Temp qw(:mktemp);
765 @ISA = qw(SimpleTemplate);
770 $self = $type->SUPER::new(@_);
772 foreach my $module (qw(Archive::Zip Text::Iconv)) {
773 eval("use ${module};");
775 $self->{"form"}->error("The Perl module '${module}' could not be " .
776 "loaded. Support for OpenDocument templates " .
777 "does not work without it. Please install your " .
778 "distribution's package or get the module from " .
779 "CPAN ( http://www.cpan.org ).");
783 $self->{"rnd"} = int(rand(1000000));
784 $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
786 $self->set_tag_style('<%', '%>');
792 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
794 my ($form, $new_contents) = ($self->{"form"}, "");
796 my $ary = $self->_get_loop_variable($var, 1, @indices);
798 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
799 $form->{"__first__"} = $i == 0;
800 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
801 $form->{"__odd__"} = (($i + 1) % 2) == 1;
802 $form->{"__counter__"} = $i + 1;
803 my $new_text = $self->parse_block($text, (@indices, $i));
804 return undef unless (defined($new_text));
805 $new_contents .= $start_tag . $new_text . $end_tag;
807 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
809 return $new_contents;
813 my ($self, $text, $pos, $var, $not) = @_;
816 $pos = 0 unless ($pos);
818 while ($pos < length($text)) {
821 next if (substr($text, $pos - 1, 5) ne '<%');
823 if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
826 } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
828 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
832 my $block = substr($text, 0, $pos - 1);
833 substr($text, 0, $pos - 1) = "";
834 $text =~ s!^\<\%[^\%]+\%\>!!;
835 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
837 return ($block, $text);
839 } elsif (substr($text, $pos + 4, 3) eq 'end') {
842 my $block = substr($text, 0, $pos - 1);
843 substr($text, 0, $pos - 1) = "";
844 $text =~ s!^\<\%[^\%]+\%\>!!;
846 return ($block, $text);
855 $main::lxdebug->enter_sub();
857 my ($self, $contents, @indices) = @_;
859 my $new_contents = "";
861 while ($contents ne "") {
862 if (substr($contents, 0, 1) eq "<") {
863 $contents =~ m|^<[^>]+>|;
865 substr($contents, 0, length($&)) = "";
867 if ($tag =~ m|<table:table-row|) {
868 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
871 substr($contents, 0, length($1) + length($end_tag)) = "";
873 if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
876 substr($table_row, length($`), length($&)) = "";
878 my ($t1, $t2) = $self->find_end($table_row, length($`));
880 $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
881 $main::lxdebug->leave_sub();
885 my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
886 if (!defined($new_text)) {
887 $main::lxdebug->leave_sub();
890 $new_contents .= $new_text;
893 my $new_text = $self->parse_block($table_row, @indices);
894 if (!defined($new_text)) {
895 $main::lxdebug->leave_sub();
898 $new_contents .= $tag . $new_text . $end_tag;
902 $new_contents .= $tag;
906 $contents =~ /^[^<]+/;
909 my $pos_if = index($text, '<%if');
910 my $pos_foreach = index($text, '<%foreach');
912 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
913 substr($contents, 0, length($text)) = "";
914 $new_contents .= $self->substitute_vars($text, @indices);
918 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
919 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
920 substr($contents, 0, $pos_foreach) = "";
922 if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
923 $self->{"error"} = "Malformed <\%foreach\%>.";
924 $main::lxdebug->leave_sub();
930 substr($contents, 0, length($&)) = "";
933 ($block, $contents) = $self->find_end($contents);
935 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
936 $main::lxdebug->leave_sub();
940 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
941 if (!defined($new_text)) {
942 $main::lxdebug->leave_sub();
945 $new_contents .= $new_text;
948 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
949 substr($contents, 0, $pos_if) = "";
951 if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
952 $self->{"error"} = "Malformed <\%if\%>.";
953 $main::lxdebug->leave_sub();
957 my ($not, $var) = ($1, $2);
959 substr($contents, 0, length($&)) = "";
961 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
963 $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
964 $main::lxdebug->leave_sub();
968 my $value = $self->{"form"}->{$var};
969 for (my $i = 0; $i < scalar(@indices); $i++) {
970 last unless (ref($value) eq "ARRAY");
971 $value = $value->[$indices[$i]];
974 if (($not && !$value) || (!$not && $value)) {
975 my $new_text = $self->parse_block($block, @indices);
976 if (!defined($new_text)) {
977 $main::lxdebug->leave_sub();
980 $new_contents .= $new_text;
986 $main::lxdebug->leave_sub();
988 return $new_contents;
992 $main::lxdebug->enter_sub();
996 my $form = $self->{"form"};
1001 if ($form->{"IN"} =~ m|^/|) {
1002 $file_name = $form->{"IN"};
1004 $file_name = $form->{"templates"} . "/" . $form->{"IN"};
1007 my $zip = Archive::Zip->new();
1008 if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
1009 $self->{"error"} = "File not found/is not a OpenDocument file.";
1010 $main::lxdebug->leave_sub();
1014 my $contents = $zip->contents("content.xml");
1016 $self->{"error"} = "File is not a OpenDocument file.";
1017 $main::lxdebug->leave_sub();
1021 my $rnd = $self->{"rnd"};
1022 my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
1023 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
1025 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
1026 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
1028 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
1029 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
1031 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
1032 <style:text-properties style:text-line-through-style="solid"/>
1034 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
1035 <style:text-properties style:text-position="super 58%"/>
1037 <style:style style:name="TLXO${rnd}SUB" style:family="text">
1038 <style:text-properties style:text-position="sub 58%"/>
1042 $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
1043 $contents =~ s|[\n\r]||gm;
1045 my $new_contents = $self->parse_block($contents);
1046 if (!defined($new_contents)) {
1047 $main::lxdebug->leave_sub();
1051 # $new_contents =~ s|>|>\n|g;
1053 $zip->contents("content.xml", $new_contents);
1055 my $styles = $zip->contents("styles.xml");
1057 my $new_styles = $self->parse_block($styles);
1058 if (!defined($new_contents)) {
1059 $main::lxdebug->leave_sub();
1062 $zip->contents("styles.xml", $new_styles);
1065 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
1068 if ($form->{"format"} =~ /pdf/) {
1069 $res = $self->convert_to_pdf();
1072 $main::lxdebug->leave_sub();
1076 sub is_xvfb_running {
1077 $main::lxdebug->enter_sub();
1082 my $dfname = $self->{"userspath"} . "/xvfb_display";
1085 $main::lxdebug->message(LXDebug::DEBUG2, " Looking for $dfname\n");
1086 if ((-f $dfname) && open(IN, $dfname)) {
1091 my $xauthority = <IN>;
1095 $main::lxdebug->message(LXDebug::DEBUG2, " found with $pid and $display\n");
1097 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
1098 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #1\n");
1099 unlink($dfname, $xauthority);
1100 $main::lxdebug->leave_sub();
1105 if ($line !~ /xvfb/i) {
1106 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #2\n");
1107 unlink($dfname, $xauthority);
1108 $main::lxdebug->leave_sub();
1112 $ENV{"XAUTHORITY"} = $xauthority;
1113 $ENV{"DISPLAY"} = $display;
1115 $main::lxdebug->message(LXDebug::DEBUG2, " not found\n");
1118 $main::lxdebug->leave_sub();
1124 $main::lxdebug->enter_sub();
1128 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
1130 my $display = $self->is_xvfb_running();
1133 $main::lxdebug->leave_sub();
1138 while ( -f "/tmp/.X${display}-lock") {
1141 $display = ":${display}";
1142 $main::lxdebug->message(LXDebug::DEBUG2, " display $display\n");
1144 my $mcookie = `mcookie`;
1145 die("Installation error: mcookie not found.") if ($? != 0);
1148 $main::lxdebug->message(LXDebug::DEBUG2, " mcookie $mcookie\n");
1150 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
1151 $ENV{"XAUTHORITY"} = $xauthority;
1153 $main::lxdebug->message(LXDebug::DEBUG2, " xauthority $xauthority\n");
1155 system("xauth add \"${display}\" . \"${mcookie}\"");
1157 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
1158 $main::lxdebug->leave_sub();
1162 $main::lxdebug->message(LXDebug::DEBUG2, " about to fork()\n");
1166 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1167 exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
1170 $main::lxdebug->message(LXDebug::DEBUG2, " parent dont sleeping\n");
1173 my $dfname = $self->{"userspath"} . "/xvfb_display";
1174 if (!open(OUT, ">$dfname")) {
1175 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
1176 unlink($xauthority);
1178 $main::lxdebug->leave_sub();
1181 print(OUT "$pid\n$display\n$xauthority\n");
1184 $main::lxdebug->message(LXDebug::DEBUG2, " parent re-testing\n");
1186 if (!$self->is_xvfb_running()) {
1187 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
1188 unlink($xauthority, $dfname);
1190 $main::lxdebug->leave_sub();
1194 $main::lxdebug->message(LXDebug::DEBUG2, " spawn OK\n");
1196 $main::lxdebug->leave_sub();
1201 sub is_openoffice_running {
1202 $main::lxdebug->enter_sub();
1204 system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
1205 "> /dev/null 2> /dev/null");
1207 $main::lxdebug->message(LXDebug::DEBUG2, " is_openoffice_running(): $?\n");
1209 $main::lxdebug->leave_sub();
1214 sub spawn_openoffice {
1215 $main::lxdebug->enter_sub();
1219 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
1221 my ($try, $spawned_oo, $res);
1224 for ($try = 0; $try < 15; $try++) {
1225 if ($self->is_openoffice_running()) {
1233 $main::lxdebug->message(LXDebug::DEBUG2, " Child daemonizing\n");
1235 open(STDIN, '/dev/null');
1236 open(STDOUT, '>/dev/null');
1237 my $new_pid = fork();
1239 my $ssres = setsid();
1240 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1241 my @cmdline = ($main::openofficeorg_writer_bin,
1242 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1244 "-accept=socket,host=localhost,port=" .
1245 $main::openofficeorg_daemon_port . ";urp;");
1249 $main::lxdebug->message(LXDebug::DEBUG2, " Parent after fork\n");
1254 sleep($try >= 5 ? 2 : 1);
1258 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
1259 "OpenOffice could not be started.";
1262 $main::lxdebug->leave_sub();
1267 sub convert_to_pdf {
1268 $main::lxdebug->enter_sub();
1272 my $form = $self->{"form"};
1274 my $filename = $form->{"tmpfile"};
1275 $filename =~ s/.odt$//;
1276 if (substr($filename, 0, 1) ne "/") {
1277 $filename = getcwd() . "/${filename}";
1280 if (substr($self->{"userspath"}, 0, 1) eq "/") {
1281 $ENV{'HOME'} = $self->{"userspath"};
1283 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
1286 if (!$self->spawn_xvfb()) {
1287 $main::lxdebug->leave_sub();
1292 if (!$main::openofficeorg_daemon) {
1293 @cmdline = ($main::openofficeorg_writer_bin,
1294 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1296 "file:${filename}.odt",
1297 "macro://" . (split('/', $filename))[-1] .
1298 "/Standard.Conversion.ConvertSelfToPDF()");
1300 if (!$self->spawn_openoffice()) {
1301 $main::lxdebug->leave_sub();
1305 @cmdline = ("./scripts/oo-uno-convert-pdf.py",
1306 $main::openofficeorg_daemon_port,
1314 $form->{"tmpfile"} =~ s/odt$/pdf/;
1316 unlink($filename . ".odt");
1318 $main::lxdebug->leave_sub();
1323 unlink($filename . ".odt", $filename . ".pdf");
1324 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
1327 $main::lxdebug->leave_sub();
1332 my ($self, $variable) = @_;
1333 my $form = $self->{"form"};
1334 my $iconv = $self->{"iconv"};
1336 $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
1338 # Allow some HTML markup to be converted into the output format's
1339 # corresponding markup code, e.g. bold or italic.
1340 my $rnd = $self->{"rnd"};
1341 my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
1342 "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
1344 foreach my $key (keys(%markup_replace)) {
1345 my $value = $markup_replace{$key};
1346 $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
1347 $variable =~ s|\</${key}\>|</text:span>|gi;
1350 return $iconv->convert($variable);
1353 sub get_mime_type() {
1354 if ($self->{"form"}->{"format"} =~ /pdf/) {
1355 return "application/pdf";
1357 return "application/vnd.oasis.opendocument.text";
1361 sub uses_temp_file {
1366 ##########################################################
1370 ##########################################################
1372 package XMLTemplate;
1376 @ISA = qw(HTMLTemplate);
1379 #evtl auskommentieren
1382 return $type->SUPER::new(@_);
1386 my ($self, $variable) = @_;
1387 my $form = $self->{"form"};
1389 $variable = $main::locale->quote_special_chars('Template/XML', $variable);
1391 # Allow no markup to be converted into the output format
1392 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
1394 foreach my $key (@markup_replace) {
1395 $variable =~ s/\<(\/?)${key}\>//g;
1401 sub get_mime_type() {
1404 if ($self->{"form"}->{"format"} =~ /elsterwinston/i) {
1405 return "application/xml ";
1406 } elsif ($self->{"form"}->{"format"} =~ /elstertaxbird/i) {
1407 return "application/x-taxbird";
1413 sub uses_temp_file {
1414 # tempfile needet for XML Output