欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

php 文字换行,用Php中的Fpdf换行文本

发布时间:2025/3/20 27 豆豆
生活随笔 收集整理的这篇文章主要介绍了 php 文字换行,用Php中的Fpdf换行文本 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

使用MultiCell()代替Cell()

改变这个:

$pdf->Cell(20,7,'Hi5(xtra)',1);

至:

$pdf->MultiCell( 20, 7, 'Hi5(xtra)', 1);

编辑:

我可以看到MultiCell()打破了这条线,因此新单元格将被放置在当前位置之下.

在这种情况下,您可以计算x和y坐标,并在输出每个单元格后计算新位置和设置位置.

require('fpdf.php');

$pdf = new FPDF();

$pdf->AddPage();

$start_x=$pdf->GetX(); //initial x (start of column position)

$current_y = $pdf->GetY();

$current_x = $pdf->GetX();

$cell_width = 20; //define cell width

$cell_height=7; //define cell height

$pdf->SetFont('Arial','',16);

$pdf->MultiCell($cell_width,$cell_height,'Hi1',1); //print one cell value

$current_x+=$cell_width; //calculate position for next cell

$pdf->SetXY($current_x, $current_y); //set position for next cell to print

$pdf->MultiCell($cell_width,$cell_height,'Hi2',1); //printing next cell

$current_x+=$cell_width; //re-calculate position for next cell

$pdf->SetXY($current_x, $current_y); //set position for next cell

$pdf->MultiCell($cell_width,$cell_height,'Hi3',1);

$current_x+=$cell_width;

$pdf->Ln();

$current_x=$start_x; //set x to start_x (beginning of line)

$current_y+=$cell_height; //increase y by cell_height to print on next line

$pdf->SetXY($current_x, $current_y);

$pdf->MultiCell($cell_width,$cell_height,'Hi4',1);

$current_x+=$cell_width;

$pdf->SetXY($current_x, $current_y);

$pdf->MultiCell($cell_width,$cell_height,'Hi5(xtra)',1);

$current_x+=$cell_width;

$pdf->SetXY($current_x, $current_y);

$pdf->MultiCell($cell_width,$cell_height,'Hi5',1);

$current_x+=$cell_width;

$pdf->SetXY($current_x, $current_y);

$pdf->Output();

?>

总结

以上是生活随笔为你收集整理的php 文字换行,用Php中的Fpdf换行文本的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。