4

Perl: divide the pages of each file into two

 2 years ago
source link: https://www.codesd.com/item/perl-divide-the-pages-of-each-file-into-two.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Perl: divide the pages of each file into two

advertisements

I have written a code which should do the following:

  1. If command line arguments != 1, then the script exits. If a filename as a command line argument is given, then the script splits this file.
  2. If the 'go' command line argument is given, then the script gets the current directory, and then tries to split each pdf file in this directory. This procedure fails. I will give more details later.
  3. If a command line argument is neither a filename nor 'go', then the script shows a suitable message.

As to the 2nd point, the errors read:

Global symbol "$oldpdf" requires explicit package name at C:\strawberry\perl\bin\current.pl line 21. Global symbol "$newpdf" requires explicit package name at C:\strawberry\perl\bin\current.pl line 24. Global symbol "$oldpdf" requires explicit package name at C:\strawberry\perl\bin\current.pl line 24. Global symbol "$newpdf" requires explicit package name at C:\strawberry\perl\bin\current.pl line 31. Global symbol "$oldpdf" requires explicit package name at C:\strawberry\perl\bin\current.pl line 31. Global symbol "$filename" requires explicit package name at C:\strawberry\perl\bin\current.pl line 39. Global symbol "$newpdf" requires explicit package name at C:\strawberry\perl\bin\current.pl line 40. Execution of C:\strawberry\perl\bin\current.pl aborted due to compilation errors.

