使用 FPDF 更改 Table 中一列的宽度

Change width of one Column in Table using FPDF

我有一个动态 PHP table,它是通过 FPDF 生成到 PDF 上的。

如何使列 'Name' 比其他列宽?

class PDF extends FPDF {
    function Header() {
        $this->Image('quote-header.png');
        $this->Ln(2);
    }

    function Footer() {
        $this->Image('quote-footer.png');
    }

    function LoadData($file) {
        $lines = file($file);
        $data = array();
        foreach($lines as $line)
        $data[] = explode(';', trim($line));
        return $data;
    }

    function BasicTable($header, $data) {
        foreach($header as $col)
        $this->Cell(40, 7, $col, 1);
        $this->Ln();
        foreach($data as $row) {
            foreach($row as $col)
            $this->Cell(40, 6, $col, 1);
            $this->Ln();
        }
    }
}

$header = array('Product Reference', 'Name', '('. $pound_sign .') Price (excl. VAT)', 'Unit');

我确信这是唯一生成 table?

的代码

任何帮助都会很棒。这是我工作的公司的产品报价系统,如果不解决此列宽问题,我将无法继续前进。

table 由 4 列组成:产品参考、名称、价格和单位。我需要名称列比其他列宽,或者如果可能(自动调整)到产品名称。

Cell方法中的第一个参数是宽度。 http://www.fpdf.org/en/doc/cell.htm

试试这个使尺寸翻倍。

function BasicTable($header, $data) {
    $nameIndex =  array_search ( 'Name' , $header );       

    foreach($header as $key => $col) {
        $width = ($key == $nameIndex) ? 80 : 40;
        $this->Cell($width, 7, $col, 1);            
    }

    $this->Ln();

    // This assumes that $row is an int indexed array
    // E.G looks like array(0 => 'some Product Reference ', 1 => 'Some Name' , 2 =>'Some Price', 3 => 'Some Unit')         
    foreach($data as $row) {
        foreach($row as $key => $col) {
            $width = ($key == $nameIndex) ? 80 : 40;
            $this->Cell($width, 6, $col, 1);
        }
        $this->Ln();
    }

}