Skip to content

Instantly share code, notes, and snippets.

@itochu0523
Created June 10, 2014 10:34
Show Gist options
  • Save itochu0523/2dab78c0848b3a875a7b to your computer and use it in GitHub Desktop.
Save itochu0523/2dab78c0848b3a875a7b to your computer and use it in GitHub Desktop.
crop.pl
use strict;
use warnings;
my @offset = (20, 20, -20, -20);
while(<*.pdf>)
{
next if /-cropped/;
open my $in, $_ or die;
s/(.*)(\.pdf)$/$1-cropped$2/;
open my $out, '>', $_ or die;
binmode $in;
binmode $out;
while(<$in>)
{
s/(\/CropBox\s*\[\s*([^\[]+)\])/crop($1, $2)/eg;
print $out $_;
}
}
sub crop
{
my @offset = @offset;
my @pos = map{int $_ + shift @offset} split /\s+/, $_[1];
my $crop = sprintf '/CropBox[%d %d %d %d]', @pos;
my $blank = length($_[0]) - length $crop;
warn $_[0] and return $_[0] if $blank < 0;
return $crop.(' ' x $blank);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment