mkpdfebook

crop pages of a pdf for an ebook reader
git clone https://a3nm.net/git/mkpdfebook/
Log | Files | Refs

commit 60c619984d9de9891b2129c5ffd861c1667c221b
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Sat, 20 Sep 2014 05:59:54 +0200

init

Diffstat:
crop.pl | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
mkpdfebook.sh | 15+++++++++++++++
2 files changed, 88 insertions(+), 0 deletions(-)

diff --git a/crop.pl b/crop.pl @@ -0,0 +1,73 @@ +#!/usr/bin/env perl +use strict; use warnings; +use PDF::API2; +use POSIX; + +# TODO clean this crap up +# TODO recursive cuts +# TODO respect aspect ratio! +my $filename = shift; +my $ofilename = shift; +my $width = int(shift); +my $height = int(shift); +my $margin = 10; +my $oldpdf = PDF::API2->open($filename); +my $newpdf = PDF::API2->new; +my $temppdf = PDF::API2->new; +for my $page_nb (1..$oldpdf->pages) { + my ($page, @cropdata); + my $nr = 0; + $page = $temppdf->importpage($oldpdf, $page_nb); + @cropdata = $page->get_mediabox; + if ($cropdata[2] ge $cropdata[3]) { + my $t = $cropdata[3]; + $cropdata[3] = $cropdata[2]; + $cropdata[2] = $t; + $nr++; + printf "prerotate\n"; + }; + print "before any cut\n"; + print "$cropdata[0] $cropdata[1] $cropdata[2] $cropdata[3]\n"; + if ($cropdata[3] ge $height) { + printf "rotate it once\n"; + print "$cropdata[0] $cropdata[1] $cropdata[2] $cropdata[3]\n"; + my $t = $cropdata[3]; + $cropdata[3] = $cropdata[2]; + $cropdata[2] = $t; + $nr++; + print "$cropdata[0] $cropdata[1] $cropdata[2] $cropdata[3]\n"; + my $aspect = $cropdata[3] / $height; + my $realw = $cropdata[2] / $aspect; + print "realw $realw aspect $aspect width $width\n"; + my $nump = ceil($realw / ($width - 2*$margin)); + print "$cropdata[0] $cropdata[1] $cropdata[2] $cropdata[3]\n"; + print "will need $nump pages\n"; + for my $i (0..($nump-1)) { + $page = $newpdf->importpage($oldpdf, $page_nb); + for my $r (1..($nr)) { + printf "rotate the real page\n"; + $page->rotate(-90); + } + print "subpage was $i\n"; + print "$cropdata[0] $cropdata[1] $cropdata[2] $cropdata[3]\n"; + @cropdata = $page->get_mediabox; + $cropdata[1] = ($nump - $i - 1) * $cropdata[3] / $nump - $margin; + $cropdata[3] = ($nump - $i) * $cropdata[3] / $nump + $margin; + print "now subpage is $i\n"; + print "$cropdata[0] $cropdata[1] $cropdata[2] $cropdata[3]\n"; + $page->cropbox(@cropdata); + $page->trimbox(@cropdata); + $page->mediabox(@cropdata); + } + } else { + print "no need to do anything\n"; + $page = $newpdf->importpage($oldpdf, $page_nb); + for my $r (1..($nr)) { + printf "rotate the real page\n"; + $page->rotate(-90); + } + } +} +(my $newfilename = $filename) =~ s/(.*)\.(\w+)$/$1.clean.$2/; +$newpdf->saveas($ofilename); + diff --git a/mkpdfebook.sh b/mkpdfebook.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +IN="$1" +OUT="$2" +DIR="$( cd "$( dirname "$0" )" && pwd )" +TEMP=`mktemp` +#WIDTH=600 +#HEIGHT=800 +WIDTH=450 +HEIGHT=600 +pdfcrop --margins "0 0 0 42" "$IN" $TEMP-crop.pdf +pdftk $TEMP-crop.pdf output $TEMP-sane.pdf +$DIR/crop.pl $TEMP-sane.pdf $TEMP-out.pdf $WIDTH $HEIGHT +#pdfmin $TEMP-out.pdf +cp $TEMP-out.pdf "$OUT"