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;
44 # 1. A typeglob for the file handle. The output will be written
45 # to this file handle.
48 # 1 on success and undef or 0 if there was an error. In the latter case
49 # the calling function can retrieve the error message via $obj->get_error()
54 print(OUT "Hallo!\n");
60 return $self->{"error"};
73 package LaTeXTemplate;
77 @ISA = qw(SimpleTemplate);
82 return $type->SUPER::new(@_);
86 my ($self, $variable) = @_;
87 my $form = $self->{"form"};
90 ('order' => [quotemeta("\\"),
93 '"', '\$', '%', '_', '#', quotemeta('^'),
94 '{', '}', '<', '>', '£', "\r"
96 quotemeta("\\") => '\\textbackslash ',
110 quotemeta('^') => '\^\\',
111 quotemeta("\n") => '\newline '
114 map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
116 # Allow some HTML markup to be converted into the output format's
117 # corresponding markup code, e.g. bold or italic.
118 my %markup_replace = ('b' => 'textbf',
122 foreach my $key (keys(%markup_replace)) {
123 my $new = $markup_replace{$key};
124 $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
130 sub substitute_vars {
131 my ($self, $text, @indices) = @_;
133 my $form = $self->{"form"};
135 while ($text =~ /<\%(.*?)\%>/) {
136 my ($var, @options) = split(/\s+/, $1);
137 my $value = $form->{$var};
139 for (my $i = 0; $i < scalar(@indices); $i++) {
140 last unless (ref($value) eq "ARRAY");
141 $value = $value->[$indices[$i]];
143 $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
144 substr($text, $-[0], $+[0] - $-[0]) = $value;
151 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
153 my ($form, $new_contents) = ($self->{"form"}, "");
155 my $ary = $form->{$var};
156 for (my $i = 0; $i < scalar(@indices); $i++) {
157 last unless (ref($ary) eq "ARRAY");
158 $ary = $ary->[$indices[$i]];
162 my $current_page = 1;
163 my ($current_line, $corrent_row) = (0, 1);
165 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
166 $form->{"__first__"} = $i == 0;
167 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
168 $form->{"__odd__"} = (($i + 1) % 2) == 1;
169 $form->{"__counter__"} = $i + 1;
171 if ((scalar(@{$form->{"description"}}) == scalar(@{$ary})) &&
172 $self->{"chars_per_line"}) {
174 int(length($form->{"description"}->[$i]) / $self->{"chars_per_line"});
177 $form->{"description"}->[$i] =~ s/(\\newline\s?)*$//;
178 my $_description = $form->{"description"}->[$i];
179 while ($_description =~ /\\newline/) {
181 $_description =~ s/\\newline//;
185 if ($current_page == 1) {
186 $lpp = $self->{"lines_on_first_page"};
188 $lpp = $self->{"lines_on_second_page"};
191 # Yes we need a manual page break -- or the user has forced one
192 if ((($current_line + $lines) > $lpp) ||
193 ($form->{"description"}->[$i] =~ /<pagebreak>/)) {
194 my $pb = $self->{"pagebreak_block"};
196 # replace the special variables <%sumcarriedforward%>
199 my $psum = $form->format_amount($myconfig, $sum, 2);
200 $pb =~ s/<%sumcarriedforward%>/$psum/g;
201 $pb =~ s/<%lastpage%>/$current_page/g;
203 my $new_text = $self->parse_block($pb, (@indices, $i));
204 return undef unless (defined($new_text));
205 $new_contents .= $new_text;
210 $current_line += $lines;
212 if ($i < scalar(@{$form->{"linetotal"}})) {
213 $sum += $form->parse_amount($myconfig, $form->{"linetotal"}->[$i]);
216 my $new_text = $self->parse_block($text, (@indices, $i));
217 return undef unless (defined($new_text));
218 $new_contents .= $start_tag . $new_text . $end_tag;
220 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
222 return $new_contents;
226 my ($self, $text, $pos, $var, $not) = @_;
229 $pos = 0 unless ($pos);
231 while ($pos < length($text)) {
234 next if (substr($text, $pos - 1, 2) ne '<%');
236 if ((substr($text, $pos + 1, 2) eq 'if') || (substr($text, $pos + 1, 3) eq 'for')) {
239 } elsif ((substr($text, $pos + 1, 4) eq 'else') && (1 == $depth)) {
241 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
245 my $block = substr($text, 0, $pos - 1);
246 substr($text, 0, $pos - 1) = "";
247 $text =~ s!^<\%[^\%]+\%>!!;
248 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
250 return ($block, $text);
252 } elsif (substr($text, $pos + 1, 3) eq 'end') {
255 my $block = substr($text, 0, $pos - 1);
256 substr($text, 0, $pos - 1) = "";
257 $text =~ s!^<\%[^\%]+\%>!!;
259 return ($block, $text);
268 $main::lxdebug->enter_sub();
270 my ($self, $contents, @indices) = @_;
272 my $new_contents = "";
274 while ($contents ne "") {
275 my $pos_if = index($contents, '<%if');
276 my $pos_foreach = index($contents, '<%foreach');
278 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
279 $new_contents .= $self->substitute_vars($contents, @indices);
283 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
284 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
285 substr($contents, 0, $pos_foreach) = "";
287 if ($contents !~ m|^<\%foreach (.*?)\%>|) {
288 $self->{"error"} = "Malformed <\%foreach\%>.";
289 $main::lxdebug->leave_sub();
295 substr($contents, 0, length($&)) = "";
298 ($block, $contents) = $self->find_end($contents);
300 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
301 $main::lxdebug->leave_sub();
305 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
306 if (!defined($new_text)) {
307 $main::lxdebug->leave_sub();
310 $new_contents .= $new_text;
313 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
314 substr($contents, 0, $pos_if) = "";
316 if ($contents !~ m|^<\%if\s*(not)?\s+(.*?)\%>|) {
317 $self->{"error"} = "Malformed <\%if\%>.";
318 $main::lxdebug->leave_sub();
322 my ($not, $var) = ($1, $2);
324 substr($contents, 0, length($&)) = "";
326 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
328 $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
329 $main::lxdebug->leave_sub();
333 my $value = $self->{"form"}->{$var};
334 for (my $i = 0; $i < scalar(@indices); $i++) {
335 last unless (ref($value) eq "ARRAY");
336 $value = $value->[$indices[$i]];
339 if (($not && !$value) || (!$not && $value)) {
340 my $new_text = $self->parse_block($block, @indices);
341 if (!defined($new_text)) {
342 $main::lxdebug->leave_sub();
345 $new_contents .= $new_text;
350 $main::lxdebug->leave_sub();
352 return $new_contents;
358 my $form = $self->{"form"};
360 # Do we have to run LaTeX two times? This is needed if
361 # the template contains page references.
364 if (!open(IN, "$form->{templates}/$form->{IN}")) {
365 $self->{"error"} = "$!";
371 my $contents = join("", @_);
372 $two_passes = 1 if ($contents =~ /\\pageref/s);
374 # detect pagebreak block and its parameters
375 if ($contents =~ /<%pagebreak\s+(\d+)\s+(\d+)\s+(\d+)\s*%>(.*?)<%end(\s*pagebreak)?%>/s) {
376 $self->{"chars_per_line"} = $1;
377 $self->{"lines_on_first_page"} = $2;
378 $self->{"lines_on_second_page"} = $3;
379 $self->{"pagebreak_block"} = $4;
381 substr($contents, length($`), length($&)) = "";
384 $self->{"forced_pagebreaks"} = [];
386 my $new_contents = $self->parse_block($contents);
387 if (!defined($new_contents)) {
388 $main::lxdebug->leave_sub();
392 print(OUT $new_contents);
394 if ($form->{"format"} =~ /postscript/i) {
395 return $self->convert_to_postscript($two_passes);
396 } elsif ($form->{"format"} =~ /pdf/i) {
397 return $self->convert_to_pdf($two_passes);
403 sub convert_to_postscript {
404 my ($self, $two_passes) = @_;
405 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
407 # Convert the tex file to postscript
409 if (!chdir("$userspath")) {
410 $self->{"error"} = "chdir : $!";
415 $form->{tmpfile} =~ s/$userspath\///g;
417 system("latex --interaction=nonstopmode $form->{tmpfile} " .
418 "> $form->{tmpfile}.err");
420 $self->{"error"} = $form->cleanup();
425 system("latex --interaction=nonstopmode $form->{tmpfile} " .
426 "> $form->{tmpfile}.err");
428 $self->{"error"} = $form->cleanup();
434 $form->{tmpfile} =~ s/tex$/dvi/;
436 system("dvips $form->{tmpfile} -o -q > /dev/null");
438 $self->{"error"} = "dvips : $!";
442 $form->{tmpfile} =~ s/dvi$/ps/;
450 my ($self, $two_passes) = @_;
451 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
453 # Convert the tex file to PDF
455 if (!chdir("$userspath")) {
456 $self->{"error"} = "chdir : $!";
461 $form->{tmpfile} =~ s/$userspath\///g;
463 system("pdflatex --interaction=nonstopmode $form->{tmpfile} " .
464 "> $form->{tmpfile}.err");
466 $self->{"error"} = $form->cleanup();
472 system("pdflatex --interaction=nonstopmode $form->{tmpfile} " .
473 "> $form->{tmpfile}.err");
475 $self->{"error"} = $form->cleanup();
481 $form->{tmpfile} =~ s/tex$/pdf/;
486 sub get_mime_type() {
489 if ($self->{"form"}->{"format"} =~ /postscript/i) {
490 return "application/postscript";
492 return "application/pdf";
505 package HTMLTemplate;
509 @ISA = qw(LaTeXTemplate);
514 return $type->SUPER::new(@_);
518 my ($self, $variable) = @_;
519 my $form = $self->{"form"};
522 ('order' => ['<', '>', quotemeta("\n")],
525 quotemeta("\n") => '<br>',
528 map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
530 # Allow some HTML markup to be converted into the output format's
531 # corresponding markup code, e.g. bold or italic.
532 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
534 foreach my $key (@markup_replace) {
535 $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
541 sub get_mime_type() {
544 if ($self->{"form"}->{"format"} =~ /postscript/i) {
545 return "application/postscript";
546 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
547 return "application/pdf";
556 if ($self->{"form"}->{"format"} =~ /postscript/i) {
558 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
565 sub convert_to_postscript {
567 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
569 # Convert the HTML file to postscript
571 if (!chdir("$userspath")) {
572 $self->{"error"} = "chdir : $!";
577 $form->{"tmpfile"} =~ s/$userspath\///g;
578 my $psfile = $form->{"tmpfile"};
579 $psfile =~ s/.html/.ps/;
580 if ($psfile eq $form->{"tmpfile"}) {
584 system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
586 $self->{"error"} = $form->cleanup();
591 $form->{"tmpfile"} = $psfile;
600 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
602 # Convert the HTML file to PDF
604 if (!chdir("$userspath")) {
605 $self->{"error"} = "chdir : $!";
610 $form->{"tmpfile"} =~ s/$userspath\///g;
611 my $pdffile = $form->{"tmpfile"};
612 $pdffile =~ s/.html/.pdf/;
613 if ($pdffile eq $form->{"tmpfile"}) {
617 system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
619 $self->{"error"} = $form->cleanup();
624 $form->{"tmpfile"} = $pdffile;
636 package OpenDocumentTemplate;
644 # use File::Temp qw(:mktemp);
647 @ISA = qw(SimpleTemplate);
652 $self = $type->SUPER::new(@_);
654 foreach my $module (qw(Archive::Zip Text::Iconv)) {
655 eval("use ${module};");
657 $self->{"form"}->error("The Perl module '${module}' could not be " .
658 "loaded. Support for OpenDocument templates " .
659 "does not work without it. Please install your " .
660 "distribution's package or get the module from " .
661 "CPAN ( http://www.cpan.org ).");
665 $self->{"rnd"} = int(rand(1000000));
666 $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
671 sub substitute_vars {
672 my ($self, $text, @indices) = @_;
674 my $form = $self->{"form"};
676 while ($text =~ /\<\%(.*?)\%\>/) {
677 my $value = $form->{$1};
679 for (my $i = 0; $i < scalar(@indices); $i++) {
680 last unless (ref($value) eq "ARRAY");
681 $value = $value->[$indices[$i]];
683 substr($text, $-[0], $+[0] - $-[0]) = $self->format_string($value);
690 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
692 my ($form, $new_contents) = ($self->{"form"}, "");
694 my $ary = $form->{$var};
695 for (my $i = 0; $i < scalar(@indices); $i++) {
696 last unless (ref($ary) eq "ARRAY");
697 $ary = $ary->[$indices[$i]];
700 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
701 $form->{"__first__"} = $i == 0;
702 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
703 $form->{"__odd__"} = (($i + 1) % 2) == 1;
704 $form->{"__counter__"} = $i + 1;
705 my $new_text = $self->parse_block($text, (@indices, $i));
706 return undef unless (defined($new_text));
707 $new_contents .= $start_tag . $new_text . $end_tag;
709 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
711 return $new_contents;
715 my ($self, $text, $pos, $var, $not) = @_;
718 $pos = 0 unless ($pos);
720 while ($pos < length($text)) {
723 next if (substr($text, $pos - 1, 5) ne '<%');
725 if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
728 } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
730 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
734 my $block = substr($text, 0, $pos - 1);
735 substr($text, 0, $pos - 1) = "";
736 $text =~ s!^\<\%[^\%]+\%\>!!;
737 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
739 return ($block, $text);
741 } elsif (substr($text, $pos + 4, 3) eq 'end') {
744 my $block = substr($text, 0, $pos - 1);
745 substr($text, 0, $pos - 1) = "";
746 $text =~ s!^\<\%[^\%]+\%\>!!;
748 return ($block, $text);
757 $main::lxdebug->enter_sub();
759 my ($self, $contents, @indices) = @_;
761 my $new_contents = "";
763 while ($contents ne "") {
764 if (substr($contents, 0, 1) eq "<") {
765 $contents =~ m|^<[^>]+>|;
767 substr($contents, 0, length($&)) = "";
769 if ($tag =~ m|<table:table-row|) {
770 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
773 substr($contents, 0, length($1) + length($end_tag)) = "";
775 if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
778 substr($table_row, length($`), length($&)) = "";
780 my ($t1, $t2) = $self->find_end($table_row, length($`));
782 $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
783 $main::lxdebug->leave_sub();
787 my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
788 if (!defined($new_text)) {
789 $main::lxdebug->leave_sub();
792 $new_contents .= $new_text;
795 my $new_text = $self->parse_block($table_row, @indices);
796 if (!defined($new_text)) {
797 $main::lxdebug->leave_sub();
800 $new_contents .= $tag . $new_text . $end_tag;
804 $new_contents .= $tag;
808 $contents =~ /^[^<]+/;
811 my $pos_if = index($text, '<%if');
812 my $pos_foreach = index($text, '<%foreach');
814 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
815 substr($contents, 0, length($text)) = "";
816 $new_contents .= $self->substitute_vars($text, @indices);
820 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
821 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
822 substr($contents, 0, $pos_foreach) = "";
824 if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
825 $self->{"error"} = "Malformed <\%foreach\%>.";
826 $main::lxdebug->leave_sub();
832 substr($contents, 0, length($&)) = "";
835 ($block, $contents) = $self->find_end($contents);
837 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
838 $main::lxdebug->leave_sub();
842 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
843 if (!defined($new_text)) {
844 $main::lxdebug->leave_sub();
847 $new_contents .= $new_text;
850 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
851 substr($contents, 0, $pos_if) = "";
853 if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
854 $self->{"error"} = "Malformed <\%if\%>.";
855 $main::lxdebug->leave_sub();
859 my ($not, $var) = ($1, $2);
861 substr($contents, 0, length($&)) = "";
863 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
865 $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
866 $main::lxdebug->leave_sub();
870 my $value = $self->{"form"}->{$var};
871 for (my $i = 0; $i < scalar(@indices); $i++) {
872 last unless (ref($value) eq "ARRAY");
873 $value = $value->[$indices[$i]];
876 if (($not && !$value) || (!$not && $value)) {
877 my $new_text = $self->parse_block($block, @indices);
878 if (!defined($new_text)) {
879 $main::lxdebug->leave_sub();
882 $new_contents .= $new_text;
888 $main::lxdebug->leave_sub();
890 return $new_contents;
894 $main::lxdebug->enter_sub();
898 my $form = $self->{"form"};
903 if ($form->{"IN"} =~ m|^/|) {
904 $file_name = $form->{"IN"};
906 $file_name = $form->{"templates"} . "/" . $form->{"IN"};
909 my $zip = Archive::Zip->new();
910 if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
911 $self->{"error"} = "File not found/is not a OpenDocument file.";
912 $main::lxdebug->leave_sub();
916 my $contents = $zip->contents("content.xml");
918 $self->{"error"} = "File is not a OpenDocument file.";
919 $main::lxdebug->leave_sub();
923 my $rnd = $self->{"rnd"};
924 my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
925 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
927 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
928 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
930 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
931 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
933 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
934 <style:text-properties style:text-line-through-style="solid"/>
936 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
937 <style:text-properties style:text-position="super 58%"/>
939 <style:style style:name="TLXO${rnd}SUB" style:family="text">
940 <style:text-properties style:text-position="sub 58%"/>
944 $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
945 $contents =~ s|[\n\r]||gm;
947 my $new_contents = $self->parse_block($contents);
948 if (!defined($new_contents)) {
949 $main::lxdebug->leave_sub();
953 # $new_contents =~ s|>|>\n|g;
955 $zip->contents("content.xml", $new_contents);
957 my $styles = $zip->contents("styles.xml");
959 my $new_styles = $self->parse_block($styles);
960 if (!defined($new_contents)) {
961 $main::lxdebug->leave_sub();
964 $zip->contents("styles.xml", $new_styles);
967 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
970 if ($form->{"format"} =~ /pdf/) {
971 $res = $self->convert_to_pdf();
974 $main::lxdebug->leave_sub();
978 sub is_xvfb_running {
979 $main::lxdebug->enter_sub();
984 my $dfname = $self->{"userspath"} . "/xvfb_display";
987 $main::lxdebug->message(LXDebug::DEBUG2, " Looking for $dfname\n");
988 if ((-f $dfname) && open(IN, $dfname)) {
993 my $xauthority = <IN>;
997 $main::lxdebug->message(LXDebug::DEBUG2, " found with $pid and $display\n");
999 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
1000 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #1\n");
1001 unlink($dfname, $xauthority);
1002 $main::lxdebug->leave_sub();
1007 if ($line !~ /xvfb/i) {
1008 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #2\n");
1009 unlink($dfname, $xauthority);
1010 $main::lxdebug->leave_sub();
1014 $ENV{"XAUTHORITY"} = $xauthority;
1015 $ENV{"DISPLAY"} = $display;
1017 $main::lxdebug->message(LXDebug::DEBUG2, " not found\n");
1020 $main::lxdebug->leave_sub();
1026 $main::lxdebug->enter_sub();
1030 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
1032 my $display = $self->is_xvfb_running();
1035 $main::lxdebug->leave_sub();
1040 while ( -f "/tmp/.X${display}-lock") {
1043 $display = ":${display}";
1044 $main::lxdebug->message(LXDebug::DEBUG2, " display $display\n");
1046 my $mcookie = `mcookie`;
1047 die("Installation error: mcookie not found.") if ($? != 0);
1050 $main::lxdebug->message(LXDebug::DEBUG2, " mcookie $mcookie\n");
1052 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
1053 $ENV{"XAUTHORITY"} = $xauthority;
1055 $main::lxdebug->message(LXDebug::DEBUG2, " xauthority $xauthority\n");
1057 system("xauth add \"${display}\" . \"${mcookie}\"");
1059 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
1060 $main::lxdebug->leave_sub();
1064 $main::lxdebug->message(LXDebug::DEBUG2, " about to fork()\n");
1068 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1069 exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
1072 $main::lxdebug->message(LXDebug::DEBUG2, " parent dont sleeping\n");
1075 my $dfname = $self->{"userspath"} . "/xvfb_display";
1076 if (!open(OUT, ">$dfname")) {
1077 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
1078 unlink($xauthority);
1080 $main::lxdebug->leave_sub();
1083 print(OUT "$pid\n$display\n$xauthority\n");
1086 $main::lxdebug->message(LXDebug::DEBUG2, " parent re-testing\n");
1088 if (!$self->is_xvfb_running()) {
1089 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
1090 unlink($xauthority, $dfname);
1092 $main::lxdebug->leave_sub();
1096 $main::lxdebug->message(LXDebug::DEBUG2, " spawn OK\n");
1098 $main::lxdebug->leave_sub();
1103 sub is_openoffice_running {
1104 $main::lxdebug->enter_sub();
1106 system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
1107 "> /dev/null 2> /dev/null");
1109 $main::lxdebug->message(LXDebug::DEBUG2, " is_openoffice_running(): $?\n");
1111 $main::lxdebug->leave_sub();
1116 sub spawn_openoffice {
1117 $main::lxdebug->enter_sub();
1121 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
1123 my ($try, $spawned_oo, $res);
1126 for ($try = 0; $try < 15; $try++) {
1127 if ($self->is_openoffice_running()) {
1135 $main::lxdebug->message(LXDebug::DEBUG2, " Child daemonizing\n");
1137 open(STDIN, '/dev/null');
1138 open(STDOUT, '>/dev/null');
1139 my $new_pid = fork();
1141 my $ssres = setsid();
1142 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1143 my @cmdline = ($main::openofficeorg_writer_bin,
1144 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1146 "-accept=socket,host=localhost,port=" .
1147 $main::openofficeorg_daemon_port . ";urp;");
1151 $main::lxdebug->message(LXDebug::DEBUG2, " Parent after fork\n");
1156 sleep($try >= 5 ? 2 : 1);
1160 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
1161 "OpenOffice could not be started.";
1164 $main::lxdebug->leave_sub();
1169 sub convert_to_pdf {
1170 $main::lxdebug->enter_sub();
1174 my $form = $self->{"form"};
1176 my $filename = $form->{"tmpfile"};
1177 $filename =~ s/.odt$//;
1178 if (substr($filename, 0, 1) ne "/") {
1179 $filename = getcwd() . "/${filename}";
1182 if (substr($self->{"userspath"}, 0, 1) eq "/") {
1183 $ENV{'HOME'} = $self->{"userspath"};
1185 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
1188 if (!$self->spawn_xvfb()) {
1189 $main::lxdebug->leave_sub();
1194 if (!$main::openofficeorg_daemon) {
1195 @cmdline = ($main::openofficeorg_writer_bin,
1196 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1198 "file:${filename}.odt",
1199 "macro://" . (split('/', $filename))[-1] .
1200 "/Standard.Conversion.ConvertSelfToPDF()");
1202 if (!$self->spawn_openoffice()) {
1203 $main::lxdebug->leave_sub();
1207 @cmdline = ("./scripts/oo-uno-convert-pdf.py",
1208 $main::openofficeorg_daemon_port,
1216 $form->{"tmpfile"} =~ s/odt$/pdf/;
1218 unlink($filename . ".odt");
1220 $main::lxdebug->leave_sub();
1225 unlink($filename . ".odt", $filename . ".pdf");
1226 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
1229 $main::lxdebug->leave_sub();
1234 my ($self, $variable) = @_;
1235 my $form = $self->{"form"};
1236 my $iconv = $self->{"iconv"};
1239 ('order' => ['&', '<', '>', '"', "'",
1241 quotemeta("\n"), quotemeta("\r")],
1247 '\x80' => chr(0xa4), # Euro
1248 quotemeta("\n") => '<text:line-break/>',
1249 quotemeta("\r") => '',
1252 map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
1254 # Allow some HTML markup to be converted into the output format's
1255 # corresponding markup code, e.g. bold or italic.
1256 my $rnd = $self->{"rnd"};
1257 my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
1258 "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
1260 foreach my $key (keys(%markup_replace)) {
1261 my $value = $markup_replace{$key};
1262 $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi;
1263 $variable =~ s|\</${key}\>|</text:span>|gi;
1266 return $iconv->convert($variable);
1269 sub get_mime_type() {
1270 if ($self->{"form"}->{"format"} =~ /pdf/) {
1271 return "application/pdf";
1273 return "application/vnd.oasis.opendocument.text";
1277 sub uses_temp_file {