Toussant Landowner
Toussant Landowner

Reputation: 75

Merge 2 pdf files with PDF:API2

Is there anyone that has an script to merge 2 pdf files with using PDF:API2? I only have this perl module installed and my host is not letting me access to the c compiler for further perl module installs.

Upvotes: 0

Views: 1926

Answers (1)

Éric Guirbal
Éric Guirbal

Reputation: 284

Here is a one shot quality script that show you how to merge several PDF files using PDF::API2.

#!/usr/bin/perl
use strict;
use warnings;

use PDF::API2;

# files to merge
my @input_files = (
    'document1.pdf',
    'document2.pdf',
    'document3.pdf',
);
my $output_file = 'new.pdf';

# the output file
my $output_pdf = PDF::API2->new(-file => $output_file);

foreach my $input_file (@input_files) {
    my $input_pdf = PDF::API2->open($input_file);
    my @numpages = (1..$input_pdf->pages());
    foreach my $numpage (@numpages) {
        # add page number $numpage from $input_file to the end of 
        # the file $output_file
        $output_pdf->importpage($input_pdf,$numpage,0);        
    }
}

$output_pdf->save();

Upvotes: 3

Related Questions