Merge alternate pages from PDFs


/ Published in: Perl
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. #!/usr/bin/env perl
  2.  
  3.  
  4. use strict; use warnings;
  5. use List::Util 'min';
  6. use PDF::API2;
  7.  
  8. die "usage: pdfmixmerge file1 [file2 ... fileN] \n"
  9. if scalar @ARGV < 1;
  10.  
  11. my @filenames = @ARGV;
  12. my $output_pdf = PDF::API2->new;
  13. my @pdfs = map { PDF::API2->open( $_ ) } @filenames;
  14. my $min = min map { $_->pages } @pdfs;
  15.  
  16. for my $page_nb (1..$min ) {
  17. for my $pdf (@pdfs) {
  18. $output_pdf->importpage($pdf, $page_nb);
  19. }
  20. }
  21.  
  22. # There might be some pages left, let's add them at the end
  23. $min++;
  24. for my $pdf (@pdfs) {
  25. for my $page_nb ($min .. $pdf->pages) {
  26. $output_pdf->importpage($pdf, $_)
  27. }
  28. }
  29.  
  30. $output_pdf->saveas('bundle.pdf');

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.