Skip to content

Instantly share code, notes, and snippets.

@i-blis
Created November 10, 2010 07:19
Show Gist options
  • Save i-blis/670489 to your computer and use it in GitHub Desktop.
Save i-blis/670489 to your computer and use it in GitHub Desktop.
Split double page PDF in two
#!/usr/bin/env perl
use strict; use warnings;
use PDF::API2;
my $filename = shift =~ /\.pdf$/i
or die "Be kind enough to provide a PDF file to split as argument\n";
my $oldpdf = PDF::API2->open($filename);
my $newpdf = PDF::API2->new;
for my $page_nb (1..$oldpdf->pages) {
my ($page, @cropdata);
$page = $newpdf->importpage($oldpdf, $page_nb);
@cropdata = $page->get_mediabox;
$cropdata[2] /= 2;
$page->cropbox(@cropdata);
$page->trimbox(@cropdata);
$page->mediabox(@cropdata);
$page = $newpdf->importpage($oldpdf, $page_nb);
@cropdata = $page->get_mediabox;
$cropdata[0] = $cropdata[2] / 2;
$page->cropbox(@cropdata);
$page->trimbox(@cropdata);
$page->mediabox(@cropdata);
}
(my $newfilename = $filename) =~ s/(.*)\.(\w+)$/$1.clean.$2/;
$newpdf->saveas('$newfilename');
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment