summaryrefslogtreecommitdiffstats
path: root/pdftool.pl
blob: f10ecec6e7100d9b8c714873b242cf2f99be7113 (plain)
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
#! /usr/bin/perl

use Getopt::Long; 
use warnings;
use strict;

Getopt::Long::Configure ("bundling");

my $remote      = 'graal';

my $pdf2ps      = 'pdftops';
my $pscrop      = 'psnup2.pl -s1 -l1';
my $psresize    = 'psresize';
my $psnup       = 'psnup';
my $psbook      = 'psbook';
my $ps2pdf      = 'ps2pdf';
my ($zip,$zcat) = ('bzip2','bzcat');
my $pdfviewer   = 'xpdf';

open(PAPERSIZE, "/etc/papersize")
    or die "Can't open `/etc/papersize'";
my $papersize   = <PAPERSIZE>;
chomp $papersize
    or die "Can't read `/etc/papersize/";
close PAPERSIZE;


my $nup     = 1;
my $margin  = 1;
#TODO: units
my $crop;
my $book;

GetOptions( "nup|n=i"    => \$nup,
            "1"          => sub { $nup = 1 },
            "2"          => sub { $nup = 2 },
            "4"          => sub { $nup = 4 },
            "8"          => sub { $nup = 8 },
            "crop|c"     => \$crop,
            "book|b"     => \$book,
            "paper|p=s"  => \$papersize,
            "margin|m=s" => \$margin );

die "I can handle only one file at the same time :P"
    if $#ARGV > 0;


my $filename = $ARGV[0];
$filename = "-" unless defined $filename;
my $filename2 = $filename;
$filename2 = "(stdin)" if $filename eq "-";

for (my $n = $nup; $n > 1; $n /= 2) {
    die "nup should be a power of two"
        unless $n % 2 == 0;
}

#Note: $margin is ignored if $crop is unset
$margin = 0 unless defined $crop;
$margin /= 2;


$remote = "localhost"
    if $remote eq `hostname` || $remote eq "127.0.0.1";




open(FILE, $filename)
    or die "Can't read `$filename'";

my $filetype;
my $line  = <FILE>;
if (defined $line && $line =~ /%!PS.*/) {
    $filetype = "PS";
} elsif (defined $line && $line =~ /%PDF.*/) {
    $filetype = "PDF";
} else {
    die "Can't recognize the type of `$filename2'";
}
close(FILE);


my $command = "";


#TODO: better do forget stdin, I guess
if ($filename eq "-" && $filetype eq "PDF") {
    $command.="cat - > /tmp/pouf.pdf";
    system $command;
}

if ($filetype eq "PDF") {
    $command .= "$pdf2ps $filename - | "; 
} else {
    $command .= "cat $filename | ";
}

if ($remote eq "localhost") {
    $command .= "cat > /tmp/paf.ps; ";
} else {
    $command .= "$zip | ssh $remote \"$zcat - > /tmp/paf.ps; ";
}

#$command .= "$psresize -p$papersize /tmp/paf.ps | ";
# useless, since the argument of $pscrop has to be seekable

if (defined $crop) {
    $command .= "$pscrop -m$margin /tmp/paf.ps | ";
} else {
    $command .= "cat /tmp/paf.ps | ";
}

$command .= "$psnup -p$papersize -m${margin}cm -$nup | ";

$command .= "$psbook | " if defined $book;

$command .= "$ps2pdf -sPAPERSIZE=$papersize - - ";

$command .= "| $zip\" | $zcat " if ($remote ne "localhost");

$command .= "> /tmp/pouf.pdf";

print "$command", "\n";

system $command;


system ($pdfviewer, "/tmp/pouf.pdf");