我正在使用FPDF脚本开发.pdf文件。除此之外,还使用函数WriteHTML()从HTML构建.pdf文件的一部分。除了证明文本的HTML部分合理之外,其他所有方法都工作正常。我正在更改HTML_TO_PDF脚本和原始的FPDF脚本。当我尝试使用函数MultiCell()编写文本时,它可以使文本对齐,但是粗体部分会放在新行中。我想删除该功能。然后,我想到了转换FPDF的Write()函数的方法,以便它可以证明文本合理。我将部分代码从MultiCell()添加到Write()函数中,以进行文本对齐。结果是文本对齐,但是有些重叠,我无法删除。当文本变为粗体时,会出现重叠。我不太确定如何消除重叠部分。我试图操纵用于计算ws(字距)的变量,但没有成功。没有文本对齐部分的Write()函数,为我提供了没有重叠的左对齐文本。我想证明文本没有重叠。任何建议都会有所帮助:)
这是transform Write()函数:
function Write($h, $txt, $link='', $align='J')
{
// Output text in flowing mode
if(!isset($this->CurrentFont))
$this->Error('No font has been set');
$cw = &$this->CurrentFont['cw'];
$w = $this->w-$this->rMargin-$this->x;
$wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
$s = str_replace("\r",'',$txt);
$nb = strlen($s);
$sep = -1;
$i = 0;
$j = 0;
$l = 0;
$ns = 0; //I copied this from MultiCell() function
$nl = 1;
while($i<$nb)
{
// Get next character
$c = $s[$i];
if($c=="\n")
{
// Explicit line break
$this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',false,$link);
$i++;
$sep = -1;
$j = $i;
$l = 0;
if($nl==1)
{
$this->x = $this->lMargin;
$w = $this->w-$this->rMargin-$this->x;
$wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
}
$ns = 0;
$nl++;
continue;
}
if($c==' '){
$sep = $i;
$ls = $l; //also this part
$ns++; //this too
}
$l += $cw[$c];
if($l>$wmax)
{
// Automatic line break
if($sep==-1)
{
if($this->x>$this->lMargin)
{
// Move to next line
$this->x = $this->lMargin;
$this->y += $h;
$w = $this->w-$this->rMargin-$this->x;
$wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
$i++;
$nl++;
continue;
}
if($i==$j)
$i++;
$this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',false,$link);
}
else
{
//this is part for text align
if($align=='J')
{
$this->ws = ($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;
$this->_out(sprintf('%.3F Tw',$this->ws*$this->k));
//echo $this->ws*$this->k . "\n";
}
//to here
$this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',false,$link);
$i = $sep+1;
}
$sep = -1;
$j = $i;
$l = 0;
$ns = 0;
if($nl==1)
{
$this->x = $this->lMargin;
$w = $this->w-$this->rMargin-$this->x;
$wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
}
$nl++;
}
else
$i++;
}
// Last chunk
if($i!=$j)
$this->Cell($l/1000*$this->FontSize,$h,substr($s,$j),0,0,'',false,$link);
}
此更改的结果如图所示: