use Cwd;
use English qw(-no_match_vars);
use File::Slurp ();
-use File::Temp ();
+use File::Spec ();
+use File::Temp ();
+use File::Copy qw(move);
use List::MoreUtils qw(uniq);
use List::Util qw(first);
+use Scalar::Util qw(blessed);
use String::ShellQuote ();
-use SL::Form;
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;
+use SL::X;
use Exporter 'import';
our @EXPORT_OK = qw(create_pdf merge_pdfs find_template);
sub create_pdf {
my ($class, %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};
- $form->{tmpdir} = $form->{cwd} . '/' . $userspath;
- my ($suffix) = $params{template} =~ m{\.(.+)};
+ return __PACKAGE__->create_parsed_file(
+ format => 'pdf',
+ template_type => 'LaTeX',
+ %params,
+ );
+}
- my $vars = $params{variables} || {};
- $form->{$_} = $vars->{$_} for keys %{ $vars };
+sub create_parsed_file {
+ my ($class, %params) = @_;
+
+ 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 $temp_fh;
- ($temp_fh, $form->{tmpfile}) = File::Temp::tempfile(
+ my $vars = $params{variables} || {};
+ my $form = Form->new('');
+ $form->{$_} = $vars->{$_} for keys %{$vars};
+ $form->{format} = lc($params{format} || 'pdf');
+ $form->{cwd} = SL::System::Process::exe_dir();
+ $form->{templates} = $::instance_conf->get_templates;
+ $form->{IN} = $params{template};
+ $form->{tmpdir} = $temp_dir->dirname;
+ my $tmpdir = $form->{tmpdir};
+ my ($suffix) = $params{template} =~ m{\.(.+)};
+
+ my ($temp_fh, $tmpfile) = File::Temp::tempfile(
'kivitendo-printXXXXXX',
SUFFIX => ".${suffix}",
- DIR => $userspath,
- UNLINK => ($::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files})? 0 : 1,
+ DIR => $form->{tmpdir},
+ UNLINK => !$keep_temp_files,
);
- 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 %driver_options;
+ eval {
+ %driver_options = _maybe_attach_zugferd_data($params{record});
+ };
+
+ if (my $e = SL::X::ZUGFeRDValidation->caught) {
+ $form->cleanup;
+ die $e->message;
+ }
+
+ 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},
+ %driver_options,
);
my $result = $parser->parse($temp_fh);
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($userspath, '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;
+}
+
+#
+# Alternativen zu pdfinfo wären (aber wesentlich langamer):
+#
+# gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=/dev/null $filename | grep 'Processing pages'
+# my (undef,undef,undef,undef,$pages) = split / +/,$shell_out;
+#
+# gs -dBATCH -dNOPAUSE -q -dNODISPLAY -c "($filename) (r) file runpdfbegin pdfpagecount = quit"
+# $pages=$shell_out;
+#
+
+sub has_odd_pages {
+ my ($class, $filename) = @_;
+ return 0 unless -f $filename;
+ my $shell_out = `pdfinfo $filename | grep 'Pages:'`;
+ my ($label, $pages) = split / +/, $shell_out;
+ return $pages & 1;
}
sub merge_pdfs {
my ($class, %params) = @_;
-
- return scalar(File::Slurp::read_file($params{file_names}->[0])) if scalar(@{ $params{file_names} }) < 2;
+ my $filecount = scalar(@{ $params{file_names} });
+
+ if ($params{inp_content}) {
+ return $params{inp_content} if $filecount == 0 && !$params{out_path};
+ } elsif ($params{out_path}) {
+ return 0 if $filecount == 0;
+ if ($filecount == 1) {
+ if (!rename($params{file_names}->[0], $params{out_path})) {
+ # special filesystem or cross filesystem etc
+ move($params{file_names}->[0], $params{out_path});
+ }
+ return 1;
+ }
+ } else {
+ return '' if $filecount == 0;
+ return scalar(File::Slurp::read_file($params{file_names}->[0])) if $filecount == 1;
+ }
my ($temp_fh, $temp_name) = File::Temp::tempfile(
'kivitendo-printXXXXXX',
);
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`;
+ my $input_names = '';
+ my $hasodd = 0;
+ my $emptypage = '';
+ if ($params{bothsided}) {
+ $emptypage = $::instance_conf->get_templates . '/emptyPage.pdf';
+ unless (-f $emptypage) {
+ $emptypage = '';
+ delete $params{bothsided};
+ }
+ }
+ if ($params{inp_content}) {
+ my ($temp_fh, $inp_name) = File::Temp::tempfile(
+ 'kivitendo-contentXXXXXX',
+ SUFFIX => '.pdf',
+ DIR => $::lx_office_conf{paths}->{userspath},
+ UNLINK => ($::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files})? 0 : 1,
+ );
+ binmode $temp_fh;
+ print $temp_fh $params{inp_content};
+ close $temp_fh;
+ $input_names = $inp_name . ' ';
+ $hasodd = $params{bothsided} && __PACKAGE__->has_odd_pages($inp_name);
+ }
+ foreach (@{ $params{file_names} }) {
+ $input_names .= $emptypage . ' ' if $hasodd;
+ $input_names .= String::ShellQuote::shell_quote($_) . ' ';
+ $hasodd = $params{bothsided} && __PACKAGE__->has_odd_pages($_);
+ }
+ 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;
+ if ($params{out_path}) {
+ if (!rename($temp_name, $params{out_path})) {
+
+ # special filesystem or cross filesystem etc
+ move($temp_name, $params{out_path});
+ }
+ return 1;
+ }
return scalar File::Slurp::read_file($temp_name);
}
return wantarray ? ($template, @template_files) : $template;
}
+sub _maybe_attach_zugferd_data {
+ my ($record) = @_;
+
+ return if !blessed($record)
+ || !$record->can('customer')
+ || !$record->customer
+ || !$record->can('create_pdf_a_print_options')
+ || !$record->can('create_zugferd_data')
+ || !$record->customer->create_zugferd_invoices_for_this_customer;
+
+ my $xmlfile = File::Temp->new;
+ $xmlfile->print($record->create_zugferd_data);
+ $xmlfile->close;
+
+ my %driver_options = (
+ pdf_a => $record->create_pdf_a_print_options(zugferd_xmp_data => $record->create_zugferd_xmp_data),
+ pdf_attachments => [
+ { source => $xmlfile,
+ name => 'factur-x.xml',
+ description => $::locale->text('Factur-X/ZUGFeRD invoice'),
+ relationship => '/Alternative',
+ mime_type => 'text/xml',
+ }
+ ],
+ );
+
+ return %driver_options;
+}
+
1;
__END__
=item C<create_pdf %params>
Parses a LaTeX template file, creates a PDF for it and returns either
-its content or its file name. The recognized parameters are:
+its content or its file name. The recognized parameters are the same
+as the ones for L</create_parsed_file> with C<format> and
+C<template_type> being pre-set.
+
+=item C<create_parsed_file %params>
+
+Parses a template file and returns either its content or its file
+name. The recognized parameters are:
=over 2
the PDF itself is returned and all temporary files have already been
deleted by L</create_pdf>.
+=item * C<format> – optional, defaults to C<pdf> and determines the
+output format. Can be set to C<html> for HTML output if
+C<template_type> is set to C<HTML> as well.
+
+=item * C<template_type> – optional, defaults to C<LaTeX> and
+determines the template's format. Can be set to C<HTML> for HTML
+output if C<format> is set to C<html> as well.
+
=back
=item C<find_template %params>
Merges two or more PDFs into a single PDF by using the external
application ghostscript.
+Normally the function returns the contents of the resulting PDF.
+if The parameter C<out_path> is set the resulting PDF is in this file
+and the return value is 1 if it successful or 0 if not.
+
The recognized parameters are:
=over 2
=item * C<file_names> – mandatory array reference containing the file
names to merge.
+=item * C<inp_content> – optional, contents of first file to merge with C<file_names>.
+
+=item * C<out_path> – optional, returns not the merged contents but wrote him into this file
+
=back
Note that this function relies on the presence of the external