【问题标题】:FPDF multicell set verticalFPDF 多单元格设置垂直
【发布时间】:2017-07-12 00:37:30
【问题描述】:

我正在使用 FPDF,我想使用 MultiCell() 生成表格,因为我需要它的自动换行属性。试过Cell(),但它无法读取自动换行。如果我使用MultiCell() 属性,那么表格中的所有行都会垂直显示。

如果我使用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', 'Order@No', '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();

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

【问题讨论】:

    标签: php pdf-generation fpdf


    【解决方案1】:

    您可以使用 FPDF 脚本页面中名为 PDF_MC_Table 的类。

    我已经将这个类用于很长的 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']));
    }
    

    【讨论】: