首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用FPDF更改表中一列的宽度

问使用FPDF更改表中一列的宽度
EN

Stack Overflow用户
提问于 2016-11-22 16:09:08
回答 1查看 2.3K关注 0票数 1

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

我怎样才能使专栏的“名字”比其他栏目更宽?

代码语言:javascript
复制
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列组成:产品引用、名称、价格和单元。我需要名称列比其他列更宽,或者如果可能的话(自动调整)到产品名称。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-22 16:24:29

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

试着把这个放大一倍。

代码语言:javascript
复制
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();
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40746564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档