#!/usr/bin/perl # # file: pdfinsertimg # purpose: inserts image in a pdf file # created: pasha jun 28 2020 # modified: pasha jul 2 2020 # modification: removed unnecessary 'rotate' # use strict; use warnings; use Getopt::Long; use PDF::API2; $SIG{__WARN__} = sub { die ($_[0]); }; # die on warnings sub _error ($); my ($page_num, $img_file, $x, $y, $scale); GetOptions ( 'img=s' => \$img_file, 'page=i' => \$page_num, 'x=i' => \$x, 'y=i' => \$y, 'scale=f' => \$scale, 'help' => sub { print STDERR < --page= --x= --y= --scale= \\ pdfinsertimg --help where: --img image file to insert --page number of page to insert image --x,y coordinates where to place image, with respect to the bottom left corner of the page, measured at 72dpi --scale scaling coefficient input pdf file output pdf file Example: pdfinsertimg --img=signature.png --page=3 --x=240 --y=465 --scale=0.18 \\ in.pdf out.pdf EOF ; exit (0); }, ); _error ('Wrong number of arguments. Try --help') if (scalar (@ARGV) != 2); _error ('--img option is mandatory') if (! defined ($img_file)); _error ('--page option is mandatory') if (! defined ($page_num)); _error ('--x option is mandatory') if (! defined ($x)); _error ('--y option is mandatory') if (! defined ($y)); _error ('--scale option is mandatory') if (! defined ($scale)); my ($input, $output) = @ARGV; my $pdf = PDF::API2->open ($input); my $page = $pdf->openpage ($page_num) || die ("unable to open page $page_num at $input"); $page->mediabox ('A4'); my $gfx=$page->gfx; my $img = $pdf->image_png ($img_file); $gfx->image ($img, $x, $y, $scale); #$gfx->rotate (180); $pdf->saveas ($output); $pdf->end(); exit (0); sub _error ($) { print (STDERR 'ERROR: ' . $_[0] . "\n"); exit (-1); } __END__