无法使用 PDF::API2 进行多页打印

发布于 2024-10-17 02:25:26 字数 1022 浏览 1 评论 0原文

我一直在修改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

左耳近心 2024-10-24 02:25:26

问题是您正在创建新页面,但立即忘记了新变量:

if(($i%50) == 0)
{
    my $page = $pdf->page;
    my $fnt = $pdf->corefont('Arial',-encoding => 'latin1');
    my $txt = $page->text;
}

您创建的所有 my 变量在右括号中消失。只需删除 my 即可修改顶级范围中的变量。

编辑:您可能还想在创建新页面时重置$line变量。

The problem is that you are making new page, but forget new variables instantly:

if(($i%50) == 0)
{
    my $page = $pdf->page;
    my $fnt = $pdf->corefont('Arial',-encoding => 'latin1');
    my $txt = $page->text;
}

All my variables you make disappear on closing parentheses. Just remove my and you will modify variables from top-level scope.

Edit: You also probably want to reset $line variable when making new page.

能怎样 2024-10-24 02:25:26

字体 $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.

谈情不如逗狗 2024-10-24 02:25:26

尽管我很喜欢 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文