Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7829571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:49:57+00:00 2026-06-02T10:49:57+00:00

I have lots of pdf documents to merge together, so I wrote this code

  • 0

I have lots of pdf documents to merge together, so I wrote this code to do it. It works for the case where I only have two pdf documents to merge, but if I give it more than two, the extra documents come out garbled. Can you help me find what’s wrong?

#!/usr/bin/perl

use PDF::API2;
use List::Util qw( reduce );


# Given two pdfs and a page number, appends the given page of the second pdf to the first pdf
sub append_page_to_pdf {
    my ( $pdf1, $pdf2, $pg ) = @_;
    $pdf1->importpage( $pdf2, $pg );
}

# Given two pdfs, appends the second to the first.  Closes pdf2
sub merge_2_pdfs {
    my ($pdf1, $pdf2) = @_;
    map &append_page_to_pdf( $pdf1, $pdf2, $_ ), 1..$pdf2->pages;
    $pdf2->end;
    return $pdf1;
}

# does what it says
sub open_pdf {
    my $file = $_[0];
    my $pdf = PDF::API2->open( $file );
    print "Opened pdf ( $file )\n";
    return $pdf;
}

# reduces merge_2_pdfs over an array of pdfs
sub merge_pdfs {
    my @files = @_;
    my $starting_filename = shift @files;
    my $start_pdf = &open_pdf( $starting_filename );
    my $final_pdf = reduce { &merge_2_pdfs( $a, &open_pdf( $b ) ) } $start_pdf, @files;
    return $final_pdf;
}

# Get the arguments ie save_name, file1, file2, file3, ...
my @files = @ARGV;
my $save_name = shift @files;
my $save = &merge_pdfs( @files );
$save->saveas( $save_name );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:49:58+00:00Added an answer on June 2, 2026 at 10:49 am

    The actual problem in your code is because you shift one of the files off before you merge them.

    my $save_name = shift @files; 
    # which should be 
    my $save_name = $files[0];
    

    Otherwise, the code actually works, and I didn’t find anything garbled.

    A few tips:

    1. use strict and use warnings

    2. The general practice now is to omit the & in your subroutine calls. See here for exceptions to that rule.

    3. In this case, the subroutines make your code verbose, which made it harder to follow. Here’s something more concise.

      use strict;
      use warnings;
      use List::Util 'reduce';
      use PDF::API2;
      
      my $new
        = reduce { $a->importpage($b, $_) foreach 1 .. $b->pages; $a }
          map { PDF::API2->open($_) }
          @ARGV;
      $new->saveas('new.pdf');
      
    4. A simple loop is a little more straighforward to read than using reduce.

      use PDF::API2;
      
      my $new = PDF::API2->new;
      foreach my $filename (@ARGV) {
          my $pdf = PDF::API2->open($filename);
          $new->importpage($pdf, $_) foreach 1 .. $pdf->pages;
      }
      $new->saveas('new.pdf');
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Passwords have lots of downsides but they remain the only technically straightforward way of
We have lots of WCF services that are only used by our own code;
I have a project where I need to generate a PDF file. Within this
I have lots of text marked up like this: <span class=section>[<a href=blablabla>Section</a>]</span> I need
I have lots of variables in an array like this: short num = 7123;
I have lots of Activities chained together with Intents and some Intents require parameters
i have lots of videos in my server and i use the following code
I have lots of few small functions, each executes a query. I want only
I have lots of global read only strings (around 100) that I am using
My problem: I have a pdf with lots of roman characters with complex diacritical

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.