【问题标题】:Create PDF report that contains header, text and table in Perl在 Perl 中创建包含标题、文本和表格的 PDF 报告
【发布时间】:2017-05-19 18:42:11
【问题描述】:

我正在尝试创建具有以下要求的报告:

  1. 每一页都有标题(例如作者姓名、报告日期)。
  2. 然后会有一些描述文档的文本(例如设备名称、ip 等)
  3. 然后会有一个只有几页的表格。

我遇到的问题是,每当我结合第 2 步和第 3 步时,每一个都是单独的页面。我希望表格就在文本之后。

我尝试过 PDF::API2、PDF::Table 和 PDF:Reuse(将这两个部分结合起来)。

它们中的每一个仍然出现在单独的页面上。 有什么建议么?

代码如下: 使用 PDF::API2; 使用 PDF::Table; 使用 PDF::重用;

my $pdftable = new PDF::Table;
my $pdf = new PDF::API2(-file => "1.pdf");
my $page = $pdf->page;

$hdr_props = 
{
        # This param could be a pdf core font or user specified TTF.
        #  See PDF::API2 FONT METHODS for more information
        font       => $pdf->corefont("Times", -encoding => "utf8"),
        font_size  => 10,
        font_color => '#006666',
        bg_color   => 'gray',
       repeat     => 1,    # 1/0 eq On/Off  if the header row should be repeated to every new       page
    };


# some data to layout
my $some_data =[
["1 Lorem ipsum dolor",
"Donec odio neque, faucibus vel",
"consequat quis, tincidunt vel, felis."],
["Nulla euismod sem eget neque.",
"Donec odio neque",
"Sed eu velit."],
["Nulla euismod sem eget neque.",
"Donec odio neque",
"Sed eu velit."],
["Nulla euismod sem eget neque.",
"Donec odio neque",
"Sed eu velit."],
["Nulla euismod sem eget neque.",
"Donec odio neque",
"Sed eu velit."],
#... and so on
];

   $left_edge_of_table = 50;
   # build the table layout
  $pdftable->table(
    # required params
    $pdf,
    $page,
    $some_data,
    x => $left_edge_of_table,
    w => 495,
    start_y => 750,
    next_y  => 700,
    start_h => 300,
    next_h  => 500,
    # some optional params
     padding => 5,
     padding_right => 10,
     background_color_odd  => shift @_ || "#FFFFFF",
     background_color_even => shift @_ || "#FFFFCC", #cell background color for even rows
     header_props   => $hdr_props, # see section HEADER ROW PROPERTIES

   );

  $pdf->saveas();



# Open an existing PDF file
$pdf = new PDF::API2(-file => "2.pdf");
$page = $pdf->page;


# Add a built-in font to the PDF
$font = $pdf->corefont('Helvetica-Bold');

# Add some text to the page
$text = $page->text();
$text->font($font, 20);
$text->translate(200, 700);
text->text('Hello World!');

  # Save the PDF
  $pdf->saveas();



prFile("report.pdf");

prDoc("2.pdf");
prDoc("1.pdf");

prEnd();

【问题讨论】:

  • LaTeX 可以工作吗?
  • 这也是我推荐的。 \usepackage{longtable} 并使用 LaTeX::Driver 构建它。
  • 我的 PDF::API2 和 PDF::Table 与生产中的文本在同一页面上。显示一些代码,也许我可以帮助你。
  • 我已经添加了代码......如果你可以看看它。谢谢

标签: perl pdf report


【解决方案1】:

只要我的 2 美分。

重新考虑你的代码:

# Open an existing PDF file
$pdf = new PDF::API2(-file => "2.pdf");
$page = $pdf->page;

您不是要“打开现有的 PDF”,而是要定义您希望能够保存 PDF 的位置(实际保存是另一项任务)。

然后您刚刚添加了一个新页面。注意:每次您拨打$pdf->page 时,您都会在您的 PDF 中添加一个新的空白页。

所以继续使用已经定义的$page 并继续使用 PDF::Table。

【讨论】:

  • 谢谢。它有助于。关键思想是您需要在表格之后(在同一页面中)写文本,然后使用 Y,H 使其出现在表格之前。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
  • 2022-11-10
  • 2019-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多