summaryrefslogtreecommitdiffstats
path: root/pdftool.pl
blob: 7e64d40529f023c400950d0f47fb580488862eab (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#! /usr/bin/perl

use Getopt::Long; 
use IPC::Open2;
use warnings;
use strict;

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

my $pdf2ps      = 'pdftops';
my $pscrop      = 'psnup2.pl -s1 -l1';
my $psresize    = 'psresize';
my $psnup       = 'psnup';
my $psbook      = 'psbook';
my $ps2pdf      = 'ps2pdf';

my $tmpdir      = '/tmp';

my $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,
            "papersize|p=s" => \$papersize,
            "margin|m=s"    => \$margin );

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

die "How should I put a non-positive number of pages per sheet?"
    if $nup <= 0;

unless (defined $papersize) {
    $papersize = `paperconf` or die "Can't guess papersize";
    chomp $papersize;
}

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

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


my ($mresize, $mcrop, $mnup) = (0,0,0);

if (defined $crop) {
    $mresize = 0;
    if (not defined $book && $nup > 1) {
        $mcrop = $margin/2;
        $mnup  = $mcrop;
    } else {
        $mcrop = $margin;
        $mnup  = 0;
    }
} else {
    if (not defined $book && $nup > 1) {
        $mresize = $margin/2;
        $mnup    = $mresize;
    } else {
        $mresize = $margin;
        $mnup    = 0;
    }
}




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

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

# auxiliary files, to remove
my @auxfiles;

# auxiliary file descriptors, to close
my @auxfds;


if ($filename eq "-") {
    # Need to copy the whole input to an auxiliary file, since
    # conversion from pdf to ps requires random access to the data
    
    # TODO: only for pdfs
    $filename = "$tmpdir/pdftool-stdin-" . int(rand 2**16) . lc ".$filetype";
    open(AUXFD, '>', "$filename")
        or die "Can't read write into `$filename'";
    push @auxfiles, "$filename";
    
    # cat > $filename
    seek STDIN, 0, 0;
    while (<FILE>) {
        print AUXFD $_;
    }
    close(AUXFD);
}
close(FILE);


# Convert to ps, or read the input file
if ($filetype eq "PDF") {
    open *PSFILE, '|-', "$pdf2ps", "$filename", "-"
#    open2 PSFILE, '<&STDIN', "$pdf2ps $filename -"
        or die "Error with `$pdf2ps $filename -'";
} else {
    open *PSFILE, '<', "$filename"
        or die "Can't open `$filename'";
}
push @auxfds, fileno PSFILE;

# Resize file to our papersize
open2 *PSRESIZE, "<&PSFILE", "$psresize", "-p$papersize"
    or die "Error with `$psresize -p$papersize'";


# pscrop
*PSCROP = *PSRESIZE;

# psbook
*PSBOOK = *PSCROP;

# psnup
# TODO: sometimes unecessary
open2 *PSNUP, "<&PSBOOK", "$psnup", "-p$papersize", "-m$mnup", "-$nup"
    or die "Error with `$psnup -p$papersize -m$mnup -$nup'";

# TODO: not always stdout
open2 ">&STDOUT", "<&PSNUP", "$ps2pdf -sPAPERSIZE=$papersize - -"
    or die "Error with `$ps2pdf -sPAPERSIZE=$papersize - -'";

#flush;
print  STDERR "ok\n";
close STDIN;
close STDOUT;




close PSNUP;
close PSBOOK;
close PSCROP;
close PSRESIZE;
close PSFILE;

# close auxiliary filehandles
#map {close $_} @auxfds;

# delete auxiliary files
unlink @auxfiles;

#TODO: better do forget stdin, I guess
#if ($filename eq "-" && $filetype eq "PDF") {
#}

#if ($filetype eq "PDF") {
#    $command .= "$pdf2ps $filename - | "; 
#} else {
#    $command .= "cat $filename | ";
#}
#
#
##$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 .= "> /tmp/pouf.pdf";
#
#print "$command", "\n";

#system $command;


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