5

Are there any perl modules to draw a table in a pdf at specified position with given rows and columns with an empty content in each cell?

1
  • The solutions below are extremely limited. For instance, you can't easily change only the interior cell borders of a table or even make center aligned cells. So you must re-write the functionality from scratch, it seems?
    – sventechie
    Sep 23, 2011 at 21:00

1 Answer 1

11

Two come to mind:


I produced a simple table using PDF::Table like so:

use PDF::API2;
use PDF::Table;

my $pdf   = PDF::API2->new( -file => 'table.pdf' );
my $table = PDF::Table->new;
my $page  = $pdf->page;

my $data = [
  [ 'A1', 'A2', 'A3' ],
  [ 'B1', 'B2', 'B3' ],
  [ 'C1', 'C2', 'C3' ],
];

$table->table( $pdf, $page, $data,
               x       => 50,
               w       => 495,
               start_y => 750,
               next_y  => 700,
               start_h => 300,
               next_h  => 500,
);

$pdf->save;


And with PDF::Report::Table like this:

use PDF::Report;
use PDF::Report::Table;

my $pdf   = PDF::Report->new( PageSize => 'A4', PageOrientation => 'Portrait' );
my $table = PDF::Report::Table->new( $pdf );

my $data = [
  [ 'A1', 'A2', 'A3' ],
  [ 'B1', 'B2', 'B3' ],
  [ 'C1', 'C2', 'C3' ],
];

$pdf->openpage;
$pdf->setAddTextPos( 50, 50  );
$table->addTable( $data, 400 );   # 400 is table width

$pdf->saveAs( 'table.pdf' );
2
  • my $table = PDF::Table->new; That is how the PDF::Table documentation says to do it, but with the latest version of it: $ perl -e 'use PDF::Table; $table = PDF::Table->new'<br /> Warning: Page must be a PDF::API2::Page object but it seems to be: at -e line 1. Error: Cannot set page from passed PDF object either as it is invalid! at -e line 1. (BTW the formatting help that says to start code with 4 spaces and insert a linebreak by ending with 2 spaces is wrong.)
    – Phil Goetz
    May 29, 2015 at 18:09
  • @PhilGoetz - Its a bug thats been introduced since I posted above. I've just tested with version of PDF::Table that most likely I used at time of answer and it works fine (version 0.02).
    – draegtun
    Jun 4, 2015 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.