如何从 PDF::Table 获取 table 高度?

How can I get table height from PDF::Table?

我想在 Perl 中创建一个 pdf 文件,其中包含 table 以及 table 前后的文本。我为此使用 PDF::API2 and PDF::Table 模块。我的问题是我无法计算出插入的 table 的高度。所以我不知道从哪里开始 table.

下面的文字
use PDF::API2;
use PDF::Table;

my $pdf = PDF::API2->new();
$pdf->mediabox('A4');
my $page = $pdf->page();

my $font = $pdf->corefont('Helvetica');
my $fontsize = 12;
my $x = 20/mm; # left and right edge
my $y = 280/mm;
my $textwidth = 210/mm - 2*$x;
my $height = $fontsize * 2;

my $text = $page->text();
$text->font($font, $fontsize);
$text->translate($x, $y);

$text->paragraph('line above table', $textwidth, $height);
$y -= $height;

my $table = new PDF::Table;
$table->table(
    $pdf,
    $page,
    [
        ["Cell 1", "Cell 2"],
        ["Cell 3", "Cell 4"],
        ["Cell 5", "Cell 6"],
        ["Cell 7", "content longer than one line - content longer than one line - content longer than one line - content longer than one line"],
    ],
    'x' => $x,
    'w' => $textwidth,
    'y' => $y,
    'h' => $y - 20/mm,
    font => $font,
    font_size => $fontsize,
);

my $table_height = $fontsize * 10; # this is just estimated - how can I get the right value?
$y -= $table_height;
$text->translate($x, $y);

$text->paragraph('line below table', $textwidth, $height);

如何获得合适的 table 高度?如果可以解决问题,也可以推荐不同的模块。

https://metacpan.org/pod/PDF::Table#table()PDF::Table 的文档建议 table() 方法 returns 最终的 Y 坐标,因此更改代码以执行 my ($final_page, $number_of_pages, $final_y) = $table->table( 而不仅仅是 $table->table(,您的 $final_y 会告诉您 table 结束的位置。

这也将帮助您处理 table 跨越多个页面的情况。