Return to Snippet

Revision: 17227
at August 27, 2009 22:06 by iblis


Updated Code
#!/usr/bin/env perl
use strict; use warnings;
use PDF::API2;

my $filename = shift || 'test.pdf';
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__

Revision: 17226
at August 27, 2009 21:27 by iblis


Initial Code
#!/usr/bin/env perl
use strict; use warnings;
use PDF::API2;

my $filename = shift || 'pdfs/test.pdf';
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('pdfs/test.clean.pdf');

__END__

Initial URL

                                

Initial Description
Each page is split vertically in two pages. Crop and media views are set accordingly. Text layer should be preserved. 
Takes a path to a PDF file as argument and produces a cropped PDF in the same location.

Initial Title
Split (crop) double page PDFs in two

Initial Tags

                                

Initial Language
Perl