2012-11-27 14 views
4

我需要一些帮助,试图在我的PDF表项目上做一个colspan。在这个示例代码中,我可以解释我正在尝试做什么。我需要用“主要信息”和“次要信息”来创建“colspan”行。我还试图让总数“线”在表上拉伸(colspan)以创建一条线来划分此表上的部分。也许我可以通过一个图像来模拟colspan,但是不知道如何通过数据字符串传递图像代码。我到处寻找,我找不到任何解决方案,也许有人在这里做过,可以告诉我如何完成他的任务。 以下是示例代码,并感谢您提前给予帮助!PDF中的Colspan ::表

#!/usr/bin/perl 

use warnings; 
use strict; 
use PDF::API2; 
use PDF::Table; 
use Data::Dumper; 

my $pdftable = new PDF::Table; 
my $pdf = new PDF::API2(-file => "report.pdf"); 
my $page = $pdf->page(); 
$pdf->mediabox('A4'); 

# some data to layout 
my $some_data =[ 

    ['Name', 'Address', 'Account', 'Amount',], 
    ['','','','',], 
    ['Primary Info','','','',], 
    ['','','','',], 
    ['1 - Charles Dawrwin', '1 - Boston, MA', '1 - 123456789', '150.00'], 
    ['2 - Arnold Shuwerts', '2 - San Rose, CA', '2 - 844756485' , '250.00'], 
    ['3 - Joe Doe',   '3 - Revere, MA', '3 - 000034559', '100.00'], 
    ['4 - Mary Loo',   '4 - New York, NY', '4 - 333449687', '125.00'], 
    ['','','','',], 
    ['','','','___________',], 
    ['','Primary Sub Total','','550.00',], 
    ['','','','',], 
    ['1a - Mary Dawrwin', '1a - Cambrigde, MA', '1a - 123452789', '550.00'], 
    ['2a - Joe Doark',  '2a - Miami, FL',  '2a - 844726485' , '350.00'], 
    ['','','','',], 
    ['Secondary Info','','','',], 
    ['','','','',], 
    ['5 - John Dell',   '5 - Portland, ME', '5 - 111000384', '55.00'], 
    ['6 - Mary Loumain',  '6 - Cambridge, MA','6 - 000034569','1,000.00'], 
    ['7 - Charles Town',  '7 - New Port, NH', '7 - 222299944', '200.00'], 
    ['8 - Jasmin Deen',  '8 - San Jose, CL', '8 - 000000122', '255.00'], 

    ['','','','___________',], 
    ['','','Total:','$3,450.00',], 

]; 

my $cell_props; 

my $j = 0; 
foreach my $row (@{$some_data})  
{ 
my $k = 0; 
foreach my $cell (@{$row}) 
{ 

    if (($cell eq 'Name') || ($cell eq 'Address') || ($cell eq 'Account') || ($cell eq 'Amount')) { 

    $cell_props->[$j]->[$k] ={ 
           background_color => '#808080', # 
           font_color  => '#FF0000', # 
           justify => "center", 
          }; 

    }elsif (($cell eq 'Primary Info') || ($cell eq 'Secondary Info')){ 

     $cell_props->[$j]->[$k] = { 
           font => $pdf->corefont("Helvetica", -encoding => "utf8"), 
           font_size => 15, 
           background_color => '#FFCA00', # 
           font_color  => '#4D4D4D', # 
           justify => "left", 
          }; 
    }elsif (($cell eq 'Total:')){ 

     $cell_props->[$j]->[$k] = { 
           background_color => '#FFCA00', # 
           font_color  => '#4D4D4D', # 
           justify => "center", 
          }; 

    }else { 

    $cell_props->[$j]->[$k] = { 
           font => $pdf->corefont("Helvetica", -encoding => "utf8"), 
           font_size => 8, 
           background_color => '#ffffff', # 
           font_color  => '#000000', # 
           justify => "center", 
          }; 
    } 

$k++; 

} 
$j++; 
} 


# build the table layout 
$pdftable->table(
    # required params 
    $pdf, 
    $page, 
    $some_data, # 2D arrayref of text strings 
    x => 50, #left_edge_of_table, 
    -w => 495, # width of table. technically optional, but almost always a good idea to use 
    start_y => 820, # baseline_of_first_line_on_first_page, 
    next_y => 800, # baseline_of_first_line_on_succeeding_pages, 
    -start_h => 800, # height_on_first_page, 
    next_h => 500, # height_on_succeeding_pages, 

    # some optional params 
    -padding => 3, # cell padding 
    padding_right => 10, #right cell padding, overides -pad 
    border => 0, # border width 0 for no border 
    background_color_odd => '', 
    background_color_even => '', #cell background color for even rows 

    header_props => 
     { 
     min_w => 250, 
     bg_color => "#808080", 
     font => $pdf->corefont("Helvetica", -encoding => "utf8"), 
     font_size => 13, 
     font_color => "#000000", 
     repeat => 1, 
     }, 

    column_props => [ map{{justify => 'center' }}1..4, ], 


    cell_props  => 
     $cell_props, 

); 

$pdf->saveas(); 
+0

http://www.perlmonks.org/?node_id=1005755 – toolic

回答

0

直到你找到一个合适的解决方案,也许一个粗糙的黑客会做。

变化

['Primary Info','','','',],

['Primary Info','col$span','col$span','col$span',],

同样地,对于要 “修理” 其他线路。 [显然,你可以在“山坳$跨度”的地方,只要它不会自然出现使用其他任何字符串。]

然后作出“山坳$跨度”的新规则,以使文本的相同的颜色作为背景:

} elsif ($cell eq 'col$span') { 
    $cell_props->[$j]->[$k] = { 
    font => $pdf->corefont("Helvetica", -encoding => "utf8"), 
    font_size => 1, 
    background_color => '#FFCA00', # 
    font_color  => '#FFCA00', # 
}; 

不知道一共是你想要什么,但其他人似乎是你想要的东西,哪怕是欺骗了一下。