#!/usr/bin/perl # pdfrotate: rotate the orientation of a PDF file. # # Oct 8, 2013 by FUJIWARA Teruyoshi # # Copyright: Copyright (c) 2013 FUJIWARA Teruyoshi # License: Distributes under the same terms as perl # # Requirement: Text::PDF and CAM::PDF (you can get them at the CPAN.) # (This script is tested with CAM::PDF-1.58, Text::PDF-0.29.) # # Usage: Try "pdfrotate --help" for help message. # use strict; use warnings; use CAM::PDF; use Getopt::Long; my $opt_degree = 90; my $opt_overwrite; my $opt_help; my ($infile, $outfile); my $msg_usage = "pdfrotate [--degree DEG] [--overwrite] infile.pdf outfile.pdf"; my $msg_help = <<'EOS'; usage: pdfrotate [--degree DEG] [--overwrite] infile.pdf outfile.pdf -d, --degree DEG Rotate DEG degrees in clockwise. DEG is 90 by default. -o, --overwrite Outfile is set same as infile. If this option is specified, outfile is ignored. -h, --help Show this help and exit. EOS GetOptions('degree=i' => \$opt_degree, 'overwrite' => \$opt_overwrite, '--help' => \$opt_help); if ($opt_help) { print $msg_help; exit; } $infile = shift || die $msg_usage; if ($opt_overwrite) { $outfile = $infile; } else { $outfile = shift || die $msg_usage; } my $pdf = CAM::PDF->new($infile) || die "Cannot open the PDF file."; my $pages = $pdf->numPages(); for my $p (1..$pages) { my $pagedict = $pdf->getPage($p); my $rotate = $pdf->getValue($pagedict->{Rotate}); my $val = $rotate + $opt_degree; $pagedict->{Rotate} = CAM::PDF::Node->new('number', $val); } $pdf->cleanoutput($outfile);