我有2个.pdf文件。两者都是1页。我用它们作为发票的水印。填写第1页(第1页pdf)后,我想在第1页之后导入第2页(第2页pdf)并写入。显然如果第1页没有填写继续写入它。这是我写的测试代码:
my $pdf = PDF::API2->new();
my $sec_page = PDF::API2->open('./useful_scripts/invoice_page_2.pdf');
$pdf = PDF::API2->open('./useful_scripts/invoice.pdf');
my $page = $pdf->openpage(1);
my $text = $page->text();
my $font = $pdf->corefont('Helvetica-Bold');
$text->fillcolor('black');
$text->font($font, 11);
$text->translate(170, 785);
$text->text($invoice->ott_invoice_number);
my ( $y, $d, $x ) = ( 718, 15, 57 );
foreach my $line ( @{ $invoice->invoice_lines } ) {
$text->translate( $x, $y );
my $product = join( ': ', @{ $line->{display} } );
$text->text($product);
$y = $y - 13;
if ( $line->{options} ) {
foreach my $option ( @{ $line->{options} } ) {
$text->translate( $x + 10, $y );
$text->text($option);
}
}
}
$y = 100; #This is for testing purposes
#so that I can make sure it is
#writing to page 2
if( $y < 150 ){
$page = $pdf->importpage($sec_page);
$page = $pdf->openpage(2);
$text = $page->text();
$text->translate(100,200);
$text->text('Some test text...'); #This is LINE 59
}
#END
$pdf->saveas('./test.pdf');
say 'Printing done... ;)';
然而,在第59行(我已经评论为第59行)我收到一个错误说:
Can't call method "isvirtual" on an undefined value at /usr/local/lib/perl5/site_perl/5.14.2/PDF/API2/Content.pm line 1558.
我已经阅读了类似问题的类似问题,并尝试了已建议的解决方案。但似乎没有一个对我有用。有什么想法吗?
答案 0 :(得分:3)
为第二页创建$text
对象时,需要再次调用$text->font(...)
。它不会从第一页开始延续。
在第57行和第59行之间添加以下内容:
$text->font($font, 11);