Split (crop) double page PDFs in two


/ Published in: Perl
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. #!/usr/bin/env perl
  2. use strict; use warnings;
  3. use PDF::API2;
  4.  
  5. my $filename = shift || 'test.pdf';
  6. my $oldpdf = PDF::API2->open($filename);
  7. my $newpdf = PDF::API2->new;
  8.  
  9. for my $page_nb (1..$oldpdf->pages) {
  10. my ($page, @cropdata);
  11.  
  12. $page = $newpdf->importpage($oldpdf, $page_nb);
  13. @cropdata = $page->get_mediabox;
  14. $cropdata[2] /= 2;
  15. $page->cropbox(@cropdata);
  16. $page->trimbox(@cropdata);
  17. $page->mediabox(@cropdata);
  18.  
  19. $page = $newpdf->importpage($oldpdf, $page_nb);
  20. @cropdata = $page->get_mediabox;
  21. $cropdata[0] = $cropdata[2] / 2;
  22. $page->cropbox(@cropdata);
  23. $page->trimbox(@cropdata);
  24. $page->mediabox(@cropdata);
  25. }
  26.  
  27. (my $newfilename = $filename) =~ s/(.*)\.(\w+)$/$1.clean.$2/;
  28. $newpdf->saveas('$newfilename');
  29.  
  30. __END__

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.