Return to Snippet

Revision: 27967
at June 28, 2010 18:37 by iblis


Initial Code
#!/usr/bin/env perl


use strict; use warnings;
use List::Util 'min';
use PDF::API2;

die "usage: pdfmixmerge file1 [file2 ... fileN] \n" 
  if scalar @ARGV < 1;

my @filenames = @ARGV;
my $output_pdf = PDF::API2->new;
my @pdfs = map { PDF::API2->open( $_ ) } @filenames;
my $min = min map { $_->pages } @pdfs;

for my $page_nb (1..$min ) {
  for my $pdf (@pdfs) {
    $output_pdf->importpage($pdf, $page_nb);
  }
}

# There might be some pages left, let's add them at the end
$min++;
for my $pdf (@pdfs) {
  for my $page_nb ($min .. $pdf->pages) {
    $output_pdf->importpage($pdf, $_)    
  }
}

$output_pdf->saveas('bundle.pdf');

Initial URL

                                

Initial Description
Merges one page of each PDFs after one another into a new PDF file

Initial Title
Merge alternate pages from PDFs

Initial Tags

                                

Initial Language
Perl