Jawle has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I have undertaken the task of learning Perl in the interest of running this script:

http://snipplr.com/view/18924/split-crop-double-page-pdfs-in-two/

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

I have created the program and am able to launch it, however I am very confused as to what I need to replace with my file and directory. For ex: should I insert my filename in line 6 in the brackets of ($filename)?

Replies are listed 'Best First'.
Re: Splitting PDFs with PDF::API2
by aitap (Curate) on Oct 29, 2014 at 20:26 UTC
    This code expects the filename as the first command line argument. For example, if the program is saved as "split.pl" in your home directory and the file to be processed is "MyDocument.pdf" on the desktop, launch perl ~/split.pl ~/Desktop/MyDocument.pdf. See shift and @ARGV for more info. The processed file will be created near the source file, with ".clean" added before the file extension (MyDocument.clean.pdf in my example).
      You my friend are brilliant! Thank you for such a simple and clear answer! This is a great script and works wonders for scanned textbooks.
Re: Splitting PDFs with PDF::API2
by onelander (Sexton) on Oct 29, 2014 at 21:09 UTC
    Place the full path of your PDF file on this line.
    my $filename = shift || 'test.pdf';
    so it would look something like the following:
    my $filename = '/downloads/YuruguOCR.pdf';
Re: Splitting PDFs with PDF::API2
by Jawle (Initiate) on Oct 29, 2014 at 18:30 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?