From f53231fdd4bb343d19026fe08817bff46341ae88 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Wed, 22 Sep 2010 00:20:29 +0200 Subject: Need to be improved, but at least it works --- pdftool.pl | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file 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 = ; 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 = ; +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; -- cgit v1.2.3