2017-02-22 89 views
0

我正在使用FPDF,我想用MultiCell()生成表格,因为我需要它的单词wrap属性。试过Cell(),但它无法读取换行。如果我使用MultiCell()属性,则表中的所有行将垂直显示。FPDF多单元集垂直

PDF

如果我用multicell()所以我的PDF数据显示垂直。请建议我如何做到垂直于水平。

我试图代码:

require('../fpdf.php'); 

class PDF extends FPDF 
{ 
// Load data 
function LoadData($file) 
{ 
    // Read file lines 
    $lines = file($file); 
    $data = array(); 
    foreach($lines as $line) 
     $data[] = explode(';',trim($line)); 
    return $data; 
} 

// Simple table 
function BasicTable($header, $data, $pdf) 
{ 
    // Header 
    foreach($header as $col) 
     $this->Cell(40, 20, $col, 1, 0, 'C', false); 
    $this->Ln(); 
    // Data 
    foreach($data as $row) 
    { 
     foreach($row as $col) 
      //$word = str_word_count($col); 
      //$this->MultiCell(30, 10,$col, 0, 'J', 0, 1, '', '', true, null, true); 
      //$this->word_wrap($pdf,$col); 
      //$this->cell(40,6,$col,1); 
      $this->MultiCell(40,6,$col,1); 
     $this->Ln(); 
    } 
} 

} 

$pdf = new PDF('P','mm',array(600,600)); 
// Column headings 
$header = array('Waybill', '[email protected]', 'Consignee Name', 'Consignee Pincode', 'Consignee City', 'Weight', 'COD Amount', 'Product', 'Shipping Client', 'Seller Name', 'Seller Pincode', 'Seller City'); 
// Data loading 
$data = $pdf->LoadData('countries.txt'); 
$pdf->SetFont('Arial','',14); 
$pdf->AddPage(); 
$pdf->BasicTable($header,$data, $pdf); 
$pdf->Output(); 

或者请建议我如何自动换行文本

回答

0

你可以使用一个叫做PDF_MC_Table类是在FPDF脚本页面。

我已经使用这个类作为一个非常长的PDF报告列表,并且它对文本非常有效。请记住,数据应该以表格的一行为每个$array[$x]的位置存储在一个二维数组中;所以使用foreach循环可以使用$pdf->Row()函数打印表格行。

以下是一些示例代码。

$data[0]['name'] = 'Some string'; 
$data[0]['address'] = 'Address of the person'; 
$data[0]['telephone'] = 'the telephone number'; 

$data[1]['name'] = 'Other Person'; 
$data[1]['address'] = 'Address of the other person'; 
$data[1]['telephone'] = 'Another phone number'; 

$pdf->SetWidths(array(30,60,30)); 

$pdf->Row(array('Name','Address','Telephone')); //Set table header 
foreach ($data as $value) { 
    $pdf->Row(array($value['name'],$value['address'],$value['telephone'])); 
}