猿问

使多个单元格并排 fpdf

所以我知道我应该使用多单元格,这样文本就不会越过单元格并换行。但是,我仍然感到困惑的一件事是如何真正让单元格彼此并排,并且仅在行尾时才继续换行。


我的html表如下:


<table class="experience" id="experience" >

  <tr>

  <td><b>Date From/To</b></td>

  <td><b>Company Name/Address</b></td>

  <td><b>Job Detail and Brief Outline of Dutie</b></td>

  <td><b>Reasons For Leaving</b></td>

  </tr>

  <tr>

  <td><input type="text" name="job_dates[]" id="job_dates" /></td>

  <td><input type="text" name="company_name[]" id="company_name"/></td>

  <td><input type="text" name="details[]" id="details" /></td>

  <td><input type="text" name="leaving[]" id="leaving"/></td>

  </tr>

</table>

<a href="#" title="" class="add-row1">Add Row</a>

用户可以通过单击添加行链接来添加行。这个表只是我表单的一部分,它确实进入了我的 php 文件。现在,当用户填写表单并点击提交时,我的 php 文件获取表值:


$jobDates        = (isset($_POST['job_dates'])   ? $_POST['job_dates']   : array());

$company          = (isset($_POST['company_name'])     ? $_POST['company_name']     : array());

$jobDetails  = (isset($_POST['details']) ? $_POST['details'] : array());

$reasons = (isset($_POST['leaving'])     ? $_POST['leaving']     : array());

目前,我通过执行以下操作在我的 pdf 文件中显示表格:


$pdf->Cell(40,10, 'Work Experience');


$pdf->Ln(20);


$width_cell=array(45,50,30,90);


$pdf->Cell($width_cell[0],10,'Date From/To',1,0); // First header column 

$pdf->Cell($width_cell[1],10,'Company Name',1,0); // Second header column

$pdf->Cell($width_cell[2],10,'Job Duties',1,0); // Third header column 

$pdf->Cell($width_cell[3],10,'Reason for leaving',1,1); // Fourth header column


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

 foreach ($jobDates as $point => $data) {

  $pdf->MultiCell($width_cell[0],10,$data,1,'C'); 

  $pdf->MultiCell($width_cell[1],10,$company[$point],1,'C');

  $pdf->MultiCell($width_cell[2],10,$jobDetails[$point],1,'L');

  $pdf->MultiCell($width_cell[3],10,$reasons[$point],1,'C');;


}

然而,这只会使它们一个接一个地显示在新行上,而不是并排显示。它应该只在进入新行数据时进入新行(如果用户在表单中输入了多行)


我附上了一张图片来显示目前正在发生的事情

largeQ
浏览 182回答 1
1回答

ABOUTYOU

设法让它工作,我将分享,以防其他遇到此问题的人偶然发现此问题。按照教程,我发现我创建了一个名为 mc_table.php 的新 php 页面<?phprequire "fpdf/fpdf.php";class PDF_MC_Table extends FPDF{var $widths;var $aligns;function SetWidths($w){&nbsp; &nbsp; //Set the array of column widths&nbsp; &nbsp; $this->widths=$w;}function SetAligns($a){&nbsp; &nbsp; //Set the array of column alignments&nbsp; &nbsp; $this->aligns=$a;}function Row($data){&nbsp; &nbsp; //Calculate the height of the row&nbsp; &nbsp; $nb=0;&nbsp; &nbsp; for($i=0;$i<count($data);$i++)&nbsp; &nbsp; &nbsp; &nbsp; $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));&nbsp; &nbsp; $h=5*$nb;&nbsp; &nbsp; //Issue a page break first if needed&nbsp; &nbsp; $this->CheckPageBreak($h);&nbsp; &nbsp; //Draw the cells of the row&nbsp; &nbsp; for($i=0;$i<count($data);$i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $w=$this->widths[$i];&nbsp; &nbsp; &nbsp; &nbsp; $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';&nbsp; &nbsp; &nbsp; &nbsp; //Save the current position&nbsp; &nbsp; &nbsp; &nbsp; $x=$this->GetX();&nbsp; &nbsp; &nbsp; &nbsp; $y=$this->GetY();&nbsp; &nbsp; &nbsp; &nbsp; //Draw the border&nbsp; &nbsp; &nbsp; &nbsp; $this->Rect($x,$y,$w,$h);&nbsp; &nbsp; &nbsp; &nbsp; //Print the text&nbsp; &nbsp; &nbsp; &nbsp; $this->MultiCell($w,5,$data[$i],0,$a);&nbsp; &nbsp; &nbsp; &nbsp; //Put the position to the right of the cell&nbsp; &nbsp; &nbsp; &nbsp; $this->SetXY($x+$w,$y);&nbsp; &nbsp; }&nbsp; &nbsp; //Go to the next line&nbsp; &nbsp; $this->Ln($h);}function CheckPageBreak($h){&nbsp; &nbsp; //If the height h would cause an overflow, add a new page immediately&nbsp; &nbsp; if($this->GetY()+$h>$this->PageBreakTrigger)&nbsp; &nbsp; &nbsp; &nbsp; $this->AddPage($this->CurOrientation);}function NbLines($w,$txt){&nbsp; &nbsp; //Computes the number of lines a MultiCell of width w will take&nbsp; &nbsp; $cw=&$this->CurrentFont['cw'];&nbsp; &nbsp; if($w==0)&nbsp; &nbsp; &nbsp; &nbsp; $w=$this->w-$this->rMargin-$this->x;&nbsp; &nbsp; $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;&nbsp; &nbsp; $s=str_replace("\r",'',$txt);&nbsp; &nbsp; $nb=strlen($s);&nbsp; &nbsp; if($nb>0 and $s[$nb-1]=="\n")&nbsp; &nbsp; &nbsp; &nbsp; $nb--;&nbsp; &nbsp; $sep=-1;&nbsp; &nbsp; $i=0;&nbsp; &nbsp; $j=0;&nbsp; &nbsp; $l=0;&nbsp; &nbsp; $nl=1;&nbsp; &nbsp; while($i<$nb)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $c=$s[$i];&nbsp; &nbsp; &nbsp; &nbsp; if($c=="\n")&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sep=-1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $j=$i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $l=0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $nl++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if($c==' ')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sep=$i;&nbsp; &nbsp; &nbsp; &nbsp; $l+=$cw[$c];&nbsp; &nbsp; &nbsp; &nbsp; if($l>$wmax)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($sep==-1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($i==$j)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i=$sep+1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sep=-1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $j=$i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $l=0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $nl++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp; &nbsp; }&nbsp; &nbsp; return $nl;}}?>然后在我的主要 php 页面的顶部,我调用脚本:require('mc_table.php');而不是: $pdf = new FPDF(); 现在是: $pdf=new PDF_MC_Table();并创建我已经完成的表:$width_cell=array(45,50,30,90);$pdf->Cell($width_cell[0],10,'Date From/To',1,0); // First header column&nbsp;$pdf->Cell($width_cell[1],10,'Company Name',1,0); // Second header column$pdf->Cell($width_cell[2],10,'Job Duties',1,0); // Third header column&nbsp;$pdf->Cell($width_cell[3],10,'Reason for leaving',1,1); // Fourth header column$pdf->SetWidths(array(45,50,30,90));foreach ($jobDates as $point => $data) {&nbsp; &nbsp; $pdf->Row(array($data,$company[$point],$jobDetails[$point],$reasons[$point]));}
随时随地看视频慕课网APP
我要回答