Revision e8880baa
Von Moritz Bunkus vor mehr als 10 Jahren hinzugefügt
SL/Controller/Helper/CreatePDF.pm | ||
---|---|---|
1 |
package SL::Controller::Helper::CreatePDF; |
|
2 |
|
|
3 |
use strict; |
|
4 |
|
|
5 |
use Cwd; |
|
6 |
use English qw(-no_match_vars); |
|
7 |
use File::Slurp (); |
|
8 |
use File::Temp (); |
|
9 |
use String::ShellQuote (); |
|
10 |
|
|
11 |
use SL::Form; |
|
12 |
use SL::Common; |
|
13 |
use SL::MoreCommon; |
|
14 |
use SL::Template; |
|
15 |
use SL::Template::LaTeX; |
|
16 |
|
|
17 |
use Exporter 'import'; |
|
18 |
our @EXPORT = qw(create_pdf merge_pdfs); |
|
19 |
|
|
20 |
sub create_pdf { |
|
21 |
my ($self, %params) = @_; |
|
22 |
|
|
23 |
my $userspath = $::lx_office_conf{paths}->{userspath}; |
|
24 |
my $form = Form->new(''); |
|
25 |
$form->{format} = 'pdf'; |
|
26 |
$form->{cwd} = getcwd(); |
|
27 |
$form->{templates} = $::instance_conf->get_templates; |
|
28 |
$form->{IN} = $params{template} . '.tex'; |
|
29 |
$form->{tmpdir} = $form->{cwd} . '/' . $userspath; |
|
30 |
|
|
31 |
my $vars = $params{variables} || {}; |
|
32 |
$form->{$_} = $vars->{$_} for keys %{ $vars }; |
|
33 |
|
|
34 |
my $temp_fh; |
|
35 |
($temp_fh, $form->{tmpfile}) = File::Temp::tempfile( |
|
36 |
'kivitendo-printXXXXXX', |
|
37 |
SUFFIX => '.tex', |
|
38 |
DIR => $userspath, |
|
39 |
UNLINK => ($::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files})? 0 : 1, |
|
40 |
); |
|
41 |
|
|
42 |
my $parser = SL::Template::LaTeX->new( |
|
43 |
$form->{IN}, |
|
44 |
$form, |
|
45 |
\%::myconfig, |
|
46 |
$userspath, |
|
47 |
); |
|
48 |
|
|
49 |
my $result = $parser->parse($temp_fh); |
|
50 |
|
|
51 |
close $temp_fh; |
|
52 |
chdir $form->{cwd}; |
|
53 |
|
|
54 |
if (!$result) { |
|
55 |
$form->cleanup; |
|
56 |
die $parser->get_error; |
|
57 |
} |
|
58 |
|
|
59 |
if (($params{return} || 'content') eq 'file_name') { |
|
60 |
my $new_name = $userspath . '/keep-' . $form->{tmpfile}; |
|
61 |
rename $userspath . '/' . $form->{tmpfile}, $new_name; |
|
62 |
|
|
63 |
$form->cleanup; |
|
64 |
|
|
65 |
return $new_name; |
|
66 |
} |
|
67 |
|
|
68 |
my $pdf = File::Slurp::read_file($userspath . '/' . $form->{tmpfile}); |
|
69 |
|
|
70 |
$form->cleanup; |
|
71 |
|
|
72 |
return $pdf; |
|
73 |
} |
|
74 |
|
|
75 |
sub merge_pdfs { |
|
76 |
my ($self, %params) = @_; |
|
77 |
|
|
78 |
return scalar(File::Slurp::read_file($params{file_names}->[0])) if scalar(@{ $params{file_names} }) < 2; |
|
79 |
|
|
80 |
my ($temp_fh, $temp_name) = File::Temp::tempfile( |
|
81 |
'kivitendo-printXXXXXX', |
|
82 |
SUFFIX => '.pdf', |
|
83 |
DIR => $::lx_office_conf{paths}->{userspath}, |
|
84 |
UNLINK => ($::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files})? 0 : 1, |
|
85 |
); |
|
86 |
close $temp_fh; |
|
87 |
|
|
88 |
my $input_names = join ' ', String::ShellQuote::shell_quote(@{ $params{file_names} }); |
|
89 |
my $exe = $::lx_office_conf{applications}->{ghostscript} || 'gs'; |
|
90 |
my $output = `$exe -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=${temp_name} ${input_names} 2>&1`; |
|
91 |
|
|
92 |
die "Executing gs failed: $ERRNO" if !defined $output; |
|
93 |
die $output if $? != 0; |
|
94 |
|
|
95 |
return scalar File::Slurp::read_file($temp_name); |
|
96 |
} |
|
97 |
|
|
98 |
1; |
Auch abrufbar als: Unified diff
CreatePDF: ein Helfer zum Erzeugen von PDFs von Controllern aus