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