Release 3.6.1
[kivitendo-erp.git] / SL / Template / HTML.pm
1 package SL::Template::HTML;
2
3 use parent qw(SL::Template::LaTeX);
4
5 use strict;
6
7 sub new {
8   my $type = shift;
9
10   return $type->SUPER::new(@_);
11 }
12
13 sub format_string {
14   my ($self, $variable) = @_;
15   my $form = $self->{"form"};
16
17   $variable = $main::locale->quote_special_chars('Template/HTML', $variable);
18
19   # Allow some HTML markup to be converted into the output format's
20   # corresponding markup code, e.g. bold or italic.
21   my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
22
23   foreach my $key (@markup_replace) {
24     $variable =~ s/\&lt;(\/?)${key}\&gt;/<$1${key}>/g;
25   }
26
27   return $variable;
28 }
29
30 sub get_mime_type() {
31   my ($self) = @_;
32
33   if ($self->{"form"}->{"format"} =~ /postscript/i) {
34     return "application/postscript";
35   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
36     return "application/pdf";
37   } else {
38     return "text/html";
39   }
40 }
41
42 sub uses_temp_file {
43   my ($self) = @_;
44
45   if ($self->{"form"}->{"format"} =~ /postscript/i) {
46     return 1;
47   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
48     return 1;
49   } else {
50     return 0;
51   }
52 }
53
54 sub convert_to_postscript {
55   my ($self) = @_;
56   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
57
58   # Convert the HTML file to postscript
59
60   if (!chdir("$userspath")) {
61     $self->{"error"} = "chdir : $!";
62     $self->cleanup();
63     return 0;
64   }
65
66   $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
67   my $psfile = $form->{"tmpfile"};
68   $psfile =~ s/.html/.ps/;
69   if ($psfile eq $form->{"tmpfile"}) {
70     $psfile .= ".ps";
71   }
72
73   if (system($::lx_office_conf{applications}->{html2ps} . " -f html2ps-config < $form->{tmpfile} > $psfile") == -1) {
74     die "system call to $::lx_office_conf{applications}->{html2ps} failed: $!";
75   }
76   if ($?) {
77     $self->{"error"} = $form->cleanup($::lx_office_conf{applications}->{html2ps});
78     return 0;
79   }
80
81   $form->{"tmpfile"} = $psfile;
82
83   $self->cleanup();
84
85   return 1;
86 }
87
88 sub convert_to_pdf {
89   my ($self) = @_;
90   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
91
92   # Convert the HTML file to PDF
93
94   if (!chdir("$userspath")) {
95     $self->{"error"} = "chdir : $!";
96     $self->cleanup();
97     return 0;
98   }
99
100   $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
101   my $pdffile = $form->{"tmpfile"};
102   $pdffile =~ s/.html/.pdf/;
103   if ($pdffile eq $form->{"tmpfile"}) {
104     $pdffile .= ".pdf";
105   }
106
107   if (system($::lx_office_conf{applications}->{html2ps} . " -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile") == -1) {
108     die "system call to $::lx_office_conf{applications}->{html2ps} failed: $!";
109   }
110   if ($?) {
111     $self->{"error"} = $form->cleanup($::lx_office_conf{applications}->{html2ps});
112     return 0;
113   }
114
115   $form->{"tmpfile"} = $pdffile;
116
117   $self->cleanup();
118
119   return 1;
120 }
121
122 1;