|
I Hope someone can help me!!!
I am trying to generate a pdf file that is of a user defined size.
This means that the size could be as little as 3cm wide. I am having trouble with text wrapping because using the normal text wrap function you can only define the wrap point in characters. If the text is "WMWMWMWM WMWWMWM" it is much longer than "iiiiii iiiiii" but with the same amount of characters.
So I am trying to use a subroutine that uses the string_width function of PDF create to determine the actual width of each word, add it to an array until it exceeds the document size, then start a new line (array element)
The problem is the string_width only seems to calculate and return the first word of the array. The next element in the loop is not calculated, but the value of the first is used for every word.
I'm new to perl, so please, If anyone knows what is wrong with this script - or has a better solution I would very much appreciate some help!!! (spent Hrs on this already!!)
subroutine:
sub wraptext{
my($txt, $docwidth, $txtfont) = @_;
my @lines;
my @words;
my $wordlngth;
my $linetxt;
my $txtpage;
my $pdftext;
my $font;
my $pdftxtlngth = "$config{'image_upload_dir'}/pdf/textlength.pdf";
# my $pdfregfile2 = "$itemnum.pdf";
$pdftext = new PDF::Create('filename' => $pdftxtlngth,
'Version' => 1.2,
'PageMode' => "UseNone",
'Author' => "$config{'auction_title'}",
'Title' => "Get text length",
'Subject' => "text length",
);
my $roottxt = $pdftext->new_page('MediaBox' => [ 0, 0, 594, 820 ]);
my $pagetxt = $roottxt->new_page;
if ($txtfont eq 'std'){
$font = $pdftext->font('Subtype' => 'Type1','Encoding' => 'WinAnsiEncoding','BaseFont
+' => 'Helvetica');
} elsif ($txtfont eq 'bold'){
$font = $pdftext->font('Subtype' => 'Type1','Encoding' => 'WinAnsiEncoding','BaseFont' => 'Helve
+tica-Bold');
}
my $linelngth;
$txt =~ s/\n/ /m; #replace all returns with a space
@words = split(' ',$txt); #split text to words
my $n = 0;
my $x;
my wordlength;
my $count=1;
for ($i=0; $i <=$#words; $i++) {
$wordlength = $pagetxt->string_width($font,"$words[$i]");
$x = $wordlength[$i] * 8; #convert size
$linelngth = $linelngth + $x;
if ($linelngth < $docwidth) {
$linetxt .= " " . $words[$i];
$n = $n + 1;
} else {
$linetxt .= "\n";
push(@lines,$linetxt);
$count++;
$i=$i-1;
$linetxt = "";
$linelngth = 0;
}
}
return(@lines);
}
|