From f08036d731f13eb80ac63b951b1c9da77693867a Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Mon, 18 Apr 2016 15:19:37 +0200 Subject: [PATCH] =?utf8?q?SL::DB::Printer:=20=C2=BBDokument=20an=20Drucker?= =?utf8?q?=20schicken=C2=AB=20zentralisiert?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Die neue Funktion print_document übernimmt das Spawnen des externen Prozesses und schickt das Dokument an den Drucker. Das Dokument kann entweder direkt als Inhalt oder als zu sendender Dateiname übergeben werden. --- .../MassRecordCreationAndPrinting.pm | 14 ++-- SL/Controller/MassInvoiceCreatePrint.pm | 7 +- SL/Controller/Order.pm | 18 ++--- SL/DB/Printer.pm | 65 +++++++++++++++++++ 4 files changed, 74 insertions(+), 30 deletions(-) diff --git a/SL/BackgroundJob/MassRecordCreationAndPrinting.pm b/SL/BackgroundJob/MassRecordCreationAndPrinting.pm index bd8783849..e4c75b812 100644 --- a/SL/BackgroundJob/MassRecordCreationAndPrinting.pm +++ b/SL/BackgroundJob/MassRecordCreationAndPrinting.pm @@ -185,16 +185,10 @@ sub print_pdfs { foreach my $local_printer_id ($printer_id, $copy_printer_id) { next unless $local_printer_id; - my $printer = SL::DB::Printer->new(id => $local_printer_id)->load; - my $command = SL::Template::create(type => 'ShellCommand', form => Form->new(''))->parse($printer->printer_command); - if (!open $out, '|-', $command) { - push @{ $data->{print_errors} }, { message => $::locale->text('Could not execute printer command: #1', $!) }; - $job_obj->update_attributes(data_as_hash => $data); - return; - } - binmode $out; - print $out $self->{merged_pdf}; - close $out; + SL::DB::Printer + ->new(id => $local_printer_id) + ->load + ->print_document(content => $self->{merged_pdf}); } } diff --git a/SL/Controller/MassInvoiceCreatePrint.pm b/SL/Controller/MassInvoiceCreatePrint.pm index cd96a73e7..c65d3b5ec 100644 --- a/SL/Controller/MassInvoiceCreatePrint.pm +++ b/SL/Controller/MassInvoiceCreatePrint.pm @@ -325,12 +325,7 @@ sub download_or_print_documents { } my $printer = SL::DB::Printer->new(id => $params{printer_id})->load; - my $command = SL::Template::create(type => 'ShellCommand', form => Form->new(''))->parse($printer->printer_command); - - open my $out, '|-', $command or die $!; - binmode $out; - print $out $merged_pdf; - close $out; + $printer->print_document(content => $merged_pdf); flash_later('info', t8('The documents have been sent to the printer \'#1\'.', $printer->printer_description)); return $self->redirect_to(action => 'list_invoices', printer_id => $params{printer_id}); diff --git a/SL/Controller/Order.pm b/SL/Controller/Order.pm index 9b5dee36d..8ed5b3326 100644 --- a/SL/Controller/Order.pm +++ b/SL/Controller/Order.pm @@ -182,20 +182,10 @@ sub action_print { } elsif ($media eq 'printer') { # printer my $printer_id = $::form->{print_options}->{printer_id}; - my $printer; - $printer = SL::DB::Printer->new(id => $printer_id)->load if $printer_id; - if (!$printer) { - return $self->js->flash('error', t8('Printer not found.'))->render; - } - - my $command = SL::Template::create(type => 'ShellCommand', form => Form->new(''))->parse($printer->printer_command); - - for my $i (1 .. $copies) { - open my $out, '|-', $command or die $!; - binmode $out; - print $out $pdf; - close $out; - } + SL::DB::Printer->new(id => $printer_id)->load->print_document( + copies => $copies, + content => $pdf, + ); $self->js->flash('info', t8('The PDF has been printed')); } diff --git a/SL/DB/Printer.pm b/SL/DB/Printer.pm index 046027bc5..9f76c0095 100644 --- a/SL/DB/Printer.pm +++ b/SL/DB/Printer.pm @@ -2,6 +2,8 @@ package SL::DB::Printer; use strict; +use Carp; + use SL::DB::MetaSetup::Printer; use SL::DB::Manager::Printer; use SL::DB::Helper::Util; @@ -23,4 +25,67 @@ sub validate { return @errors; } +sub print_document { + my ($self, %params) = @_; + + croak "Need either a 'content' or a 'file_name' parameter" if !defined($params{content}) && !$params{file_name}; + + my $copies = $params{copies} || 1; + my $command = SL::Template::create(type => 'ShellCommand', form => Form->new(''))->parse($self->printer_command); + my $content = $params{content} // scalar(File::Slurp::read_file($params{file_name})); + + for (1..$copies) { + open my $out, '|-', $command or die $!; + binmode $out; + print $out $content; + close $out; + } +} + 1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::DB::Printer - Rose model for database table printers + +=head1 SYNOPSIS + + my $printer = SL::DB::Printer->new(id => 4711)->load; + $printer->print_document( + copies => 2, + file_name => '/path/to/file.pdf', + ); + +=head1 FUNCTIONS + +=over 4 + +=item C + +Prints a document by spawning the external command stored in +C<$self-Eprinter_command> and sending content to it. + +The caller must provide either the content to send to the printer +(parameter C) or a name to a file whose content is sent +verbatim (parameter C). + +An optional parameter C can be given to specify the number of +copies to print. This is done by invoking the print command multiple +times. The number of copies defaults to 1. + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut -- 2.20.1