1 #====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #====================================================================
9 package SimpleTemplate;
12 # 1. The template's file name
13 # 2. A reference to the Form object
14 # 3. A reference to the myconfig hash
17 # A new template object
31 $self->{source} = shift;
32 $self->{form} = shift;
33 $self->{myconfig} = shift;
34 $self->{userspath} = shift;
36 $self->{error} = undef;
38 $self->set_tag_style('<%', '%>');
43 my $tag_start = shift;
46 $self->{tag_start} = $tag_start;
47 $self->{tag_end} = $tag_end;
48 $self->{tag_start_qm} = quotemeta $tag_start;
49 $self->{tag_end_qm} = quotemeta $tag_end;
57 # 1. A typeglob for the file handle. The output will be written
58 # to this file handle.
61 # 1 on success and undef or 0 if there was an error. In the latter case
62 # the calling function can retrieve the error message via $obj->get_error()
67 print(OUT "Hallo!\n");
73 return $self->{"error"};
86 package LaTeXTemplate;
90 @ISA = qw(SimpleTemplate);
95 return $type->SUPER::new(@_);
99 my ($self, $variable) = @_;
100 my $form = $self->{"form"};
102 $variable = $main::locale->quote_special_chars('Template/LaTeX', $variable);
104 # Allow some HTML markup to be converted into the output format's
105 # corresponding markup code, e.g. bold or italic.
106 my %markup_replace = ('b' => 'textbf',
110 foreach my $key (keys(%markup_replace)) {
111 my $new = $markup_replace{$key};
112 $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
115 $variable =~ s/[\x00-\x1f]//g;
120 sub substitute_vars {
121 my ($self, $text, @indices) = @_;
123 my $form = $self->{"form"};
125 while ($text =~ /$self->{tag_start_qm}(.+?)$self->{tag_end_qm}/) {
126 my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]);
127 my ($var, @options) = split(/\s+/, $1);
128 my $value = $form->{$var};
130 for (my $i = 0; $i < scalar(@indices); $i++) {
131 last unless (ref($value) eq "ARRAY");
132 $value = $value->[$indices[$i]];
134 $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
135 substr($text, $tag_pos, $tag_len) = $value;
142 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
144 my ($form, $new_contents) = ($self->{"form"}, "");
146 my $ary = $form->{$var};
147 for (my $i = 0; $i < scalar(@indices); $i++) {
148 last unless (ref($ary) eq "ARRAY");
149 $ary = $ary->[$indices[$i]];
153 my $current_page = 1;
154 my ($current_line, $corrent_row) = (0, 1);
156 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
157 $form->{"__first__"} = $i == 0;
158 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
159 $form->{"__odd__"} = (($i + 1) % 2) == 1;
160 $form->{"__counter__"} = $i + 1;
162 if ((scalar(@{$form->{"description"}}) == scalar(@{$ary})) &&
163 $self->{"chars_per_line"}) {
165 int(length($form->{"description"}->[$i]) / $self->{"chars_per_line"});
168 $form->{"description"}->[$i] =~ s/(\\newline\s?)*$//;
169 my $_description = $form->{"description"}->[$i];
170 while ($_description =~ /\\newline/) {
172 $_description =~ s/\\newline//;
176 if ($current_page == 1) {
177 $lpp = $self->{"lines_on_first_page"};
179 $lpp = $self->{"lines_on_second_page"};
182 # Yes we need a manual page break -- or the user has forced one
183 if ((($current_line + $lines) > $lpp) ||
184 ($form->{"description"}->[$i] =~ /<pagebreak>/)) {
185 my $pb = $self->{"pagebreak_block"};
187 # replace the special variables <%sumcarriedforward%>
190 my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
191 $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
192 $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
194 my $new_text = $self->parse_block($pb, (@indices, $i));
195 return undef unless (defined($new_text));
196 $new_contents .= $new_text;
201 $current_line += $lines;
203 if ($i < scalar(@{$form->{"linetotal"}})) {
204 $sum += $form->parse_amount($self->{"myconfig"},
205 $form->{"linetotal"}->[$i]);
208 $form->{"cumulatelinetotal"}[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
210 my $new_text = $self->parse_block($text, (@indices, $i));
211 return undef unless (defined($new_text));
212 $new_contents .= $start_tag . $new_text . $end_tag;
214 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
216 return $new_contents;
220 my ($self, $text, $pos, $var, $not) = @_;
222 my $tag_start_len = length $self->{tag_start};
225 $pos = 0 unless ($pos);
227 while ($pos < length($text)) {
230 next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
232 my $keyword_pos = $pos - 1 + $tag_start_len;
234 if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 3) eq 'for')) {
237 } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
240 "$self->{tag_start}else$self->{tag_end} outside of "
241 . "$self->{tag_start}if$self->{tag_end} / "
242 . "$self->{tag_start}ifnot$self->{tag_end}.";
246 my $block = substr($text, 0, $pos - 1);
247 substr($text, 0, $pos - 1) = "";
248 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
249 $text = $self->{tag_start} . 'if' . ($not ? " " : "not ") . $var . $self->{tag_end} . $text;
251 return ($block, $text);
253 } elsif (substr($text, $keyword_pos, 3) eq 'end') {
256 my $block = substr($text, 0, $pos - 1);
257 substr($text, 0, $pos - 1) = "";
258 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
260 return ($block, $text);
269 $main::lxdebug->enter_sub();
271 my ($self, $contents, @indices) = @_;
273 my $new_contents = "";
275 while ($contents ne "") {
276 my $pos_if = index($contents, $self->{tag_start} . 'if');
277 my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
279 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
280 $new_contents .= $self->substitute_vars($contents, @indices);
284 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
285 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
286 substr($contents, 0, $pos_foreach) = "";
288 if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
289 $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
290 $main::lxdebug->leave_sub();
296 substr($contents, 0, length($&)) = "";
299 ($block, $contents) = $self->find_end($contents);
301 $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
302 $main::lxdebug->leave_sub();
306 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
307 if (!defined($new_text)) {
308 $main::lxdebug->leave_sub();
311 $new_contents .= $new_text;
314 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
315 substr($contents, 0, $pos_if) = "";
317 if ($contents !~ m|^$self->{tag_start_qm}if\s*(not)?\s+(.*?)$self->{tag_end_qm}|) {
318 $self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}.";
319 $main::lxdebug->leave_sub();
323 my ($not, $var) = ($1, $2);
325 substr($contents, 0, length($&)) = "";
327 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
329 $self->{"error"} = "Unclosed $self->{tag_start}if${not}$self->{tag_end}." unless ($self->{"error"});
330 $main::lxdebug->leave_sub();
334 my $value = $self->{"form"}->{$var};
335 for (my $i = 0; $i < scalar(@indices); $i++) {
336 last unless (ref($value) eq "ARRAY");
337 $value = $value->[$indices[$i]];
340 if (($not && !$value) || (!$not && $value)) {
341 my $new_text = $self->parse_block($block, @indices);
342 if (!defined($new_text)) {
343 $main::lxdebug->leave_sub();
346 $new_contents .= $new_text;
351 $main::lxdebug->leave_sub();
353 return $new_contents;
356 sub parse_first_line {
358 my $line = shift || "";
360 if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
362 $self->{error} = "The tag start and end markers must not be equal.";
366 $self->set_tag_style($1, $2);
372 sub _parse_config_option {
379 my ($key, $value) = split m/\s*=\s*/, $line, 2;
381 if ($key eq 'tag-style') {
382 $self->set_tag_style(split(m/\s+/, $value, 2));
386 sub _parse_config_lines {
390 my ($comment_start, $comment_end) = ("", "");
392 if (ref $self eq 'LaTeXTemplate') {
393 $comment_start = '\s*%';
394 } elsif (ref $self eq 'HTMLTemplate') {
395 $comment_start = '\s*<!--';
396 $comment_end = '>\s*';
398 $comment_start = '\s*\#';
401 my $num_lines = scalar @{ $lines };
404 while ($i < $num_lines) {
405 my $line = $lines->[$i];
407 if ($line !~ m/^${comment_start}\s*config\s*:(.*)${comment_end}$/i) {
412 $self->_parse_config_option($1);
413 splice @{ $lines }, $i, 1;
421 my $form = $self->{"form"};
423 if (!open(IN, "$form->{templates}/$form->{IN}")) {
424 $self->{"error"} = "$!";
430 $self->_parse_config_lines(\@lines);
432 my $contents = join("", @lines);
434 # detect pagebreak block and its parameters
435 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) {
436 $self->{"chars_per_line"} = $1;
437 $self->{"lines_on_first_page"} = $2;
438 $self->{"lines_on_second_page"} = $3;
439 $self->{"pagebreak_block"} = $4;
441 substr($contents, length($`), length($&)) = "";
444 $self->{"forced_pagebreaks"} = [];
446 my $new_contents = $self->parse_block($contents);
447 if (!defined($new_contents)) {
448 $main::lxdebug->leave_sub();
452 print(OUT $new_contents);
454 if ($form->{"format"} =~ /postscript/i) {
455 return $self->convert_to_postscript();
456 } elsif ($form->{"format"} =~ /pdf/i) {
457 return $self->convert_to_pdf();
463 sub convert_to_postscript {
465 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
467 # Convert the tex file to postscript
469 if (!chdir("$userspath")) {
470 $self->{"error"} = "chdir : $!";
475 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
477 for (my $run = 1; $run <= 2; $run++) {
478 system("latex --interaction=nonstopmode $form->{tmpfile} " .
479 "> $form->{tmpfile}.err");
481 $self->{"error"} = $form->cleanup();
487 $form->{tmpfile} =~ s/tex$/dvi/;
489 system("dvips $form->{tmpfile} -o -q > /dev/null");
491 $self->{"error"} = "dvips : $!";
495 $form->{tmpfile} =~ s/dvi$/ps/;
504 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
506 # Convert the tex file to PDF
508 if (!chdir("$userspath")) {
509 $self->{"error"} = "chdir : $!";
514 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
516 for (my $run = 1; $run <= 2; $run++) {
517 system("pdflatex --interaction=nonstopmode $form->{tmpfile} " .
518 "> $form->{tmpfile}.err");
520 $self->{"error"} = $form->cleanup();
526 $form->{tmpfile} =~ s/tex$/pdf/;
531 sub get_mime_type() {
534 if ($self->{"form"}->{"format"} =~ /postscript/i) {
535 return "application/postscript";
537 return "application/pdf";
550 package HTMLTemplate;
554 @ISA = qw(LaTeXTemplate);
559 return $type->SUPER::new(@_);
563 my ($self, $variable) = @_;
564 my $form = $self->{"form"};
566 $variable = $main::locale->quote_special_chars('Template/HTML', $variable);
568 # Allow some HTML markup to be converted into the output format's
569 # corresponding markup code, e.g. bold or italic.
570 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
572 foreach my $key (@markup_replace) {
573 $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
579 sub get_mime_type() {
582 if ($self->{"form"}->{"format"} =~ /postscript/i) {
583 return "application/postscript";
584 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
585 return "application/pdf";
594 if ($self->{"form"}->{"format"} =~ /postscript/i) {
596 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
603 sub convert_to_postscript {
605 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
607 # Convert the HTML file to postscript
609 if (!chdir("$userspath")) {
610 $self->{"error"} = "chdir : $!";
615 $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
616 my $psfile = $form->{"tmpfile"};
617 $psfile =~ s/.html/.ps/;
618 if ($psfile eq $form->{"tmpfile"}) {
622 system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
624 $self->{"error"} = $form->cleanup();
629 $form->{"tmpfile"} = $psfile;
638 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
640 # Convert the HTML file to PDF
642 if (!chdir("$userspath")) {
643 $self->{"error"} = "chdir : $!";
648 $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
649 my $pdffile = $form->{"tmpfile"};
650 $pdffile =~ s/.html/.pdf/;
651 if ($pdffile eq $form->{"tmpfile"}) {
655 system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
657 $self->{"error"} = $form->cleanup();
662 $form->{"tmpfile"} = $pdffile;
671 #### PlainTextTemplate
674 package PlainTextTemplate;
678 @ISA = qw(LaTeXTemplate);
683 return $type->SUPER::new(@_);
687 my ($self, $variable) = @_;
702 #### OpenDocumentTemplate
705 package OpenDocumentTemplate;
713 # use File::Temp qw(:mktemp);
716 @ISA = qw(SimpleTemplate);
721 $self = $type->SUPER::new(@_);
723 foreach my $module (qw(Archive::Zip Text::Iconv)) {
724 eval("use ${module};");
726 $self->{"form"}->error("The Perl module '${module}' could not be " .
727 "loaded. Support for OpenDocument templates " .
728 "does not work without it. Please install your " .
729 "distribution's package or get the module from " .
730 "CPAN ( http://www.cpan.org ).");
734 $self->{"rnd"} = int(rand(1000000));
735 $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
740 sub substitute_vars {
741 my ($self, $text, @indices) = @_;
743 my $form = $self->{"form"};
745 while ($text =~ /\<\%(.*?)\%\>/) {
746 my $value = $form->{$1};
748 for (my $i = 0; $i < scalar(@indices); $i++) {
749 last unless (ref($value) eq "ARRAY");
750 $value = $value->[$indices[$i]];
752 substr($text, $-[0], $+[0] - $-[0]) = $self->format_string($value);
759 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
761 my ($form, $new_contents) = ($self->{"form"}, "");
763 my $ary = $form->{$var};
764 for (my $i = 0; $i < scalar(@indices); $i++) {
765 last unless (ref($ary) eq "ARRAY");
766 $ary = $ary->[$indices[$i]];
769 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
770 $form->{"__first__"} = $i == 0;
771 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
772 $form->{"__odd__"} = (($i + 1) % 2) == 1;
773 $form->{"__counter__"} = $i + 1;
774 my $new_text = $self->parse_block($text, (@indices, $i));
775 return undef unless (defined($new_text));
776 $new_contents .= $start_tag . $new_text . $end_tag;
778 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
780 return $new_contents;
784 my ($self, $text, $pos, $var, $not) = @_;
787 $pos = 0 unless ($pos);
789 while ($pos < length($text)) {
792 next if (substr($text, $pos - 1, 5) ne '<%');
794 if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
797 } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
799 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
803 my $block = substr($text, 0, $pos - 1);
804 substr($text, 0, $pos - 1) = "";
805 $text =~ s!^\<\%[^\%]+\%\>!!;
806 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
808 return ($block, $text);
810 } elsif (substr($text, $pos + 4, 3) eq 'end') {
813 my $block = substr($text, 0, $pos - 1);
814 substr($text, 0, $pos - 1) = "";
815 $text =~ s!^\<\%[^\%]+\%\>!!;
817 return ($block, $text);
826 $main::lxdebug->enter_sub();
828 my ($self, $contents, @indices) = @_;
830 my $new_contents = "";
832 while ($contents ne "") {
833 if (substr($contents, 0, 1) eq "<") {
834 $contents =~ m|^<[^>]+>|;
836 substr($contents, 0, length($&)) = "";
838 if ($tag =~ m|<table:table-row|) {
839 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
842 substr($contents, 0, length($1) + length($end_tag)) = "";
844 if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
847 substr($table_row, length($`), length($&)) = "";
849 my ($t1, $t2) = $self->find_end($table_row, length($`));
851 $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
852 $main::lxdebug->leave_sub();
856 my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
857 if (!defined($new_text)) {
858 $main::lxdebug->leave_sub();
861 $new_contents .= $new_text;
864 my $new_text = $self->parse_block($table_row, @indices);
865 if (!defined($new_text)) {
866 $main::lxdebug->leave_sub();
869 $new_contents .= $tag . $new_text . $end_tag;
873 $new_contents .= $tag;
877 $contents =~ /^[^<]+/;
880 my $pos_if = index($text, '<%if');
881 my $pos_foreach = index($text, '<%foreach');
883 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
884 substr($contents, 0, length($text)) = "";
885 $new_contents .= $self->substitute_vars($text, @indices);
889 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
890 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
891 substr($contents, 0, $pos_foreach) = "";
893 if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
894 $self->{"error"} = "Malformed <\%foreach\%>.";
895 $main::lxdebug->leave_sub();
901 substr($contents, 0, length($&)) = "";
904 ($block, $contents) = $self->find_end($contents);
906 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
907 $main::lxdebug->leave_sub();
911 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
912 if (!defined($new_text)) {
913 $main::lxdebug->leave_sub();
916 $new_contents .= $new_text;
919 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
920 substr($contents, 0, $pos_if) = "";
922 if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
923 $self->{"error"} = "Malformed <\%if\%>.";
924 $main::lxdebug->leave_sub();
928 my ($not, $var) = ($1, $2);
930 substr($contents, 0, length($&)) = "";
932 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
934 $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
935 $main::lxdebug->leave_sub();
939 my $value = $self->{"form"}->{$var};
940 for (my $i = 0; $i < scalar(@indices); $i++) {
941 last unless (ref($value) eq "ARRAY");
942 $value = $value->[$indices[$i]];
945 if (($not && !$value) || (!$not && $value)) {
946 my $new_text = $self->parse_block($block, @indices);
947 if (!defined($new_text)) {
948 $main::lxdebug->leave_sub();
951 $new_contents .= $new_text;
957 $main::lxdebug->leave_sub();
959 return $new_contents;
963 $main::lxdebug->enter_sub();
967 my $form = $self->{"form"};
972 if ($form->{"IN"} =~ m|^/|) {
973 $file_name = $form->{"IN"};
975 $file_name = $form->{"templates"} . "/" . $form->{"IN"};
978 my $zip = Archive::Zip->new();
979 if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
980 $self->{"error"} = "File not found/is not a OpenDocument file.";
981 $main::lxdebug->leave_sub();
985 my $contents = $zip->contents("content.xml");
987 $self->{"error"} = "File is not a OpenDocument file.";
988 $main::lxdebug->leave_sub();
992 my $rnd = $self->{"rnd"};
993 my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
994 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
996 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
997 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
999 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
1000 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
1002 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
1003 <style:text-properties style:text-line-through-style="solid"/>
1005 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
1006 <style:text-properties style:text-position="super 58%"/>
1008 <style:style style:name="TLXO${rnd}SUB" style:family="text">
1009 <style:text-properties style:text-position="sub 58%"/>
1013 $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
1014 $contents =~ s|[\n\r]||gm;
1016 my $new_contents = $self->parse_block($contents);
1017 if (!defined($new_contents)) {
1018 $main::lxdebug->leave_sub();
1022 # $new_contents =~ s|>|>\n|g;
1024 $zip->contents("content.xml", $new_contents);
1026 my $styles = $zip->contents("styles.xml");
1028 my $new_styles = $self->parse_block($styles);
1029 if (!defined($new_contents)) {
1030 $main::lxdebug->leave_sub();
1033 $zip->contents("styles.xml", $new_styles);
1036 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
1039 if ($form->{"format"} =~ /pdf/) {
1040 $res = $self->convert_to_pdf();
1043 $main::lxdebug->leave_sub();
1047 sub is_xvfb_running {
1048 $main::lxdebug->enter_sub();
1053 my $dfname = $self->{"userspath"} . "/xvfb_display";
1056 $main::lxdebug->message(LXDebug::DEBUG2, " Looking for $dfname\n");
1057 if ((-f $dfname) && open(IN, $dfname)) {
1062 my $xauthority = <IN>;
1066 $main::lxdebug->message(LXDebug::DEBUG2, " found with $pid and $display\n");
1068 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
1069 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #1\n");
1070 unlink($dfname, $xauthority);
1071 $main::lxdebug->leave_sub();
1076 if ($line !~ /xvfb/i) {
1077 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #2\n");
1078 unlink($dfname, $xauthority);
1079 $main::lxdebug->leave_sub();
1083 $ENV{"XAUTHORITY"} = $xauthority;
1084 $ENV{"DISPLAY"} = $display;
1086 $main::lxdebug->message(LXDebug::DEBUG2, " not found\n");
1089 $main::lxdebug->leave_sub();
1095 $main::lxdebug->enter_sub();
1099 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
1101 my $display = $self->is_xvfb_running();
1104 $main::lxdebug->leave_sub();
1109 while ( -f "/tmp/.X${display}-lock") {
1112 $display = ":${display}";
1113 $main::lxdebug->message(LXDebug::DEBUG2, " display $display\n");
1115 my $mcookie = `mcookie`;
1116 die("Installation error: mcookie not found.") if ($? != 0);
1119 $main::lxdebug->message(LXDebug::DEBUG2, " mcookie $mcookie\n");
1121 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
1122 $ENV{"XAUTHORITY"} = $xauthority;
1124 $main::lxdebug->message(LXDebug::DEBUG2, " xauthority $xauthority\n");
1126 system("xauth add \"${display}\" . \"${mcookie}\"");
1128 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
1129 $main::lxdebug->leave_sub();
1133 $main::lxdebug->message(LXDebug::DEBUG2, " about to fork()\n");
1137 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1138 exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
1141 $main::lxdebug->message(LXDebug::DEBUG2, " parent dont sleeping\n");
1144 my $dfname = $self->{"userspath"} . "/xvfb_display";
1145 if (!open(OUT, ">$dfname")) {
1146 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
1147 unlink($xauthority);
1149 $main::lxdebug->leave_sub();
1152 print(OUT "$pid\n$display\n$xauthority\n");
1155 $main::lxdebug->message(LXDebug::DEBUG2, " parent re-testing\n");
1157 if (!$self->is_xvfb_running()) {
1158 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
1159 unlink($xauthority, $dfname);
1161 $main::lxdebug->leave_sub();
1165 $main::lxdebug->message(LXDebug::DEBUG2, " spawn OK\n");
1167 $main::lxdebug->leave_sub();
1172 sub is_openoffice_running {
1173 $main::lxdebug->enter_sub();
1175 system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
1176 "> /dev/null 2> /dev/null");
1178 $main::lxdebug->message(LXDebug::DEBUG2, " is_openoffice_running(): $?\n");
1180 $main::lxdebug->leave_sub();
1185 sub spawn_openoffice {
1186 $main::lxdebug->enter_sub();
1190 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
1192 my ($try, $spawned_oo, $res);
1195 for ($try = 0; $try < 15; $try++) {
1196 if ($self->is_openoffice_running()) {
1204 $main::lxdebug->message(LXDebug::DEBUG2, " Child daemonizing\n");
1206 open(STDIN, '/dev/null');
1207 open(STDOUT, '>/dev/null');
1208 my $new_pid = fork();
1210 my $ssres = setsid();
1211 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1212 my @cmdline = ($main::openofficeorg_writer_bin,
1213 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1215 "-accept=socket,host=localhost,port=" .
1216 $main::openofficeorg_daemon_port . ";urp;");
1220 $main::lxdebug->message(LXDebug::DEBUG2, " Parent after fork\n");
1225 sleep($try >= 5 ? 2 : 1);
1229 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
1230 "OpenOffice could not be started.";
1233 $main::lxdebug->leave_sub();
1238 sub convert_to_pdf {
1239 $main::lxdebug->enter_sub();
1243 my $form = $self->{"form"};
1245 my $filename = $form->{"tmpfile"};
1246 $filename =~ s/.odt$//;
1247 if (substr($filename, 0, 1) ne "/") {
1248 $filename = getcwd() . "/${filename}";
1251 if (substr($self->{"userspath"}, 0, 1) eq "/") {
1252 $ENV{'HOME'} = $self->{"userspath"};
1254 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
1257 if (!$self->spawn_xvfb()) {
1258 $main::lxdebug->leave_sub();
1263 if (!$main::openofficeorg_daemon) {
1264 @cmdline = ($main::openofficeorg_writer_bin,
1265 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1267 "file:${filename}.odt",
1268 "macro://" . (split('/', $filename))[-1] .
1269 "/Standard.Conversion.ConvertSelfToPDF()");
1271 if (!$self->spawn_openoffice()) {
1272 $main::lxdebug->leave_sub();
1276 @cmdline = ("./scripts/oo-uno-convert-pdf.py",
1277 $main::openofficeorg_daemon_port,
1285 $form->{"tmpfile"} =~ s/odt$/pdf/;
1287 unlink($filename . ".odt");
1289 $main::lxdebug->leave_sub();
1294 unlink($filename . ".odt", $filename . ".pdf");
1295 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
1298 $main::lxdebug->leave_sub();
1303 my ($self, $variable) = @_;
1304 my $form = $self->{"form"};
1305 my $iconv = $self->{"iconv"};
1307 $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
1309 # Allow some HTML markup to be converted into the output format's
1310 # corresponding markup code, e.g. bold or italic.
1311 my $rnd = $self->{"rnd"};
1312 my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
1313 "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
1315 foreach my $key (keys(%markup_replace)) {
1316 my $value = $markup_replace{$key};
1317 $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
1318 $variable =~ s|\</${key}\>|</text:span>|gi;
1321 return $iconv->convert($variable);
1324 sub get_mime_type() {
1325 if ($self->{"form"}->{"format"} =~ /pdf/) {
1326 return "application/pdf";
1328 return "application/vnd.oasis.opendocument.text";
1332 sub uses_temp_file {
1337 ##########################################################
1341 ##########################################################
1343 package XMLTemplate;
1347 @ISA = qw(HTMLTemplate);
1350 #evtl auskommentieren
1353 return $type->SUPER::new(@_);
1357 my ($self, $variable) = @_;
1358 my $form = $self->{"form"};
1360 $variable = $main::locale->quote_special_chars('Template/XML', $variable);
1362 # Allow no markup to be converted into the output format
1363 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
1365 foreach my $key (@markup_replace) {
1366 $variable =~ s/\<(\/?)${key}\>//g;
1372 sub get_mime_type() {
1375 if ($self->{"form"}->{"format"} =~ /elsterwinston/i) {
1376 return "application/xml ";
1377 } elsif ($self->{"form"}->{"format"} =~ /elstertaxbird/i) {
1378 return "application/x-taxbird";
1384 sub uses_temp_file {
1385 # tempfile needet for XML Output