File/Pdf.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« File/Pcap.pm ↑ Summary File/Psv.pm »
1 #
2 # $Id$
3 #
4 # file::pdf Brik
5 #
6 package Metabrik::File::Pdf;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::Shell::Command);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 },
20 attributes_default => {
21 },
22 commands => {
23 secure => [ qw(pdf password) ],
24 to_png => [ qw(pdf) ],
25 from_png => [ qw(glob output) ],
26 from_pdf_to_image_pdf => [ qw(pdf secure|OPTIONAL) ],
27 },
28 require_modules => {
29 'Metabrik::String::Password' => [],
30 'Metabrik::System::File' => [],
31 },
32 require_binaries => {
33 pdftk => [], # snap install pdftk
34 pdftoppm => [],
35 convert => [],
36 },
37 need_packages => {
38 ubuntu => [ qw(poppler-utils imagemagick) ], # pdftoppm, convert
39 debian => [ qw(poppler-utils imagemagick) ], # pdftoppm, convert
40 kali => [ qw(poppler-utils imagemagick) ], # pdftoppm, convert
41 },
42 };
43 }
44  
45 #
46 # Secures a PDF by forbidding: printing, copy/paste and related things.
47 #
48 # Example:
49 #
50 # run string::password generate
51 # my $pass = $RUN->[3]
52 # ls *.pdf
53 # run file::pdf secure $RUN $pass
54 #
55 sub secure {
56 my $self = shift;
57 my ($pdf, $password) = @_;
58  
59 $self->brik_help_run_undef_arg('secure', $pdf) or return;
60 $self->brik_help_run_undef_arg('secure', $password) or return;
61  
62 if (ref($pdf) ne 'ARRAY') {
63 $pdf = [ $pdf ];
64 }
65  
66 my @secure = ();
67 for my $this (@$pdf) {
68 if (! -f $this) {
69 $self->log->warning("secure: file not found [$this], skipping");
70 next;
71 }
72 my $secure = $this;
73 $secure =~ s{\.pdf$}{\.secure\.pdf};
74 $self->system("pdftk \"$this\" output \"$secure\" owner_pw $password");
75 push @secure, $secure;
76 }
77  
78 return \@secure;
79 }
80  
81 sub to_png {
82 my $self = shift;
83 my ($pdf) = @_;
84  
85 $self->brik_help_run_undef_arg('to_png', $pdf) or return;
86 $self->brik_help_run_file_not_found('to_png', $pdf) or return;
87  
88 my $output = $pdf;
89 $output =~ s{\.pdf$}{};
90 $self->system("pdftoppm \"$pdf\" \"$output\" -png");
91  
92 my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
93 my $png = $sf->glob("*.png");
94  
95 return $png;
96 }
97  
98 sub from_png {
99 my $self = shift;
100 my ($glob, $output) = @_;
101  
102 $self->brik_help_run_undef_arg('from_png', $glob) or return;
103 $self->brik_help_run_undef_arg('from_png', $output) or return;
104  
105 $self->system("convert $glob \"$output\"");
106  
107 return $output;
108 }
109  
110 sub from_pdf_to_image_pdf {
111 my $self = shift;
112 my ($pdf, $secure) = @_;
113  
114 $self->brik_help_run_undef_arg('from_pdf_to_image_pdf', $pdf) or return;
115 $self->brik_help_run_file_not_found('from_pdf_to_image_pdf', $pdf)
116 or return;
117  
118 my $sp = Metabrik::String::Password->new_from_brik_init($self) or return;
119 my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
120  
121 my $png = $self->to_png($pdf) or return;
122  
123 my $output = $pdf;
124 $output =~ s{\.pdf$}{.image.pdf};
125  
126 my $png_list = '';
127 for (@$png) {
128 $png_list .= "\"$_\" ";
129 }
130  
131 $self->system("convert $png_list \"$output\"");
132  
133 $sf->remove($png);
134  
135 if ($secure) {
136 my $list = $sp->generate;
137 my $pass = $list->[3];
138 $output = $self->secure($output, $pass);
139 return [ $output, $pass ];
140 }
141  
142 return $output;
143 }
144  
145 1;
146  
147 __END__
« File/Pcap.pm ↑ Summary File/Psv.pm »