マージするPDFドキュメントがたくさんあるので、このコードを作成してマージしました。マージするPDFドキュメントが2つしかない場合は機能しますが、2つ以上指定すると、余分なドキュメントが文字化けして表示されます。何が悪いのかを見つけるのを手伝ってもらえますか?
#!/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 );