问题描述
我正在尝试使用FPDF在单元格中换行.这是我的代码.
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',16);
$pdf->Cell(20,7,'Hi1',1);
$pdf->Cell(20,7,'Hi2',1);
$pdf->Cell(20,7,'Hi3',1);
$pdf->Ln();
$pdf->Cell(20,7,'Hi4',1);
$pdf->Cell(20,7,'Hi5(xtra)',1);
$pdf->Cell(20,7,'Hi5',1);
$pdf->Output();
?>
此代码的输出如下所示:
现在,我想将Xtra文本包装在单元格中. Xtra文本应进入第二行.我该怎么做.
当我将MultiCell用于该行时$ pdf-> MultiCell(20,7,'Hi5(xtra)',1);它正在更改为以下内容.
我已经尝试过Log1c提到的答案就是这样
使用MultiCell()
代替Cell()
更改此:
$pdf->Cell(20,7,'Hi5(xtra)',1);
收件人:
$pdf->MultiCell( 20, 7, 'Hi5(xtra)', 1);
MultiCell()用于打印多行文本. /p>
我可以看到MultiCell()
中断了该行,因此新单元格将被放置在当前位置下方.
在这种情况下,可以在输出每个单元格后计算x
和y
坐标,并计算新位置和设置位置.
<?php
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();
?>
I am trying to Wrap a text in the Cell using FPDF. here is my code.
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',16);
$pdf->Cell(20,7,'Hi1',1);
$pdf->Cell(20,7,'Hi2',1);
$pdf->Cell(20,7,'Hi3',1);
$pdf->Ln();
$pdf->Cell(20,7,'Hi4',1);
$pdf->Cell(20,7,'Hi5(xtra)',1);
$pdf->Cell(20,7,'Hi5',1);
$pdf->Output();
?>
The output for this code looks like this
Now I want to Wrap that Xtra text which is there into the Cell. The xtra text should go into the second line. How should I do that.
when I use MultiCell for that line $pdf->MultiCell( 20, 7, 'Hi5(xtra)', 1); It is changing into the following..
I have tried the Answer mentioned by Log1c It came out this way
Use MultiCell()
instead Cell()
Change this:
$pdf->Cell(20,7,'Hi5(xtra)',1);
To:
$pdf->MultiCell( 20, 7, 'Hi5(xtra)', 1);
The MultiCell() is used for print text with multiple lines.
EDIT:
I can see that MultiCell()
, breaks the line so new cell will be placed below current position.
In such case you can calculate x
and y
co-ordinate and calculate new position and set position after outputting every cell.
<?php
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中的Fpdf中换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!