简体   繁体   中英

Indentation misplaced while creating PDF using perl module PDF::API2

I have data in an array and I can write the data into a pdf format using PDF::API2 .But the problem is during the writing process the Indentation(spaces) is not exactly same as in the array

In array format:

 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

In PDF format:

 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 code:

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( );

}

That happens because Arial is not a mono-spaced font . The characters all have different widths. Especially a blank space is usually not very wide. If you want the spacing to stay intact, you need to use a mono-spaced font , such as Courier .

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

That fact is also why PDF::API2 includes a method advancewidth in its PDF::API2::Content class. You can use that to check if a block of text is too wide to fit into a line, and manually wrap it if needed. Of course for your table, that doesn't help.


An alternative to the mono-spaced font might be to use PDF::Table , which can create tables inside a PDF::API2 document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM