From e8880baa36cdb39bfcce232762ceb11817bb1082 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Tue, 4 Feb 2014 15:19:46 +0100 Subject: [PATCH] CreatePDF: ein Helfer zum Erzeugen von PDFs von Controllern aus --- SL/Controller/Helper/CreatePDF.pm | 98 +++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 SL/Controller/Helper/CreatePDF.pm diff --git a/SL/Controller/Helper/CreatePDF.pm b/SL/Controller/Helper/CreatePDF.pm new file mode 100644 index 000000000..90b5a6344 --- /dev/null +++ b/SL/Controller/Helper/CreatePDF.pm @@ -0,0 +1,98 @@ +package SL::Controller::Helper::CreatePDF; + +use strict; + +use Cwd; +use English qw(-no_match_vars); +use File::Slurp (); +use File::Temp (); +use String::ShellQuote (); + +use SL::Form; +use SL::Common; +use SL::MoreCommon; +use SL::Template; +use SL::Template::LaTeX; + +use Exporter 'import'; +our @EXPORT = qw(create_pdf merge_pdfs); + +sub create_pdf { + my ($self, %params) = @_; + + my $userspath = $::lx_office_conf{paths}->{userspath}; + my $form = Form->new(''); + $form->{format} = 'pdf'; + $form->{cwd} = getcwd(); + $form->{templates} = $::instance_conf->get_templates; + $form->{IN} = $params{template} . '.tex'; + $form->{tmpdir} = $form->{cwd} . '/' . $userspath; + + my $vars = $params{variables} || {}; + $form->{$_} = $vars->{$_} for keys %{ $vars }; + + my $temp_fh; + ($temp_fh, $form->{tmpfile}) = File::Temp::tempfile( + 'kivitendo-printXXXXXX', + SUFFIX => '.tex', + DIR => $userspath, + UNLINK => ($::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files})? 0 : 1, + ); + + my $parser = SL::Template::LaTeX->new( + $form->{IN}, + $form, + \%::myconfig, + $userspath, + ); + + my $result = $parser->parse($temp_fh); + + close $temp_fh; + chdir $form->{cwd}; + + if (!$result) { + $form->cleanup; + die $parser->get_error; + } + + if (($params{return} || 'content') eq 'file_name') { + my $new_name = $userspath . '/keep-' . $form->{tmpfile}; + rename $userspath . '/' . $form->{tmpfile}, $new_name; + + $form->cleanup; + + return $new_name; + } + + my $pdf = File::Slurp::read_file($userspath . '/' . $form->{tmpfile}); + + $form->cleanup; + + return $pdf; +} + +sub merge_pdfs { + my ($self, %params) = @_; + + return scalar(File::Slurp::read_file($params{file_names}->[0])) if scalar(@{ $params{file_names} }) < 2; + + my ($temp_fh, $temp_name) = File::Temp::tempfile( + 'kivitendo-printXXXXXX', + SUFFIX => '.pdf', + DIR => $::lx_office_conf{paths}->{userspath}, + UNLINK => ($::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files})? 0 : 1, + ); + close $temp_fh; + + my $input_names = join ' ', String::ShellQuote::shell_quote(@{ $params{file_names} }); + my $exe = $::lx_office_conf{applications}->{ghostscript} || 'gs'; + my $output = `$exe -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=${temp_name} ${input_names} 2>&1`; + + die "Executing gs failed: $ERRNO" if !defined $output; + die $output if $? != 0; + + return scalar File::Slurp::read_file($temp_name); +} + +1; -- 2.20.1