无法使用 PDF::API2 进行多页打印
我一直在修改 PDF::API2,我面临一个问题,很好地创建一个 pdf 文件并向其中添加文本。但是,如果要写入的文本超出一页,则脚本不会打印到下一页。我曾尝试研究这个问题的答案,但没有成功。我希望每页正好有 50 行文本。我的脚本如下。它仅打印第一页,创建其他页面,但不打印到其中。任何有解决方案的人
!/usr/bin/perl
use PDF::API2;
use POSIX qw(setsid strftime);
my $filename = scalar(strftime('%F', localtime));
my $pdf = PDF::API2->new(-file => "$filename.pdf");
$pdf->mediabox(595,842);
my $page = $pdf->page;
my $fnt = $pdf->corefont('Arial',-encoding => 'latin1');
my $txt = $page->text;
$txt->textstart;
$txt->font($fnt, 20);
$txt->translate(100,800);
$txt->text("Lines for $filename");
my $i=0;
my $line = 780;
while($i<310)
{
if(($i%50) == 0)
{
my $page = $pdf->page;
my $fnt = $pdf->corefont('Arial',-encoding => 'latin1');
my $txt = $page->text;
}
$txt->font($fnt, 10);
$txt->translate(100,$line);
$txt->text("$i This is the first line");
$line=$line-15;
$i++;
}
$txt->textend;
$pdf->save;
$pdf->end( );
I have been tinkering around with PDF::API2 and i am facing a problem, create a pdf file very well and add text into it. However say if the text to be written flows over to more than one page, the script does not print over to the next page. I have tried researching for an answer to this but to no avail. I would like each page to have exactly 50 lines of text. My script is as below. It only prints on the first page, creates the other pages but does not print into them. Anyone with a solution
!/usr/bin/perl
use PDF::API2;
use POSIX qw(setsid strftime);
my $filename = scalar(strftime('%F', localtime));
my $pdf = PDF::API2->new(-file => "$filename.pdf");
$pdf->mediabox(595,842);
my $page = $pdf->page;
my $fnt = $pdf->corefont('Arial',-encoding => 'latin1');
my $txt = $page->text;
$txt->textstart;
$txt->font($fnt, 20);
$txt->translate(100,800);
$txt->text("Lines for $filename");
my $i=0;
my $line = 780;
while($i<310)
{
if(($i%50) == 0)
{
my $page = $pdf->page;
my $fnt = $pdf->corefont('Arial',-encoding => 'latin1');
my $txt = $page->text;
}
$txt->font($fnt, 10);
$txt->translate(100,$line);
$txt->text("$i This is the first line");
$line=$line-15;
$i++;
}
$txt->textend;
$pdf->save;
$pdf->end( );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您正在创建新页面,但立即忘记了新变量:
您创建的所有
my
变量在右括号中消失。只需删除my
即可修改顶级范围中的变量。编辑:您可能还想在创建新页面时重置
$line
变量。The problem is that you are making new page, but forget new variables instantly:
All
my
variables you make disappear on closing parentheses. Just removemy
and you will modify variables from top-level scope.Edit: You also probably want to reset
$line
variable when making new page.字体 $fnt 不必更改,因为它取决于 PDF $pdf,而不是页面 $page。
The typeface, $fnt, does not have to be changed since it depends on the PDF, $pdf, and not the page, $page.
尽管我很喜欢 Perl,但我也学了足够的 Python 来使用 ReportLabs 库生成 PDF。创建 PDF 是 Perl 与 Python 的弱点之一。
As much as I love Perl, I learned enough Python to use the ReportLabs library for PDF generation. Creating PDF is one of the weak spots of Perl v. Python.