Skip to content

Commit 5f3dbb3

Browse files
author
Ian Walls
committed
Merge branch 'issue378'
2 parents b2394a8 + 451a9a3 commit 5f3dbb3

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

docker/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ RUN install_packages \
4444
# Print uploads filetype detection
4545
shared-mime-info \
4646
# For cli access to the database
47-
mariadb-client
47+
mariadb-client \
48+
# for https support in PDF conversion services
49+
liblwp-protocol-https-perl
4850

4951
RUN sed -i '/disable ghostscript format types/,+6d' /etc/ImageMagick-6/policy.xml
5052

lib/Libki/Controller/Public.pm

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ use namespace::autoclean;
55

66
use Libki::Utils::Printing;
77

8+
use LWP::UserAgent;
9+
use LWP::Protocol::https;
10+
use HTTP::Request;
11+
use HTTP::Request::Common;
12+
use Data::Dumper;
13+
14+
{
15+
package Libki::PDFAutoConvert;
16+
sub filename { shift->{_filename} }
17+
sub type { shift->{_type} }
18+
sub decoded_slurp { shift->{_content} }
19+
}
20+
821
BEGIN {extends 'Catalyst::Controller'; }
922

1023
=head1 NAME
@@ -51,7 +64,8 @@ sub upload_print_file :Local :Args(0) {
5164
my $printer_id = $c->req->params->{printer_id};
5265

5366
my $mime = $print_file->mimetype;
54-
my $ext = $print_file->extension;
67+
my ($ext) = $print_file->filename =~ /\.([^.]+)$/;
68+
$ext = lc $ext if defined $ext;
5569

5670
if ( $mime eq "application/pdf" && $ext eq "pdf" ) {
5771
Libki::Utils::Printing::create_print_job_and_file( $c, {
@@ -64,6 +78,38 @@ sub upload_print_file :Local :Args(0) {
6478
} );
6579

6680
$c->response->redirect( $c->uri_for('/public/printing') );
81+
} elsif ($ext ~~ $c->get_printer_configuration->{'pdf_conversion_service'}->{'supported_extensions'} ) {
82+
my $ua = LWP::UserAgent->new(timeout => 600);
83+
my $service_url = $c->get_printer_configuration->{'pdf_conversion_service'}->{'service_url'};
84+
85+
my $request = HTTP::Request::Common::POST(
86+
$service_url,
87+
Content_Type => 'form-data',
88+
Content => [
89+
file => [ $print_file->tempname, $print_file->filename, $print_file->type ]
90+
]
91+
);
92+
my $res = $ua->request($request);
93+
if ($res->is_success) {
94+
my $pdf_data = $res->decoded_content(charset => 'none');
95+
my $converted_upload = bless {
96+
_filename => $print_file->filename . ".pdf",
97+
_type => 'application/pdf',
98+
_content => $pdf_data,
99+
}, 'Libki::PDFAutoConvert';
100+
Libki::Utils::Printing::create_print_job_and_file( $c, {
101+
client_name => Libki::Utils::Printing::PRINT_FROM_WEB,
102+
copies => 1,
103+
print_file => $converted_upload,
104+
printer_id => $printer_id,
105+
user => $user,
106+
username => $user->username,
107+
} );
108+
$c->response->redirect( $c->uri_for( '/public/printing', undef, { success => 'PDF_CONV_SUCCESS' } ));
109+
} else {
110+
$c->response->redirect( $c->uri_for( '/public/printing', undef, { error => 'PDF_CONV_ERROR' } ));
111+
# print Dumper $res;
112+
}
67113
} else {
68114
$c->response->redirect( $c->uri_for( '/public/printing', undef, { error => 'INVALID_FILETYPE' } ));
69115
}

root/dynamic/includes/index/printing.tt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
11
[%- SET public_print_release = c.get_printer_configuration.public_print_release %]
2+
[%- SET pdf_conversion_service = c.get_printer_configuration.pdf_conversion_service %]
23
<div class="container">
34
<div class="row">
45
<div class="col">
56
<fieldset class="">
67
<legend class="">[% c.loc("Upload file to print") %]</legend>
78
[% IF c.req.params.error == "INVALID_FILETYPE" %]
89
<div class="alert alert-danger" role="alert">
9-
[% c.loc('Filetype was invalid, file uploads must be PDF.') %]
10+
[% c.loc('Filetype was invalid; PDFs work best.') %]
1011
[% c.loc('If your file is not a PDF, you may use a PDF printer such as <a target="_blank" class="alert-link" href="https://www.cutepdf.com/Products/CutePDF/writer.asp">CutePDF</a>,') %]
1112
[% c.loc('or a <a target="_blank" class="alert-link" href="https://www.freepdfconvert.com/">web based PDF converter</a>.') %]
1213
</div>
14+
[% ELSIF c.req.params.success == "PDF_CONV_SUCCESS" %]
15+
<div class="alert alert-success" role="alert">
16+
[% c.loc('File successfully converted to PDF and ready to print!') %]
17+
</div>
18+
[% ELSIF c.req.params.error == "PDF_CONV_ERROR" %]
19+
<div class="alert alert-danger" role="alert">
20+
[% c.loc('Something went wrong with the automatic PDF conversion process.') %]
21+
</div>
1322
[% ELSE %]
23+
[% IF pdf_conversion_service %]
24+
<div class="alert alert-warning" role="alert">
25+
[% c.loc('<strong>PDFs work best</strong>; non-PDF printing is currently experimental. Best of luck!') %]
26+
[% c.loc('Currently supporting: ') %]
27+
[% c.get_printer_configuration.pdf_conversion_service.supported_extensions.join(", ") %].
28+
</div>
29+
[% ELSE %]
1430
<div class="alert alert-info" role="alert">
1531
[% c.loc('If your file is not a PDF, you may use a PDF printer such as <a target="_blank" class="alert-link" href="https://www.cutepdf.com/Products/CutePDF/writer.asp">CutePDF</a>,') %]
1632
[% c.loc('or a <a target="_blank" class="alert-link" href="https://www.freepdfconvert.com/">web based PDF converter</a>.') %]
1733
</div>
34+
[% END %]
1835
[% END %]
1936
<form action="[% c.uri_for('upload_print_file') %]" method="post" enctype="multipart/form-data">
2037
<div class="form-group">
@@ -23,7 +40,7 @@
2340
<span class="input-group-text" id="inputGroupFileAddon01">[% c.loc("Upload") %]</span>
2441
</div>
2542
<div class="custom-file">
26-
<input type="file" accept="application/pdf" class="form-control custom-file-input" name="print_file" id="print_file"
43+
<input type="file" accept="application/pdf[% IF pdf_conversion_service %],.[% c.get_printer_configuration.pdf_conversion_service.supported_extensions.join(",.") %][% END %]" class="form-control custom-file-input" name="print_file" id="print_file"
2744
aria-describedby="inputGroupFileAddon01" required/>
2845
<label class="custom-file-label" for="inputGroupFile01">[% c.loc("Choose file") %]</label>
2946
</div>

0 commit comments

Comments
 (0)