I want to create a pdf file in Perl that includes a table and text before and after the table. I use PDF::API2 and PDF::Table modules for that. My problem that I can't figure out the height of the inserted table. So I don't know where to start the text below the 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);
How can I get the right table height? You can also suggest different modules if the problem can be solved with them.
CodePudding user response:
The documentation for PDF::Table at https://metacpan.org/pod/PDF::Table#table() suggests that the table() method returns the final Y co-ordinate, so change your code to do my ($final_page, $number_of_pages, $final_y) = $table->table( instead of just $table->table( and your $final_y will tell you where the table ends.
This will also help you to handle the case where the table spans multiple pages.
