我正在尝试使用perl程序在PDF文件中插入JPEG图像。 我正在使用PDF :: Create模块。
我尝试过使用PDF :: Image :: JPEG模块,结果如下 "无法调用方法"图像"在./PDF_IMG.pl"
上的未定义值有没有人可以帮助/建议使用PDF :: Create或PDF :: Image :: JPEG以PDF格式添加图像。
以下是我正在使用的代码段。
use strict;
use Data::Dumper;
use PDF::Create;
use PDF::Image::JPEG;
print "PDF with Image\n";
#Create
my $pdf = new PDF::Create('filename' => "./image_embed_test.pdf",
'Version' => 1.2,
'PageMode' => 'UseNone',
'Author' => 'Madhan',
'Title' => 'My PDF',
);
# Prepare 2 fonts
my $f1 = $pdf->font('Subtype' => 'Type1',
'Encoding' => 'WinAnsiEncoding',
'BaseFont' => 'Helvetica');
my $f2 = $pdf->font('Subtype' => 'Type1',
'Encoding' => 'WinAnsiEncoding',
'BaseFont' => 'Helvetica-Bold');
my $root = $pdf->new_page('MediaBox' => [ 0, 0, 612, 792 ]);
my @page;
$page[1]=$root->new_page;
$page[1]->stringc($f2, 8, 306, 738, "My First Page");
my $image1 = new PDF::Image::JPEG('./logo1.jpg');
$page[2]->image($image1, 100, 100, 1, 2, 1.0, 1.0 ,0, 0, 0);
$pdf->close;
答案 0 :(得分:3)
您的代码有两个问题:
$page[2]
未定义,您需要再次运行$root->new_page()
。image
方法采用键值对,而不是有序参数。这是一个应该有效的更新版本:
use warnings;
use strict;
use PDF::Create;
use PDF::Image::JPEG;
print "PDF with Image\n";
#Create
my $pdf = new PDF::Create(
'filename' => "./image_embed_test.pdf",
'Version' => 1.2,
'PageMode' => 'UseNone',
'Author' => 'Madhan',
'Title' => 'My PDF',
);
# Prepare 2 fonts
my $f1 = $pdf->font('Subtype' => 'Type1',
'Encoding' => 'WinAnsiEncoding',
'BaseFont' => 'Helvetica');
my $f2 = $pdf->font('Subtype' => 'Type1',
'Encoding' => 'WinAnsiEncoding',
'BaseFont' => 'Helvetica-Bold');
my $root = $pdf->new_page('MediaBox' => [ 0, 0, 612, 792 ]);
my @page;
$page[1]=$root->new_page;
$page[1]->stringc($f2, 8, 306, 738, "My First Page");
$page[2]=$root->new_page;
my $jpg1 = $pdf->image('./logo1.jpg');
$page[2]->image( 'image' => $jpg1,
'xscale' => 0.2,
'yscale' => 0.2,
'xpos' => 350,
'ypos' => 400 );
$pdf->close;