From: Moritz Bunkus Date: Tue, 5 Nov 2019 14:07:24 +0000 (+0100) Subject: PDF-Erzeugung: alles in temporärem Unterverzeichnis von users erledigen X-Git-Tag: release-3.5.6.1~245^2~35 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=dc8ffeaa1211987d6b3e0a3f1d2c576c82584d37;p=kivitendo-erp.git PDF-Erzeugung: alles in temporärem Unterverzeichnis von users erledigen Das vermeidet Kollisionen, wenn Dateien mit festem Namen wie `pdfa.xmp` für PDF/A erzeugt werden müssen. --- diff --git a/SL/Controller/RequirementSpec.pm b/SL/Controller/RequirementSpec.pm index 875f17073..6ba1c4e74 100644 --- a/SL/Controller/RequirementSpec.pm +++ b/SL/Controller/RequirementSpec.pm @@ -23,6 +23,7 @@ use SL::DB::RequirementSpec; use SL::Helper::CreatePDF qw(); use SL::Helper::Flash; use SL::Locale::String; +use SL::System::Process; use SL::Template::LaTeX; use Rose::Object::MakeMethods::Generic @@ -217,9 +218,16 @@ sub action_revert_to { sub action_create_pdf { my ($self, %params) = @_; + my $keep_temp_files = $::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files}; + my $temp_dir = File::Temp->newdir( + "kivitendo-print-XXXXXX", + DIR => SL::System::Process::exe_dir() . "/" . $::lx_office_conf{paths}->{userspath}, + CLEANUP => !$keep_temp_files, + ); + my $base_name = $self->requirement_spec->type->template_file_name || 'requirement_spec'; - my @pictures = $self->prepare_pictures_for_printing; - my %result = SL::Template::LaTeX->parse_and_create_pdf("${base_name}.tex", SELF => $self, rspec => $self->requirement_spec); + my @pictures = $self->prepare_pictures_for_printing($temp_dir->dirname); + my %result = SL::Template::LaTeX->parse_and_create_pdf("${base_name}.tex", SELF => $self, rspec => $self->requirement_spec, userspath => $temp_dir->dirname); unlink @pictures unless ($::lx_office_conf{debug} || {})->{keep_temp_files}; @@ -599,11 +607,11 @@ sub render_first_pasted_section_as_list { } sub prepare_pictures_for_printing { - my ($self) = @_; + my ($self, $userspath) = @_; my @files; - my $userspath = File::Spec->rel2abs($::lx_office_conf{paths}->{userspath}); - my $target = "${userspath}/kivitendo-print-requirement-spec-picture-" . Common::unique_id() . '-'; + $userspath ||= SL::System::Process::exe_dir() . "/" . $::lx_office_conf{paths}->{userspath}; + my $target = "${userspath}/kivitendo-print-requirement-spec-picture-" . Common::unique_id() . '-'; foreach my $picture (map { @{ $_->pictures } } @{ $self->requirement_spec->text_blocks }) { my $output_file_name = $target . $picture->id . '.' . $picture->get_default_file_name_extension; diff --git a/SL/Form.pm b/SL/Form.pm index 097b65d8d..e81435136 100644 --- a/SL/Form.pm +++ b/SL/Form.pm @@ -47,6 +47,7 @@ use CGI; use Cwd; use Encode; use File::Copy; +use File::Temp (); use IO::File; use Math::BigInt; use POSIX qw(strftime); @@ -909,11 +910,18 @@ sub parse_template { local (*IN, *OUT); - my $defaults = SL::DB::Default->get; - my $userspath = $::lx_office_conf{paths}->{userspath}; + my $defaults = SL::DB::Default->get; - $self->{"cwd"} = getcwd(); - $self->{"tmpdir"} = $self->{cwd} . "/${userspath}"; + my $keep_temp_files = $::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files}; + $self->{cwd} = getcwd(); + my $temp_dir = File::Temp->newdir( + "kivitendo-print-XXXXXX", + DIR => $self->{cwd} . "/" . $::lx_office_conf{paths}->{userspath}, + CLEANUP => !$keep_temp_files, + ); + + my $userspath = File::Spec->abs2rel($temp_dir->dirname); + $self->{tmpdir} = $temp_dir->dirname; my $ext_for_format; @@ -973,7 +981,6 @@ sub parse_template { # OUT is used for the media, screen, printer, email # for postscript we store a copy in a temporary file - my $keep_temp_files = $::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files}; my ($temp_fh, $suffix); $suffix = $self->{IN}; diff --git a/SL/Helper/CreatePDF.pm b/SL/Helper/CreatePDF.pm index 13d517460..6a570fa92 100644 --- a/SL/Helper/CreatePDF.pm +++ b/SL/Helper/CreatePDF.pm @@ -17,6 +17,7 @@ use SL::Common; use SL::DB::Language; use SL::DB::Printer; use SL::MoreCommon; +use SL::System::Process; use SL::Template; use SL::Template::LaTeX; @@ -39,15 +40,22 @@ sub create_pdf { sub create_parsed_file { my ($class, %params) = @_; - my $userspath = $::lx_office_conf{paths}->{userspath}; + my $keep_temp_files = $::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files}; + my $userspath = SL::System::Process::exe_dir() . "/" . $::lx_office_conf{paths}->{userspath}; + my $temp_dir = File::Temp->newdir( + "kivitendo-print-XXXXXX", + DIR => $userspath, + CLEANUP => !$keep_temp_files, + ); + my $vars = $params{variables} || {}; my $form = Form->new(''); $form->{$_} = $vars->{$_} for keys %{$vars}; $form->{format} = lc($params{format} || 'pdf'); - $form->{cwd} = getcwd(); + $form->{cwd} = SL::System::Process::exe_dir(); $form->{templates} = $::instance_conf->get_templates; $form->{IN} = $params{template}; - $form->{tmpdir} = $form->{cwd} . '/' . $userspath; + $form->{tmpdir} = $temp_dir->dirname; my $tmpdir = $form->{tmpdir}; my ($suffix) = $params{template} =~ m{\.(.+)}; @@ -55,7 +63,7 @@ sub create_parsed_file { 'kivitendo-printXXXXXX', SUFFIX => ".${suffix}", DIR => $form->{tmpdir}, - UNLINK => ($::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files})? 0 : 1, + UNLINK => !$keep_temp_files, ); $form->{tmpfile} = $tmpfile; @@ -87,7 +95,7 @@ sub create_parsed_file { my ($volume, $directory, $file_name) = File::Spec->splitpath($form->{tmpfile}); my $full_file_name = File::Spec->catfile($tmpdir, $file_name); if (($params{return} || 'content') eq 'file_name') { - my $new_name = File::Spec->catfile($tmpdir, 'keep-' . $form->{tmpfile}); + my $new_name = File::Spec->catfile($userspath, 'keep-' . $form->{tmpfile}); rename $full_file_name, $new_name; $form->cleanup; diff --git a/SL/Template/LaTeX.pm b/SL/Template/LaTeX.pm index d4b336b88..8b4c4e865 100644 --- a/SL/Template/LaTeX.pm +++ b/SL/Template/LaTeX.pm @@ -530,7 +530,7 @@ sub _texinputs_path { my $exe_dir = SL::System::Process::exe_dir(); $templates_path = $exe_dir . '/' . $templates_path unless $templates_path =~ m{^/}; - return join(':', grep({ $_ } ('.', $exe_dir . '/texmf', $templates_path, $ENV{TEXINPUTS})), ''); + return join(':', grep({ $_ } ('.', $exe_dir . '/texmf', $exe_dir . '/users', $templates_path, $ENV{TEXINPUTS})), ''); } sub convert_to_postscript { @@ -652,11 +652,12 @@ sub uses_temp_file { sub parse_and_create_pdf { my ($class, $template_file_name, %params) = @_; + my $userspath = delete($params{userspath}) || $::lx_office_conf{paths}->{userspath}; my $keep_temp = $::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files}; my ($tex_fh, $tex_file_name) = File::Temp::tempfile( 'kivitendo-printXXXXXX', SUFFIX => '.tex', - DIR => $::lx_office_conf{paths}->{userspath}, + DIR => $userspath, UNLINK => $keep_temp ? 0 : 1,, ); @@ -665,7 +666,7 @@ sub parse_and_create_pdf { my $local_form = Form->new(''); $local_form->{cwd} = $old_wd; $local_form->{IN} = $template_file_name; - $local_form->{tmpdir} = $::lx_office_conf{paths}->{userspath}; + $local_form->{tmpdir} = $userspath; $local_form->{tmpfile} = $tex_file_name; $local_form->{templates} = SL::DB::Default->get->templates; @@ -676,7 +677,7 @@ sub parse_and_create_pdf { my $error; eval { - my $template = SL::Template::LaTeX->new(file_name => $template_file_name, form => $local_form); + my $template = SL::Template::LaTeX->new(file_name => $template_file_name, form => $local_form, userspath => $userspath); my $result = $template->parse($tex_fh) && $template->convert_to_pdf; die $template->{error} unless $result;