/
pdf-rewriter.pl
executable file
·41 lines (32 loc) · 1.3 KB
/
pdf-rewriter.pl
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
#!/usr/bin/env perl6
# Simple round trip read and rewrite a PDF
use v6;
use PDF::Reader;
#| rewrite a PDF or FDF and/or convert to/from JSON
sub MAIN (
Str $file-in, #| input PDF, FDF or JSON file (.json extension)
Str $file-out = $file-in, #| output PDF, FDF or JSON file (.json extension)
Str :$password = ''; #| password for encrypted documents
Bool :$repair = False, #| bypass and repair index. recompute stream lengths. Handy when
#| PDF files have been hand-edited.
Bool :$rebuild = False, #| rebuild object tree (renumber, garbage collect and deduplicate objects)
Bool :$compress = False, #| compress streams
Bool :$uncompress = False, #| uncompress streams
Bool :$zen = False, #| require PDF::Zen
) {
if $zen {
require ::('PDF::Zen')
}
die "conflicting arguments: --compress --uncompress"
if $compress && $uncompress;
my $reader = PDF::Reader.new;
note "opening {$file-in} ...";
$reader.open( $file-in, :$repair, :$password );
if $uncompress || $compress {
note $compress ?? "compressing ..." !! "uncompressing ...";
$reader.recompress(:$compress)
}
note "saving ...";
$reader.save-as($file-out, :$rebuild);
note "done";
}