line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id$ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# file::pdf Brik |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Metabrik::File::Pdf; |
7
|
1
|
|
|
1
|
|
776
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
29
|
|
8
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
27
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
5
|
use base qw(Metabrik::Shell::Command); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
892
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub brik_properties { |
13
|
|
|
|
|
|
|
return { |
14
|
0
|
|
|
0
|
1
|
|
revision => '$Revision$', |
15
|
|
|
|
|
|
|
tags => [ qw(unstable) ], |
16
|
|
|
|
|
|
|
author => 'GomoR ', |
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
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
57
|
0
|
|
|
|
|
|
my ($pdf, $password) = @_; |
58
|
|
|
|
|
|
|
|
59
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('secure', $pdf) or return; |
60
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('secure', $password) or return; |
61
|
|
|
|
|
|
|
|
62
|
0
|
0
|
|
|
|
|
if (ref($pdf) ne 'ARRAY') { |
63
|
0
|
|
|
|
|
|
$pdf = [ $pdf ]; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
my @secure = (); |
67
|
0
|
|
|
|
|
|
for my $this (@$pdf) { |
68
|
0
|
0
|
|
|
|
|
if (! -f $this) { |
69
|
0
|
|
|
|
|
|
$self->log->warning("secure: file not found [$this], skipping"); |
70
|
0
|
|
|
|
|
|
next; |
71
|
|
|
|
|
|
|
} |
72
|
0
|
|
|
|
|
|
my $secure = $this; |
73
|
0
|
|
|
|
|
|
$secure =~ s{\.pdf$}{\.secure\.pdf}; |
74
|
0
|
|
|
|
|
|
$self->system("pdftk \"$this\" output \"$secure\" owner_pw $password"); |
75
|
0
|
|
|
|
|
|
push @secure, $secure; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
return \@secure; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub to_png { |
82
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
83
|
0
|
|
|
|
|
|
my ($pdf) = @_; |
84
|
|
|
|
|
|
|
|
85
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('to_png', $pdf) or return; |
86
|
0
|
0
|
|
|
|
|
$self->brik_help_run_file_not_found('to_png', $pdf) or return; |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
my $output = $pdf; |
89
|
0
|
|
|
|
|
|
$output =~ s{\.pdf$}{}; |
90
|
0
|
|
|
|
|
|
$self->system("pdftoppm \"$pdf\" \"$output\" -png"); |
91
|
|
|
|
|
|
|
|
92
|
0
|
0
|
|
|
|
|
my $sf = Metabrik::System::File->new_from_brik_init($self) or return; |
93
|
0
|
|
|
|
|
|
my $png = $sf->glob("*.png"); |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
return $png; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub from_png { |
99
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
100
|
0
|
|
|
|
|
|
my ($glob, $output) = @_; |
101
|
|
|
|
|
|
|
|
102
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('from_png', $glob) or return; |
103
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('from_png', $output) or return; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
$self->system("convert $glob \"$output\""); |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
return $output; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub from_pdf_to_image_pdf { |
111
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
112
|
0
|
|
|
|
|
|
my ($pdf, $secure) = @_; |
113
|
|
|
|
|
|
|
|
114
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('from_pdf_to_image_pdf', $pdf) or return; |
115
|
0
|
0
|
|
|
|
|
$self->brik_help_run_file_not_found('from_pdf_to_image_pdf', $pdf) |
116
|
|
|
|
|
|
|
or return; |
117
|
|
|
|
|
|
|
|
118
|
0
|
0
|
|
|
|
|
my $sp = Metabrik::String::Password->new_from_brik_init($self) or return; |
119
|
0
|
0
|
|
|
|
|
my $sf = Metabrik::System::File->new_from_brik_init($self) or return; |
120
|
|
|
|
|
|
|
|
121
|
0
|
0
|
|
|
|
|
my $png = $self->to_png($pdf) or return; |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
my $output = $pdf; |
124
|
0
|
|
|
|
|
|
$output =~ s{\.pdf$}{.image.pdf}; |
125
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
my $png_list = ''; |
127
|
0
|
|
|
|
|
|
for (@$png) { |
128
|
0
|
|
|
|
|
|
$png_list .= "\"$_\" "; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
|
$self->system("convert $png_list \"$output\""); |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
$sf->remove($png); |
134
|
|
|
|
|
|
|
|
135
|
0
|
0
|
|
|
|
|
if ($secure) { |
136
|
0
|
|
|
|
|
|
my $list = $sp->generate; |
137
|
0
|
|
|
|
|
|
my $pass = $list->[3]; |
138
|
0
|
|
|
|
|
|
$output = $self->secure($output, $pass); |
139
|
0
|
|
|
|
|
|
return [ $output, $pass ]; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
return $output; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
1; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
__END__ |