-2

I am using Perl to generate a PDF file with the PDF::API2 module.

I am having difficulty writing data to the file in the required format.

Currently this is how it appears in the PDF file.

Currently looks like this in PDF

Here's my script

my %data = (
    '1' => {
        'SEQUENCE' => '1',
        'NAME'     => 'John',
        'ADDR1'    => 'Road 1',
        'GRADE'    => '5'
    },
    '2' => {
        'SEQUENCE' => '2',
        'NAME'     => 'Smith',
        'ADDR1'    => 'Road 2',
        'GRADE'    => '6'
    }
);

...
...

my @rows = qw( NAME ADDR1 GRADE );

for my $id (sort keys %data){
    push @tbl, [ @{$data{$id}}{@rows} ];
    ($name, $addr, $grade) = ($data{$id}{'NAME'}, $data{$id}{'ADDR1'}, $data{$id}{'GRADE'});
} 

...
...

my $pdftable = new PDF::Table;
$pdftable->table(
     $pdf,
     $page,
     \@tbl,
     x => 50,
     w => 400,
     start_y => 630,
     start_h => 630,
     next_y  => 630,
     next_h  => 630,
     padding => 5,
     border => 1,
     padding_right => 10,
);
$pdf->saveas();

But I want it to to appear like this:

It should be formatted like this

I know I have to make a modification to @tbl data, but how?

Borodin
  • 126,100
  • 9
  • 70
  • 144
vkk05
  • 3,137
  • 11
  • 25

1 Answers1

-1

The PDF::Table table function takes an "array or arrays" as its third argument. You currently have it strucutred like:

my @tbl = ( [$name,$addr,$grade], [...] )

Each nested array (what's inside the [] brackets) is a new row of the table, and the elements to that array are the cells of that row. So instead, you want it structured:

my @tbl = ( ['NAME',$name], ['ADDR1',$addr], ['GRADE',$grade], [...] ) 

To construct it using your %data structure:

for my $id (sort keys %data) {
    push @tbl, [$_, $data{$id}->{$_}] for qw/NAME ADDR1 GRADE/;
    push @tbl, [qw/- -/]; # to add a blank row as a separator
}
beasy
  • 1,227
  • 8
  • 16