【问题标题】:Indentation misplaced while creating PDF using perl module PDF::API2使用 perl 模块 PDF::API2 创建 PDF 时缩进错位
【发布时间】:2016-04-22 13:30:46
【问题描述】:

我在数组中有数据,我可以使用 PDF::API2 将数据写入 pdf 格式。但问题是在写入过程中,缩进(空格)与数组中的不完全相同

数组格式:

 ATOM      1  N   MET A   0      24.277   8.374  -9.854  1.00 38.41           N   0.174

 ATOM     38  OE2 GLU A   4      37.711  19.692 -12.684  1.00 28.70           O   0.150

PDF 格式:

 ATOM  1 N MET A 0 24.277 8.374-9.8541.0038.41    N 0.174

 ATOM 38 OE2  GLU A 4 37.71119.692-12.684  1.00 28.70    O 0.150 

我的代码:

my $pdf  = PDF::API2->new(-file => "/home/httpd/cgi-bin/new.pdf");
$pdf->mediabox("A4");
my $page = $pdf->page;
my $fnt = $pdf->corefont('Arial',-encoding => 'latin1');
my $txt = $page->text;
$txt->textstart;
$txt->font($fnt, 8); 
$txt->translate(100,800);
$j1=0;
for($i=0;$i<=scalar(@ar_velz);$i++) #Data input to write in PDF
{
$txt->lead(10);
$txt->section("$ar_velz[$i]", 500, 800);    #writing each array index 
if($j1 == 75)                   #To create a page for every 75 lines 
{
$page = $pdf->page;
$fnt = $pdf->corefont('Arial',-encoding => 'latin1');
$txt = $page->text;
$txt->textstart;
$txt->font($fnt, 8); 
$txt->lead(10);
$txt->translate(100,800);
$j1=0;
}
$j1++;
}
$txt->textend;
$pdf->save;
$pdf->end( );

}

【问题讨论】:

    标签: perl pdf


    【解决方案1】:

    这是因为 Arial 不是 mono-spaced font。字符都有不同的宽度。尤其是空白空间通常不是很宽。如果希望间距保持不变,则需要使用等间距的font,例如Courier

    $fnt = $pdf->corefont('Courier',-encoding => 'latin1');
    

    这也是为什么 PDF::API2 includes a method advancewidth 在其 PDF::API2::Content 类中的原因。您可以使用它来检查文本块是否太宽而无法放入一行,并在需要时手动换行。当然,对于您的桌子,这无济于事。


    等宽字体的替代方法可能是使用PDF::Table,它可以在 PDF::API2 文档中创建表格。

    【讨论】:

    • @simbabque:我认为 PDF 文档没有制表位,但找不到关于该主题的任何内容。你知道吗? PDF如何呈现包含水平标签的字符串?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多