-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.pl
106 lines (87 loc) · 2.79 KB
/
run.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
#! /usr/bin/perl
# doublespaces a two-column pdf
# req: xpdf (pdftops, pstops, pstopdf)
# only tested with v0.12.4
# req: pdfcrop
# only tested with 1.20
++$|;
use strict;
sub filter($);
die "usage: $0 inputfile.pdf" unless $ARGV[0] =~ /(.*?)\.pdf$/;
my $inputpdf = "$1.pdf";
my $inputps = "$1.ps";
my $outputps = "$1-tmp.ps";
my $tmppdf = "$1-tmp.pdf";
my $finalps = "$1-dbl.ps";
my $outputpdf = "$1-dbl.pdf";
my $cmd = "pdftops $inputpdf $inputps";
print "Executing $cmd\n";
my $retVal = system($cmd);
die "pdftops failed" if $retVal;
my $pageWidth;
my $pageHeight;
open my $ifh, $inputps;
open my $ofh, ">$outputps";
print "Filtering postscript...\n";
for my $line (<$ifh>) {
print $ofh filter($line);
}
close $ofh;
close $ifh;
# now to rearrange pages
# pages are currently twice as tall. cut them in half
my $DPageHeight = $pageHeight * 2;
my $hPageWidth = $pageWidth / 2;
$cmd = "pstops -q -w$pageWidth -h$DPageHeight ";
my $slop = 50;
my $hWithSlop = -$pageHeight+$slop;
$cmd .="\"0(0,$hWithSlop),0(0,$slop),0(-$hPageWidth,$hWithSlop),0(-$hPageWidth,$slop)\" ";
$cmd .= "$outputps $finalps";
print "Executing $cmd\n";
die "pstops failed" if system($cmd);
open my $ifh, $finalps;
open my $ofh, ">$outputps";
print "Refiltering postscript to $outputps...\n";
my $boundingBox;
for (<$ifh>) {
s/^(\%\%DocumentMedia: \w+) \d+ \d+/$1 $hPageWidth $pageHeight/ and print;
s/^(\%\%BoundingBox: (\d+ \d+)) \d+ \d+/$1 $hPageWidth $pageHeight/ and
($boundingBox = $2. " $hPageWidth $pageHeight", print);
print $ofh $_;
}
close $ofh;
close $ifh;
print "Converting to pdf...\n";
system("pstopdf $outputps");
$cmd = "pdfcrop --bbox \"$boundingBox\" $tmppdf $tmppdf.crop.pdf";
print "Executing $cmd...\n";
system($cmd);
print "Converting back to postscript...\n";
system("pdftops -paperw $pageWidth -paperh $pageHeight -nocenter $tmppdf.crop.pdf $outputps");
#my $slop = 50;
#$cmd = "pstops -q -w$pageWidth -h$pageHeight ";
#$cmd .="\"2:0(0,$slop),1(0,-$slop)\" ";
#$cmd .= "$finalps $outputps";
#print "Executing $cmd\n";
#die "pstops failed" if system($cmd);
$cmd = "pstops -q -w$pageWidth -h$pageHeight ";
$cmd .="\"2:0(0,0)+1($hPageWidth,0)\" ";
$cmd .= "$outputps $finalps";
print "Executing $cmd\n";
die "pstops failed" if system($cmd);
$cmd = "pstopdf -q $finalps $outputpdf >/dev/null 2>&1";
print "Executing $cmd\n";
die "pstopdf failed" if system($cmd);
sub filter($) {
$_ = shift;
# Document size
s/^(\%\%DocumentMedia: \w+ (\d+) )(\d+)/$1.($3 * 2)/e and
($pageWidth = $2, $pageHeight = $3, print);
s/^(\%\%BoundingBox: \d+ \d+ \d+ )(\d+)/$1.($2 * 2)/e and print;
s|pdfSetup {|$& exch 2 mul exch | and print;
# Change y
s|/cm {|$& dup dup 5 get 2 mul 5 2 -1 roll put | and print;
s|/re {|$& 3 2 roll 2 mul 3 -2 roll 2 mul | and print;
s|/Td { pdfTextMat transform|$& 2 mul | and print;
return $_;
}