title: How to convert CSV data to PDF table using PDF::Table
body: <p>I need to convert CSV data into into a PDF table using Perl.</p>
  <p>I've tried using <a href="http://search.cpan.org/%7Essimms/PDF-API2-2.031/lib/PDF/API2.pm" rel="nofollow noreferrer"><code>PDF::API2</code></a> and <a href="http://search.cpan.org/%7Eomega/PDF-Table-0.9.12/lib/PDF/Table.pm" rel="nofollow noreferrer"><code>PDF::Table</code></a>, but cannot achieve what I need.</p>
  <p>Can anyone suggest what I have missed, or is there a better module to use?</p>
  <h3>CSV</h3>
  <p><a href="https://i.stack.imgur.com/dffO9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dffO9.png" alt="enter image description here" /></a></p>
  <h3>Expected PDF Output</h3>
  <p><a href="https://i.stack.imgur.com/vqbwC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vqbwC.png" alt="enter image description here" /></a></p>
  <h3>Actual output</h3>
  <p><a href="https://i.stack.imgur.com/At0y7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/At0y7.png" alt="enter image description here" /></a></p>
  <h3>My Perl code</h3>
  <pre><code>use strict;
  
  use lib '/data/fdr/scripts/irp/PDF-API2-2.031/lib';
  use lib '/data/fdr/scripts/irp/PDF-Table-0.9.12/lib';
  
  use PDF::API2;
  use PDF::Table;
  
  my %env_cfg;
  
  my @inp_arg = @ARGV;
  
  my ($ldlm,    $bid,     $lf,      $xf,      $hdrcnt,  $klm,  $sts,
      $iR,      $iC,      $od,      $lst,     $csv,     $cell, $hdr_seq,
      $key_cnt, $dat_rec, $dat_off, $dat_lmt, $len_cnt, $len_cnt1
  ) = ( '', '', '', '', '', '', '', 0, 0, '', '', '', '', 0, 0, '', 0, 0, 0, 0 );
  
  my ( $worksheet, $workbook, $fxls, $keyhdr, $dathdr, $dat_cnt, $csvcol )
          = ( '', '', '', '', '', 0, 0 );
  
  my @csv_rcd = ( '' );
  
  my $header   = '';
  my $data     = '';
  my $somedata = [];
  my $cellval  = '';
  
  my $testfile = &quot;/data/fdr/scripts/config_file/test.txt&quot;;
  
  $sts = open( CSV, &quot;&lt; /data/fdr/output/IRP/FILING/1/Misc_Symbols.csv&quot; );
  
  if ( $sts == 0 ) {
  
      print LOG tmstmp() . &quot;: Error opening CSV file Misc_Symbols.csv\n&quot;;
      print LOG &quot;$ldlm\n&quot;;
  
      next;
  }
  
  my $pdftable = new PDF::Table;
  my $pdf      = PDF::API2-&gt;new();
  my $page     = $pdf-&gt;page();
  
  $page-&gt;mediabox( 'A4' );
  
  my $font = $pdf-&gt;corefont( 'Helvetica-Bold' );
  my $text = $page-&gt;text();
  
  $text-&gt;textstart;
  $text-&gt;font( $font, 12 );
  $text-&gt;translate( 200, 700 );
  
  $iR = -1;
  $iC = 0;
  
  while ( &lt;CSV&gt; ) {
  
      $iR++;
      $iC = 0;    # This is the column co-ordinate for the excel file
  
      $csvcol = -1;    # This is the column co-ordinate for the csv file
      $csv    = $_;
  
      chomp( $csv );
  
      $cellval = '';
      @csv_rcd = split( ',', $csv );
  
      foreach $cell ( @csv_rcd ) {
  
          $csvcol++;
  
          if ( ( ${cell} ne '*' ) ) {
  
              if ( $iR &lt;= 5 ) {
                  $text-&gt;lead( 10 );
                  $text-&gt;section( &quot;${cell}&quot;, 500, 800 );
              }
              else {
                  $cellval = $cellval . $cell;
                  push( @$somedata, [ $cellval . ',' ] );
  
              }
  
          }
          
          $iC++;
      }
  
  }
  
  close CSV;
  
  $pdftable-&gt;table(
  
      # required params
      $pdf,
      $page,
      $somedata,
  
      x       =&gt; 50,
      w       =&gt; 495,
      start_y =&gt; 792,
      next_y  =&gt; 700,
      start_h =&gt; 400,
      next_h  =&gt; 500,
  
      # some optional params
      border          =&gt; 1,
      font_size       =&gt; 20,
      max_word_length =&gt; 20,
      padding         =&gt; 5,
      row_height      =&gt; 30,
  
      column_props    =&gt; [
          {},    # no properties for the first column
          {   min_w            =&gt; 250,
              justify          =&gt; &quot;right&quot;,
              font             =&gt; $pdf-&gt;corefont( &quot;Times&quot;, -encoding =&gt; &quot;latin1&quot; ),
              font_size        =&gt; 14,
              font_color       =&gt; 'white',
              background_color =&gt; '#8CA6C5',
          },
      ],
  );
  
  $text-&gt;textend;
  $pdf-&gt;saveas( &quot;Sample3.pdf&quot; );
  $pdf-&gt;end();
  </code></pre>