Posted By

iblis on 08/27/09


pdf batch crop

Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


Split (crop) double page PDFs in two

 / Published in: Perl

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.

  1. #!/usr/bin/env perl
  2. use strict; use warnings;
  3. use PDF::API2;
  5. my $filename = shift || 'test.pdf';
  6. my $oldpdf = PDF::API2->open($filename);
  7. my $newpdf = PDF::API2->new;
  9. for my $page_nb (1..$oldpdf->pages) {
  10. my ($page, @cropdata);
  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);
  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. }
  27. (my $newfilename = $filename) =~ s/(.*)\.(\w+)$/$1.clean.$2/;
  28. $newpdf->saveas('$newfilename');
  30. __END__

Report this snippet  


RSS Icon Subscribe to comments
Posted By: roninxyz on March 17, 2010

I have spent the last few hours learning about Perl and scripting on my Mac to be able to run this script, and I have to say that I was extremely skeptical that I'd be able to figure out anything as an almost complete newbie. Fortunately it didn't take long before I realized that PDF::API2 was a module, which sent me scrambling to figure out how to install those. But, with good instruction on the 'net, I've got this thing up and running. What a great experience and so useful! Thank you for this!

Posted By: iblis on June 28, 2010

You're welcome. I should have indeed made it clear that one needs PDF::API2...

On the Mac, you can also crop left and then right pages with Preview and re-assemble them with this script

Posted By: martienne on October 24, 2010

It works nicely for smaller files... but it doesn't seem to work on larger files, for example one with 24 pages didn't work but one with 4 pages worked. The pages all come out blank for the bigger files.
Any idea?

Posted By: iblis on November 9, 2010

@martienne I used the script on large files (300+ pages) without any problem (the only issue is increase in size). What platform are you running Perl on? Where do your PDFs come from (scan, OCRed scan...)? Do they already have the crop box set?

Posted By: andreapisac on July 22, 2011

Hi there,

I would really like to try this code. Someone on the Adobe Forum posted a step to step guide for total beginners (like myself) on how to install Perl Modules (for Mac users). I followed that and it worked fine with no problems. Then they said to fire the script (meaning this one) in order to get this great feature, but this was assuming people knew how to fire scripts. Can someone be so kind to explain to a beginner what to do once I have installed PDF::API2. Thanks a million! Andrea

Posted By: jlhkrans on December 10, 2011

Thank you very much.

If you have done some left and right cropping in the PDF before, you should change the lines 14 and 21 to: 14. $cropdata[2] = ($cropdata[2] + $cropdata[0]) / 2; 21. $cropdata[0] = ($cropdata[2] + $cropdata[0]) / 2;

Posted By: teoric on July 19, 2014

Nice snippet!

When I run it, PDF::API2 does not copy the cropbox on import, so that I have to take it from the old page:

my $original_page = $oldpdf->openpage($page_nb);
my @cropdata = $original_page->get_cropbox;

You need to login to post a comment.