/
pdf-info.p6
132 lines (101 loc) · 3.37 KB
/
pdf-info.p6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env perl6
use v6;
use PDF::Class;
use PDF::IO::Input::Str;
use PDF::IO::Input::IOH;
multi sub pretty-print(DateTime $dt --> Str) {
sprintf('%s %s %02d %02d:%02d:%02d %04d',
<Mon Tue Wed Thu Fri Sat Sun>[$dt.day-of-week - 1],
<Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec>[$dt.month - 1],
$dt.day-of-month,
$dt.hour, $dt.minute, $dt.second,
$dt.year)
}
multi sub pretty-print(Mu $val --> Str) is default {
~$val
}
# A port of pdfinfo.pl from the Perl 5 CAM::PDF module to PDF and Perl 6
multi sub MAIN(Bool :$version! where $_) {
# nyi in rakudo https://rt.perl.org/Ticket/Display.html?id=125017
say "PDF::Class {PDF::Class.^version}";
say "this script was ported from the CAM::PDF PDF Manipulation library";
say "see - https://metacpan.org/pod/CAM::PDF";
}
sub yes-no(Bool $cond) {
$cond ?? 'yes' !! 'no';
}
multi sub MAIN(Str $infile, #| input PDF
Str :$password = '', #| password for the input PDF, if encrypted
) {
my $input = $infile eq '-'
?? PDF::IO::Input::Str.new( :value($*IN.slurp-rest( :enc<latin-1> )) )
!! PDF::IO::Input::IOH.new( :value($infile.IO.open( :enc<latin-1> )) );
my $doc = PDF::Class.open( $input, :$password );
my UInt $size = $input.codes;
my UInt $pages = $doc.page-count;
my Version $pdf-version = $doc.version;
my $pdf-info = $doc.Info;
my $box = $doc.Pages.MediaBox;
my $encrypt = $doc.Encrypt;
my $catalog = $doc.Root;
my $tagged = $catalog.?MarkInfo.?Marked // False;
my $partial = $catalog.?MarkInfo.?Suspects // False;
my UInt $revisions = + $doc.reader.xrefs;
my UInt @page-size = $box
?? ($box[2] - $box[0], $box[3] - $box[1])
!! (0, 0);
say "File: $infile";
say "File Size: $size bytes";
say "Pages: $pages";
if $pdf-info {
for $pdf-info.keys -> $key {
my Str $info = pretty-print( $pdf-info{$key} );
printf "%-13s %s\n", $key ~ q{:}, $info
unless $info eq '';
}
}
say 'Tagged: ' ~ yes-no($tagged) ~ ($partial ?? ' (partial)' !! '');
say 'Page Size: ' ~ (@page-size[0] ?? "@page-size[0] x @page-size[1] pts" !! 'variable');
## say 'Optimized: '.($doc->isLinearized()?'yes':'no');
say "PDF version: $pdf-version";
say "Revisions: $revisions";
use PDF::DAO::Type::Encrypt :PermissionsFlag;
print 'Encryption: ';
if my $enc = $doc.Encrypt and $enc.O {
my $V = $enc.V // 0;
my $Length = $enc.Length // 40;
print "yes (";
# show user, not owner, permissions
temp $doc.reader.crypt.is-owner = False
if $doc.reader.?crypt;
for :print(PermissionsFlag::Print), :copy(PermissionsFlag::Copy),
:change(PermissionsFlag::Modify), :addNotes(PermissionsFlag::Add) {
print "{.key}:{yes-no( $doc.permitted: .value)} ";
}
say "algorithm:RC4 {$V}.{$enc.R}, $Length bits)";
}
else {
say "no";
}
## if (@ARGV > 0)
## {
## print "---------------------------------\n";
## }
}
=begin pod
=head1 NAME
pdf-info.p6 - Print information about PDF file(s)
=head1 SYNOPSIS
pdf-info.p6 [options] file.pdf
Options:
--password password for an encrypted PDF
=head1 DESCRIPTION
Prints to STDOUT various basic details about the specified PDF
file(s).
=head1 SEE ALSO
CAM::PDF (Perl 5)
PDF (Perl 6)
=head1 AUTHOR
See L<CAM::PDF>
=cut
=end pod