From 3404de8b1fd89f7d634744b80596a1bc5c079b37 Mon Sep 17 00:00:00 2001 From: Jakob Haufe Date: Mon, 3 Jul 2017 03:13:01 +0200 Subject: [PATCH 1/1] Inital commit --- README.md | 39 ++++++++++++++++++++++++++++++++++++++ gencropped | 32 +++++++++++++++++++++++++++++++ genmeta | 43 ++++++++++++++++++++++++++++++++++++++++++ genpreview | 39 ++++++++++++++++++++++++++++++++++++++ pdfstitch | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 208 insertions(+) create mode 100644 README.md create mode 100755 gencropped create mode 100755 genmeta create mode 100755 genpreview create mode 100755 pdfstitch diff --git a/README.md b/README.md new file mode 100644 index 0000000..a20bea7 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# pdfstitch + +`pdfstitch` does a similar job to `pdfnup` but incorporates the following additional features: + +* Crop pages to a certain size +* Adjust the crop position per page + +## Dependencies + +`pdfstitch` makes use of the following Perl modules: + +* File::Basename (part of perl base) +* PDF::API2 +* YAML + +On Debian, you can installed them with: + +`# apt install libpdf-api2-perl libyaml-perl` + +## Usage + +1. Run `genmeta` on your input PDF like so: + + `./genmeta foobar.pdf` + + This will generate a YAML file called `foobar.pdf.stitch`. Edit this file according to the desired output. +2. Optional: Run `genpreview` on that YAML file like so: + + `./genpreview foobar.pdf.stitch` + + This will generate a new PDF called `foobar-preview.pdf` containing only the pages you selected + in the YAML file with each page being overlayed with a transparent box showing the area the + page will be cropped to. + +3. Optional: Run `gencropped` on the YAML file. This will generate a new PDF called `foobar-cropped.pdf` containing + only the pages you selected in the YAML file with each page being cropped as specified. +4. Run `pdfstitch` on the YAML file. This will generated a single-page PDF called `foobar-stitched.pdf` with all + selected pages being as specified in the YAML file. + diff --git a/gencropped b/gencropped new file mode 100755 index 0000000..1b99c2d --- /dev/null +++ b/gencropped @@ -0,0 +1,32 @@ +#!/usr/bin/perl -w + +use strict; + +use File::Basename; +use PDF::API2; +use YAML; + +my $metafile = $ARGV[0]; + +die "Please specify a .pdf.stitch to process!\n" unless defined $metafile; +die "Can't open $metafile!\n" unless -r $metafile; + +my $meta = YAML::LoadFile($metafile); + +my $inpdf = PDF::API2->open($meta->{input}); +my $cropped = PDF::API2->new(); + +foreach my $nr (@{$meta->{pageorder}}) +{ + print "Cropping page $nr...\n"; + my $page = $cropped->import_page($inpdf, $nr, 0); + my ($llx, $lly, $urx, $ury); + $llx = $meta->{x} + $meta->{pageoffsets}->{$nr}->{x}; + $lly = $meta->{y} + $meta->{pageoffsets}->{$nr}->{y}; + $urx = $llx + $meta->{width}; + $ury = $lly + $meta->{height}; + $page->cropbox($llx, $lly, $urx, $ury); +} + +$cropped->saveas(basename($meta->{input}, '.pdf') . '-cropped.pdf'); + diff --git a/genmeta b/genmeta new file mode 100755 index 0000000..ee534b6 --- /dev/null +++ b/genmeta @@ -0,0 +1,43 @@ +#!/usr/bin/perl -w + +use strict; + +use PDF::API2; +use YAML; + +my $infile = $ARGV[0]; + +die "Please specify PDF to examine!\n" unless defined $infile; +die "Could not open $infile!\n" unless -r $infile; + +my $outfile = "${infile}.stitch"; + +die "$outfile exists, aborting!\n" if -e $outfile; + +print 'Creating metafile for '.$infile."...\n"; + +my $pdf = PDF::API2->open($infile); + +my $page = $pdf->openpage(1); +my ($llx, $lly, $urx, $ury) = $page->get_mediabox; + +my $meta = { + input => $infile, + x => (($urx - $llx)*0.1)/2, + y => (($ury - $lly)*0.1)/2, + width => ($urx - $llx)*0.9, + height => ($ury - $lly)*0.9, + columns => int(sqrt($pdf->pages)), + rows => int(sqrt($pdf->pages)), + pageorder => [(1 .. $pdf->pages)], +}; + +foreach $page (1..$pdf->pages) +{ + $meta->{pageoffsets}->{$page}->{x} = 0; + $meta->{pageoffsets}->{$page}->{y} = 0; +} + +YAML::Bless($meta)->keys(['input','x','y','width','height','columns','rows', 'pageorder','pageoffsets']); +YAML::DumpFile($outfile,$meta); + diff --git a/genpreview b/genpreview new file mode 100755 index 0000000..6cd9b8f --- /dev/null +++ b/genpreview @@ -0,0 +1,39 @@ +#!/usr/bin/perl -w + +use strict; + +use File::Basename; +use PDF::API2; +use YAML; + +my $metafile = $ARGV[0]; + +die "Please specify a .pdf.stitch to process!\n" unless defined $metafile; +die "Can't open $metafile!\n" unless -r $metafile; + +my $meta = YAML::LoadFile($metafile); + +my $inpdf = PDF::API2->open($meta->{input}); +my $outpdf = PDF::API2->new(); + +my $transp = $outpdf->egstate(); +$transp->transparency(0.8); + +foreach my $nr (@{$meta->{pageorder}}) +{ + print "Generating preview for page $nr...\n"; + my $page = $outpdf->import_page($inpdf, $nr, 0); + my $content = $page->gfx(); + $content->egstate($transp); + my ($llx, $lly, $urx, $ury); + $llx = $meta->{x} + $meta->{pageoffsets}->{$nr}->{x}; + $lly = $meta->{y} + $meta->{pageoffsets}->{$nr}->{y}; + $urx = $meta->{width}; + $ury = $meta->{height}; + $content->rect($llx, $lly, $urx, $ury); + $content->fillcolor('%F000'); + $content->fill(); +} + +$outpdf->saveas(basename($meta->{input}, '.pdf') . '-preview.pdf'); + diff --git a/pdfstitch b/pdfstitch new file mode 100755 index 0000000..1794a8a --- /dev/null +++ b/pdfstitch @@ -0,0 +1,55 @@ +#!/usr/bin/perl -w + +use strict; + +use File::Basename; +use PDF::API2; +use YAML; + +my $metafile = $ARGV[0]; + +die "Please specify a .pdf.stitch to process!\n" unless defined $metafile; +die "Can't open $metafile!\n" unless -r $metafile; + +my $meta = YAML::LoadFile($metafile); + +my $width = $meta->{width} * $meta->{columns}; +my $height = $meta->{height} * $meta->{rows}; + +my $inpdf = PDF::API2->open($meta->{input}); +my $outpdf = PDF::API2->new(); + +my $page = $outpdf->page(); + +$page->mediabox($width + 100, $height + 100); + +my $content = $page->gfx(); +my $column = 1; +my $row = 1; + +foreach my $nr (@{$meta->{pageorder}}) +{ + my $xo = $outpdf->importPageIntoForm($inpdf, $nr); + + my ($llx, $lly, $urx, $ury); + $llx = $meta->{x} + $meta->{pageoffsets}->{$nr}->{x}; + $lly = $meta->{y} + $meta->{pageoffsets}->{$nr}->{y}; + $urx = $llx + $meta->{width}; + $ury = $lly + $meta->{height}; + $xo->bbox($llx, $lly, $urx, $ury); + + my $xpos = ($column - 1) * $meta->{width}; + my $ypos = $height - ($row * $meta->{height}); + print "Stitching page $nr at $xpos,$ypos...\n"; + $content->formimage($xo, $xpos, $ypos); + $column++; + if($column > $meta->{columns}) + { + $row++; + $column=1; + } + +} + +$outpdf->saveas(basename($meta->{input}, '.pdf') . '-stitched.pdf'); + -- 2.39.5