X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/aa925eed40c22fe731ac70b496812f7e5a38e240..61d88e69bd686dcc00bc3cb0cbfa37bc808cebeb:/SL/Helper/CreatePDF.pm diff --git a/SL/Helper/CreatePDF.pm b/SL/Helper/CreatePDF.pm index 6cffc2925..dda4b0df3 100644 --- a/SL/Helper/CreatePDF.pm +++ b/SL/Helper/CreatePDF.pm @@ -28,30 +28,42 @@ our %EXPORT_TAGS = ( sub create_pdf { my ($class, %params) = @_; + return $class->create_parsed_file( + format => 'pdf', + template_type => 'LaTeX', + %params, + ); +} + +sub create_parsed_file { + my ($class, %params) = @_; + my $userspath = $::lx_office_conf{paths}->{userspath}; + my $vars = $params{variables} || {}; my $form = Form->new(''); - $form->{format} = 'pdf'; + $form->{$_} = $vars->{$_} for keys %{ $vars }; + $form->{format} = lc($params{format} || 'pdf'); $form->{cwd} = getcwd(); $form->{templates} = $::instance_conf->get_templates; - $form->{IN} = $params{template} . '.tex'; + $form->{IN} = $params{template}; $form->{tmpdir} = $form->{cwd} . '/' . $userspath; + my ($suffix) = $params{template} =~ m{\.(.+)}; - my $vars = $params{variables} || {}; - $form->{$_} = $vars->{$_} for keys %{ $vars }; - - my $temp_fh; - ($temp_fh, $form->{tmpfile}) = File::Temp::tempfile( + my ($temp_fh, $tmpfile) = File::Temp::tempfile( 'kivitendo-printXXXXXX', - SUFFIX => '.tex', + SUFFIX => ".${suffix}", 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, + $form->{tmpfile} = $tmpfile; + + my $parser = SL::Template::create( + type => ($params{template_type} || 'LaTeX'), + source => $form->{IN}, + form => $form, + myconfig => \%::myconfig, + userspath => $userspath, ); my $result = $parser->parse($temp_fh); @@ -65,19 +77,19 @@ sub create_pdf { } if (($params{return} || 'content') eq 'file_name') { - my $new_name = $userspath . '/keep-' . $form->{tmpfile}; - rename $userspath . '/' . $form->{tmpfile}, $new_name; + my $new_name = $userspath . '/keep-' . $tmpfile; + rename $tmpfile, $new_name; $form->cleanup; return $new_name; } - my $pdf = File::Slurp::read_file($userspath . '/' . $form->{tmpfile}); + my $content = File::Slurp::read_file($tmpfile); $form->cleanup; - return $pdf; + return $content; } sub merge_pdfs { @@ -152,3 +164,150 @@ sub find_template { } 1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::Helper::CreatePDF - A helper for creating PDFs from template files + +=head1 SYNOPSIS + + # Retrieve a sales order from the database and create a PDF for + # it: + my $order = SL::DB::Order->new(id => …)->load; + my $print_form = Form->new(''); + $print_form->{type} = 'invoice'; + $print_form->{formname} = 'invoice', + $print_form->{format} = 'pdf', + $print_form->{media} = 'file'; + + $order->flatten_to_form($print_form, format_amounts => 1); + $print_form->prepare_for_printing; + + my $pdf = SL::Helper::CreatePDF->create_pdf( + template => 'sales_order', + variables => $print_form, + ); + +=head1 FUNCTIONS + +=over 4 + +=item C + +Parses a LaTeX template file, creates a PDF for it and returns either +its content or its file name. The recognized parameters are the same +as the ones for L with C and +C being pre-set. + +=item C + +Parses a template file and returns either its content or its file +name. The recognized parameters are: + +=over 2 + +=item * C