-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.pl
126 lines (106 loc) · 2.91 KB
/
mod.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
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
#!"C:\perl\bin\perl.exe"
use strict;
use Switch;
use File::Copy;
my $command = "pdftk ";
my %finalpages;
my $ARGC = @ARGV;
my $num = 0;
main();
sub num_pages {
my $file = shift;
my $num = 0;
my $command = "pdftk $file dump_data";
my $out = `$command`;
chomp $out;
if ( $out =~ /NumberOfPages: (\d+)/ ) {
$num = $1;
}
print "File: $file, pg: $num\n";
return $num;
}
sub get_direction {
my $d = shift;
switch ($d) {
case 'R' { return "right" }
case 'L' { return "left" }
case 'S' { return "south" }
case 'N' { return "north" }
else {return }
}
}
sub update_finals {
my $page = shift;
if ( $page =~ /(\d+)(-\d+|-end)(\w?)/ ) {
my $begin = $1;
my $end = $2;
my $altern = $3;
if ( defined($altern) && ($altern eq "S" || $altern eq "R" || $altern eq "L" || $altern eq "D") ) {
if ( $end == "-end" ) {
$end = $num
} else {
$end =~ s/-//;
}
for (my $var = $begin; $var <= $end; $var++) {
if ( $altern eq "D" ) {
$finalpages{$var} = "-1";
}
else {
$finalpages{$var} = get_direction($altern);
}
}
}
else {
print "ERROR: $page is incorrect parameter: $altern cannot be used as modifier. Use 'S', 'R' and 'L' only.\n";
exit 1;
}
}
elsif ( $page =~ /(\d+)(\w?)/) {
my $num = $1;
my $altern = $2;
if ( defined($altern) && ($altern eq "S" || $altern eq "R" || $altern eq "L") ) {
$finalpages{$num} = get_direction($altern);
}
elsif ( $altern eq "D" ) {
$finalpages{$num} = "-1";
}
else {
print "ERROR: $page is incorrect parameter: $altern cannot be used as modifier. Use 'S', 'R' and 'L' only.\n";
exit 1;
}
}
else {
print "ERROR: $page is incorrect parameter: Only use page numbers with modifiers.\n";
exit 1;
}
}
sub main {
my ($doc, @pages) = @ARGV;
my $finalfile = "finalfile.pdf";
if ( $doc == "-i" ) {
$doc = shift (@pages);
$finalfile = $doc;
}
$command = "$command \"$doc\" cat";
move($doc, "temp.pdf");
$num = num_pages("temp.pdf");
move("temp.pdf", $doc);
for ( my $i = 1; $i < $num+1; $i++ ) {
$finalpages{$i} = "";
}
while (<@pages>) {
my $current = $_;
update_finals($current);
}
for ( my $i = 1; $i < $num+1; $i++ ) {
my $next_page = "";
if ( $finalpages{$i} ne "-1" ) {
$next_page = "$i$finalpages{$i}";
}
$command = "$command $next_page";
}
$command = "$command output temp.pdf";
print "Command:\n$command\n";
`$command`;
move("temp.pdf", $finalfile);
}