X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;ds=inline;f=SL%2FHelper%2FCreatePDF.pm;h=fa1aafa2e02d67c838f87144f0cd588918eec54c;hb=ba9e2e01d2662182f04da256e0ff7ce258428332;hp=6cffc292524eb3dd97e20741374bd97f7b9eb592;hpb=aa925eed40c22fe731ac70b496812f7e5a38e240;p=kivitendo-erp.git
diff --git a/SL/Helper/CreatePDF.pm b/SL/Helper/CreatePDF.pm
index 6cffc2925..fa1aafa2e 100644
--- a/SL/Helper/CreatePDF.pm
+++ b/SL/Helper/CreatePDF.pm
@@ -6,6 +6,7 @@ use Carp;
use Cwd;
use English qw(-no_match_vars);
use File::Slurp ();
+use File::Spec ();
use File::Temp ();
use List::MoreUtils qw(uniq);
use List::Util qw(first);
@@ -28,30 +29,45 @@ our %EXPORT_TAGS = (
sub create_pdf {
my ($class, %params) = @_;
+ return __PACKAGE__->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 $tmpdir = $form->{tmpdir};
+ 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',
- DIR => $userspath,
+ SUFFIX => ".${suffix}",
+ DIR => $form->{tmpdir},
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;
+ (undef, undef, $form->{template_meta}{tmpfile}) = File::Spec->splitpath($tmpfile);
+
+ my $parser = SL::Template::create(
+ type => ($params{template_type} || 'LaTeX'),
+ source => $form->{IN},
+ form => $form,
+ myconfig => \%::myconfig,
+ userspath => $tmpdir,
+ variable_content_types => $params{variable_content_types},
);
my $result = $parser->parse($temp_fh);
@@ -64,20 +80,26 @@ sub create_pdf {
die $parser->get_error;
}
+ # SL::Template:** modify $form->{tmpfile} by removing its
+ # $form->{userspath} prefix. They also store the final file's actual
+ # file name in $form->{tmpfile} â but it is now relative to
+ # $form->{userspath}. Other modules return the full file nameâ¦
+ 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 = $userspath . '/keep-' . $form->{tmpfile};
- rename $userspath . '/' . $form->{tmpfile}, $new_name;
+ my $new_name = File::Spec->catfile($tmpdir, 'keep-' . $form->{tmpfile});
+ rename $full_file_name, $new_name;
$form->cleanup;
return $new_name;
}
- my $pdf = File::Slurp::read_file($userspath . '/' . $form->{tmpfile});
+ my $content = File::Slurp::read_file($full_file_name);
$form->cleanup;
- return $pdf;
+ return $content;
}
sub merge_pdfs {
@@ -152,3 +174,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 â mandatory. The template file name relative to
+the users' templates directory. Must be an existing file name,
+e.g. one retrieved by L.
+
+=item * C â optional hash reference containing variables
+available to the template.
+
+=item * C â optional scalar containing either C (the
+default) or C. If it is set to C then the file
+name of the temporary file containing the PDF is returned, and the
+caller is responsible for deleting it. Otherwise a scalar containing
+the PDF itself is returned and all temporary files have already been
+deleted by L.
+
+=item * C â optional, defaults to C and determines the
+output format. Can be set to C for HTML output if
+C is set to C as well.
+
+=item * C â optional, defaults to C and
+determines the template's format. Can be set to C for HTML
+output if C is set to C as well.
+
+=back
+
+=item C
+
+Searches the user's templates directory for a template file name to
+use. The file names considered depend on the parameters; they can
+contain a template base name and suffixes for email, language and
+printers. As a fallback the name C is also
+considered.
+
+The return value depends on the context. In scalar context the
+template file name that matches the given parameters is returned. It's
+a file name relative to the user's templates directory. If no template
+file is found then C is returned.
+
+In list context the first element is the same value as in scalar
+context. Additionally a list of considered template file names is
+returned.
+
+The recognized parameters are:
+
+=over 2
+
+=item * C â mandatory. The template's file name basis
+without any additional suffix or extension, e.g. C.
+
+=item * C â optional file name extension to use without the
+dot. Defaults to C.
+
+=item * C â optional flag indicating whether or not the
+template is to be sent via email. If set to true then template file
+names containing C<_email> are considered as well.
+
+=item * C and C â optional parameters
+indicating the language to be used. C can be either a string
+containing the language code to use or an instance of
+C. C can contain the ID of the
+C instance to load and use. If given template file
+names containing C<_language_template_code> are considered as well.
+
+=item * C and C â optional parameters indicating
+the printer to be used. C can be either a string containing
+the printer code to use or an instance of
+C. C can contain the ID of the
+C instance to load and use. If given template file
+names containing C<_printer_template_code> are considered as well.
+
+=back
+
+=item C
+
+Merges two or more PDFs into a single PDF by using the external
+application ghostscript.
+
+The recognized parameters are:
+
+=over 2
+
+=item * C â mandatory array reference containing the file
+names to merge.
+
+=back
+
+Note that this function relies on the presence of the external
+application ghostscript. The executable to use is configured via
+kivitendo's configuration file setting C.
+
+=back
+
+=head1 BUGS
+
+Nothing here yet.
+
+=head1 AUTHOR
+
+Moritz Bunkus Em.bunkus@linet-services.deE
+
+=cut