The script is as follows (I know it is ugly - I'm completely newbie):

use strict;
use warnings;
use PDF::API2;
use Cwd;

if ( @ARGV != 1 ) {
die "Command line should be either \"go\" or a filename!\n";
}

my $cmd = $ARGV[0];

if ($cmd eq "go") {
    my $current = getcwd();
    print "Current folder: $current\n";
    my @files = glob("*.pdf");
    foreach my $file (@files) {
        my $filename = $file;
        my $oldpdf = PDF::API2->open($filename);
        my $newpdf = PDF::API2->new;
}
for my $page_nb (1..$oldpdf->pages) {
my ($page, @cropdata);

$page = $newpdf->importpage($oldpdf, $page_nb);
@cropdata = $page->get_mediabox;
$cropdata[2] /= 2;
$page->cropbox(@cropdata);
$page->trimbox(@cropdata);
$page->mediabox(@cropdata);

$page = $newpdf->importpage($oldpdf, $page_nb);
@cropdata = $page->get_mediabox;
$cropdata[0] = $cropdata[2] / 2;
$page->cropbox(@cropdata);
$page->trimbox(@cropdata);
$page->mediabox(@cropdata);
}

(my $newfilename = $filename) =~ s/(.*)\.(\w+)$/$1-crop.$2/;
$newpdf->saveas("$newfilename");
}

elsif   (-f $cmd) {
    my $oldpdf = PDF::API2->open($cmd);
    my $newpdf = PDF::API2->new;
    for my $page_nb (1..$oldpdf->pages) {
    my ($page, @cropdata);

    $page = $newpdf->importpage($oldpdf, $page_nb);
    @cropdata = $page->get_mediabox;
    $cropdata[2] /= 2;
    $page->cropbox(@cropdata);
    $page->trimbox(@cropdata);
    $page->mediabox(@cropdata);

    $page = $newpdf->importpage($oldpdf, $page_nb);
    @cropdata = $page->get_mediabox;
    $cropdata[0] = $cropdata[2] / 2;
    $page->cropbox(@cropdata);
    $page->trimbox(@cropdata);
    $page->mediabox(@cropdata);
}

(my $newfilename = $cmd) =~ s/(.*)\.(\w+)$/$1-crop.$2/;
$newpdf->saveas("$newfilename");
}

else {
    print "Bad parameter!\n";
}


Updated

use strict;
use warnings;

use PDF::API2;
use Cwd;

if ( @ARGV != 1 ) {
    die "Command line should be either \"go\" or a filename!\n";
}

my $cmd = $ARGV[0];

if ( $cmd eq "go" ) {

    my $current = getcwd();
    print "Current folder: $current\n";
    my @files = glob("*.pdf");

    foreach my $file (@files) {

        my $filename = $file;
        my $oldpdf   = PDF::API2->open($filename);
        my $newpdf   = PDF::API2->new;
    }

    for my $page_nb ( 1 .. $oldpdf->pages ) {

        my ( $page, @cropdata );

        $page = $newpdf->importpage( $oldpdf, $page_nb );
        @cropdata = $page->get_mediabox;
        $cropdata[2] /= 2;
        $page->cropbox(@cropdata);
        $page->trimbox(@cropdata);
        $page->mediabox(@cropdata);

        $page        = $newpdf->importpage( $oldpdf, $page_nb );
        @cropdata    = $page->get_mediabox;
        $cropdata[0] = $cropdata[2] / 2;
        $page->cropbox(@cropdata);
        $page->trimbox(@cropdata);
        $page->mediabox(@cropdata);
    }

    ( my $newfilename = $filename ) =~ s/(.*)\.(\w+)$/$1-crop.$2/;
    $newpdf->saveas("$newfilename");
}

elsif ( -f $cmd ) {

    my $oldpdf = PDF::API2->open($cmd);
    my $newpdf = PDF::API2->new;

    for my $page_nb ( 1 .. $oldpdf->pages ) {
        my ( $page, @cropdata );

        $page = $newpdf->importpage( $oldpdf, $page_nb );
        @cropdata = $page->get_mediabox;
        $cropdata[2] /= 2;
        $page->cropbox(@cropdata);
        $page->trimbox(@cropdata);
        $page->mediabox(@cropdata);

        $page        = $newpdf->importpage( $oldpdf, $page_nb );
        @cropdata    = $page->get_mediabox;
        $cropdata[0] = $cropdata[2] / 2;
        $page->cropbox(@cropdata);
        $page->trimbox(@cropdata);
        $page->mediabox(@cropdata);
    }

    ( my $newfilename = $cmd ) =~ s/(.*)\.(\w+)$/$1-crop.$2/;
    $newpdf->saveas("$newfilename");
}

else {
    print "Bad parameter!\n";
}


What I was trying to get at is that your loop

foreach my $file (@files) {

    my $filename = $file;
    my $oldpdf   = PDF::API2->open($filename);
    my $newpdf   = PDF::API2->new;
}

just sets the values of three variables and then discards them over and over again, once for each file. You're trying to use those values in the next loop

for my $page_nb ( 1 .. $oldpdf->pages ) { ... }

but by now $oldpdf no longer exists so the program dies

Here you go. I've added a few things, in particular I've put the process of splitting a file into a subroutine to avoid duplicating the code

use strict;
use warnings 'all';

use PDF::API2;
use Cwd 'getcwd';

STDOUT->autoflush;

@ARGV == 1 or die qq{Command line should be either "go" or a filename!\n};

my ($cmd) = @ARGV;

if ( -f $cmd ) {
    split_file($cmd);
}
elsif ( lc($cmd) eq 'go' ) {

    printf "Current folder: %s\n", getcwd;

    split_file($_) for glob '*.pdf';
}
else {
    print "Bad parameter!\n";
}

sub split_file {

    my ($file) = @_;
    ( my $newfile = $file ) =~ s/(.+)\./$1-crop./x;

    printf qq{Processing "%s" => "%s"\n}, $file, $newfile;

    my $oldpdf = PDF::API2->open($file);
    my $newpdf = PDF::API2->new;

    for my $page_nb ( 1 .. $oldpdf->pages ) {

        for my $i ( 2, 0 ) {
            my $page      = $newpdf->importpage( $oldpdf, $page_nb );
            my @mediabox  = $page->get_mediabox;    # Left X, Bottom Y, Right X, Top Y
            $mediabox[$i] = $mediabox[2] / 2;
            $page->cropbox(@mediabox);
            $page->trimbox(@mediabox);
            $page->mediabox(@mediabox);
        }
    }

    $newpdf->saveas($newfile);
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK