如何使用PDF::API2通过Perl将多个PDF合并成一个PDF?
我有很多pdf文档要合并在一起,所以我写了这段代码。它适用于我只有两个pdf文档要合并的情况,但是如果我给它两个以上的文档,额外的文档就会乱码。你能帮我找出哪里不对劲吗如何使用PDF::API2通过Perl将多个PDF合并成一个PDF?,perl,pdf,merge,Perl,Pdf,Merge,我有很多pdf文档要合并在一起,所以我写了这段代码。它适用于我只有两个pdf文档要合并的情况,但是如果我给它两个以上的文档,额外的文档就会乱码。你能帮我找出哪里不对劲吗 #!/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 appe
#!/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 );
代码中的实际问题是,在合并文件之前,您将其中一个文件移出
my $save_name = shift @files;
# which should be
my $save_name = $files[0];
否则,代码实际上是有效的,我没有发现任何乱码
以下是一些提示:
使用严格的
和使用警告
&
。这条规则的例外情况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');
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');
另一种可能是我的图书馆 或者将其在@ARGV上循环。对于两个PDF,我有一个简单的cmdline包装器来执行相同的操作:
file1.pdf file2.pdf out.pdf是的,我在另一个SO线程中看到了类似问题的答案。问题是appendpdf.pl只合并两个pdf文档。我将使用一个使用appendpdf.pl的shell脚本来实现这一点,但它是错误的:每个文件都会出现不必要的文件io(额外的读写操作)。我是appendpdf.pl的作者。这是一个围绕CAM::PDF的简单包装脚本。附加大量PDF是很简单的。只要看看appendpdf.pl源代码,你就会看到。真棒的答案,伙计!我有个问题。为什么&merge_2_pdf的返回值没有输入到原始代码的下一次迭代中?我错了。来自
merge_2_pdfs()
的返回会反馈到下一次迭代中。错误就在上面提到的其他地方。
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');
my $pdf1 = CAM::PDF->new($file1) or die;
my $pdf2 = CAM::PDF->new($file2) or die;
my $pdf3 = CAM::PDF->new($file3) or die;
$pdf1->appendPDF($pdf2);
$pdf1->appendPDF($pdf3);
$pdf1->cleanoutput($outfile);