in reply to Splitting PDFs with PDF::API2

Perhaps I should mention I'm running OS X Yosemite through the default Terminal.App. My PDF is located in the Downloads folder and is an OCR'ed version of this book:

http://www.docdroid.net/98ej/yurugu-an-african-centered-critique-of-european-cultural-thought-and-behavior-marimba-ani-smaller.pdf.html

Replies are listed 'Best First'.
Re^2: Splitting PDFs with PDF::API2
by perlron (Pilgrim) on Oct 29, 2014 at 18:45 UTC

    Hi, U could try to create a simple pdf first ,if you are a new user to perl. and then work forward sequentially. link to the documentation of PDF::API2 TO create a new file , this is the syntax
    $pdf = PDF::API2->new(-file => 'our/new.pdf');

    The temporal difficulty with perl is u need to know C well to know its awesome.else u just keep *using* it and writing inefficient code
      Hi, I did create a new blank pdf from scratch using the PDF::Create module as a test and it worked.

      Should my code look like this? (Notice change made to line 5)

      #!/usr/bin/env perl use strict; use warnings; use PDF::API2; my $filename = shift || 'test.pdf'; my $oldpdf = PDF::API2->open(/downloads/YuruguOCR.pdf); 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.clean.$2/; $newpdf->saveas('$newfilename'); __END__

      Or should I have edited more lines?


        about line 5 So the open statement opens an existing PDF file.
        # Open an existing PDF file $pdf = PDF::API2->open('some.pdf');

        and to create a new pdf in the directory of your choice..
        $pdf = PDF::API2->new(-file => 'our/new.pdf');
        Please note PDF::API2 and PDF::Create are two different modules on CPAN. more info on the metacpan site

        The temporal difficulty with perl is u need to know C well to know its awesome.else u just keep *using* it and writing inefficient code