summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem.moulin@chalmers.se>2010-09-22 00:20:29 +0200
committerGuilhem Moulin <guilhem.moulin@chalmers.se>2010-09-22 00:20:29 +0200
commitf53231fdd4bb343d19026fe08817bff46341ae88 (patch)
tree3c519e9b776d19b38341d97bdd98def481a0fbc2
parentf1a89b02805dd2307bbf0dc29fe33319c6514f92 (diff)
Need to be improved, but at least it works
-rwxr-xr-xpdftool.pl77
1 files changed, 73 insertions, 4 deletions
diff --git a/pdftool.pl b/pdftool.pl
index 269825d..5136849 100755
--- a/pdftool.pl
+++ b/pdftool.pl
@@ -16,14 +16,14 @@ my $psbook = 'psbook';
my ($zip,$zcat) = ('bzip2','bzcat');
open(PAPERSIZE, "/etc/papersize")
- or die "Couldn't read `/etc/papersize'";
+ or die "Can't open `/etc/papersize'";
my $papersize = <PAPERSIZE>;
chomp $papersize
- or die "Couldn't read papersize";
+ or die "Can't read `/etc/papersize/";
close PAPERSIZE;
-my $nup = 1;
+my $nup = 1;
my $margin = 1;
#TODO: units
my $crop;
@@ -38,12 +38,81 @@ GetOptions( "nup|n=i" => \$nup,
"book|b" => \$book,
"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;
}
+$margin /= 2 if defined $crop; #&& $nup > 1;
+
+
+$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; ";
+}
+
+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 - - ";
+
+$command .= "| $zip\" | $zcat " if ($remote ne "localhost");
+
+$command .= "> /tmp/pouf.pdf";
-print $nup, "\n";
+print "$command", "\n";
+system $command;