Skip to content

Instantly share code, notes, and snippets.

@hexatridecimal
Created September 8, 2011 21:10
Show Gist options
  • Save hexatridecimal/1204736 to your computer and use it in GitHub Desktop.
Save hexatridecimal/1204736 to your computer and use it in GitHub Desktop.
Adding a box to a PDF file
#!/usr/bin/perl
use strict;
use PDF::API2::Simple;
my ($input_pdf, $pdf, $api, $page, $width, $height);
my ($box_width, $box_height);
$input_pdf = shift @ARGV;
$pdf = PDF::API2::Simple->new(file => $input_pdf);
$api = $pdf->pdf();
$width = $pdf->width();
$height = $pdf->height();
print "Width: $width\n";
print "Height: $height\n";
# Make the box 1/2 the document's widest dimension
foreach my $page (1 .. $api->pages()) {
$api->openpage($page);
$pdf->current_page($page);
if ($width > $height) {
$box_width = $width / 2;
$box_height = $height;
} else {
$box_height = $height / 2;
$box_width = $width;
my $half = (($height - $box_height) / 2);
$pdf->rect( x => 0,
y => $half,
to_x => $width,
to_y => $half + $box_height,
fill_color => 'white',
fill => 'on' );
}
}
$api->saveas('/tmp/out.pdf');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment