#!/usr/bin/perl # # file: pdfrotate # purpose: rotates 1-page pdf # created: pasha sep 17 2009 # modified: pasha oct 21 2020 # modification: allowed negative angles in the option # use strict; use warnings; use Getopt::Long; use PDF::API2; $SIG{__WARN__} = sub { die ($_[0]); }; # die on warnings GetOptions ( 'version' => sub { print (STDERR 'PDF::API2 version ' . $PDF::API2::VERSION . "\n"); exit (0); }, 'help' => sub { print STDERR < pdfrotate --help|--version where: could be one of: 90 (rotate clockwise) m90 (= -90, rotate counterclockwise) 180 (turn upside down) --help prints this help and quites --version prints version of PDF::API2 and quites Produces file .pdf Example: pdfrotate 1.pdf 90 EOF ; exit (0); } ); if (scalar (@ARGV) != 2) { print (STDERR "Wrong number of arguments. Try --help\n"); exit (-1); } my ($input, $angle) = @ARGV; $angle = -90 if ($angle eq 'm90'); if (($angle != 90) && ($angle != -90) && ($angle != 180)) { print (STDERR "illegale value of angle\n"); exit (-1); } my $inputpdf = PDF::API2->open ($input); if ($inputpdf->pages != 1) { print (STDERR "input pdf should contain 1 page only\n"); exit (-1); } # do rotation and write a new file my $outputpdf = PDF::API2->new; $outputpdf->importpage ($inputpdf, 1, 1); my $page = $outputpdf->openpage (1); $page->rotate ($angle); $outputpdf->saveas ("${angle}.pdf"); $inputpdf->end(); $outputpdf->end(); exit (0); __END__