我想在 Perl 中創建一個 pdf 檔案,其中包含一個表格以及表格前后的文本。我為此使用PDF::API2和PDF::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);
如何獲得合適的桌子高度?如果可以解決問題,您還可以建議不同的模塊。
uj5u.com熱心網友回復:
對于檔案PDF::Table在https://metacpan.org/pod/PDF::Table#table()表明,該table()方法回傳最終Y坐標,那么改變你的代碼來執行my ($final_page, $number_of_pages, $final_y) = $table->table(,而不是僅僅$table->table(和你$final_y會告訴你在哪里表結束。
這也將幫助您處理表格跨越多個頁面的情況。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/403822.html
標籤:
上一篇:方法assertThat(List<Flight>)對于SpringDataOverviewApplicationTestsJava(67108964)型別未定義
