【问题标题】:Change width of one Column in Table using FPDF使用 FPDF 更改表格中一列的宽度
【发布时间】:2017-04-06 09:44:44
【问题描述】:

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

如何使“名称”列比其他列更宽?

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');

我确信这是生成表格的唯一代码?

任何帮助都会很棒。这是针对我工作的公司的产品报价系统,如果不解决此列宽问题,我将无法取得进一步进展。

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

【问题讨论】:

    标签: php fpdf


    【解决方案1】:

    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();
        }
    
    }
    

    【讨论】:

    • 这不行,我的桌子到处都是?
    • 感激不尽。有没有办法限制表格宽度超过页边距?左边距与右边距相同会很棒。
    • 不幸的是,FPDF 不跟踪当前位置。因此,为了防止脚本从页面中注销,您需要跟踪循环中每一行的写入量。您想在哪里停下并向下移动取决于您的纸张大小、字体等。
    • 每行只有大约 10 个单词,没有任何内容会超过一页的长度。有什么建议吗?
    • 如果您只想添加边距,请查看SetMargins 方法。 fpdf.org/en/doc/setmargins.htm
    猜你喜欢
    • 1970-01-01
    • 2017-10-06
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 2013-05-04
    • 2012-01-02
    相关资源
    最近更新 更多