FPDF表格单元格的边距和圆角 [英] FPDF table cells margin and rounded corners

查看:118
本文介绍了FPDF表格单元格的边距和圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用表格创建PDF报告文件.

I am working on the creating PDF report files with tables.

我还需要在每个表格单元格和每个单元格的圆角之间留出边距.像这样的东西: http://joxi.net/L21XyyLh8aRnLm

And I need to have margin between each table cell and rounded corners for each cell also. Something like this: http://joxi.net/L21XyyLh8aRnLm

有什么办法吗?

推荐答案

此脚本来自fpdf.org 可以绘制圆角矩形.让我们扩展它以添加一个$cellspacing变量:

This script from fpdf.org is capable of drawing rounded rectangles. Let's extend it to add a $cellspacing variable:

<?php
require('rounded_rect.php');

class Bohdan_PDF extends PDF {
    var $cellspacing = 1;

    function SetCellspacing($cellspacing) {
        $this->cellspacing = $cellspacing;
    }

接下来,让我们添加一个方法,该方法输出一个单元格,在其周围绘制一个圆角矩形,并添加单元格间距.除了$border,它应接受与Cell相同的参数.

Next, let's add a method which outputs a cell, drawing a rounded rectangle around it, and adding the cellspacing. It should accept the same arguments as Cell except for $border.

    function RoundedBorderCell($w, $h=0, $txt='', $ln=0, $align='',
            $fill=false, $link='') {
        $this->RoundedRect($this->getX() + $this->cellspacing / 2,
            $this->getY() + $this->cellspacing / 2,
            $w - $this->cellspacing, $h, 1, 'DF');
        $this->Cell($w, $h + $this->cellspacing, $txt, $ln, 0, $align,
            $fill, $link);
    }

以下方法负责输出表格.

The following method takes care of outputting the table.

    function BohdanTable($header, $data, $widths, $aligns) {
        $this->SetLineWidth(0.15);
        $rowHeight = $this->FontSizePt - 2;

        $this->SetFillColor(255, 255, 0);
        for ($i = 0, $j = sizeof($header); $i < $j; $i++) {
            $this->RoundedBorderCell($widths[$i], $rowHeight,
                $header[$i], 0, $aligns[$i]);
        }
        $this->Ln();

        foreach ($data as $rowId => $row) {
            $this->SetFillColor($rowId % 2 == 1 ? 240 : 255);
            for ($i = 0, $j = sizeof($row); $i < $j; $i++) {
                $this->RoundedBorderCell($widths[$i], $rowHeight,
                    $row[$i], 0, $aligns[$i]);
            }
            $this->Ln();
        }
    }
}

示例用法:

$pdf = new Bohdan_PDF();
$pdf->AddPage();
$pdf->SetFont("Arial", "", 9);
$pdf->BohdanTable(array_fill(0, 4, "Building Name"),
    array(
        array("Administration Building", "46,314", "1,471,818", "4%"),
        array("Alumni House", "4,939", "1,471,818", "400%"),
        array("Alumni House Garage", "347", "1,471,818", "34%"),
        array("Alumni House Garden House", "165", "1,471,818", "16%")
    ),
    array(105, 26, 26, 26),
    array('L', 'R', 'R', 'R'));
$pdf->Output();

示例输出:

这解决了注释中描述的分页符问题.该代码检查当前页面上是否有足够的空间来添加下一行,如果没有,则将该行放在新页面上.

This fixes the page break problem described in the comments. This code checks if there is enough space left on the current page to add the next row, and if there isn't, it puts the row on a new page.

    function RoundedBorderCell($w, $h=0, $txt='', $ln=0, $align='',
            $fill=false, $link='') {
        $spaceLeft = ($this->h - $this->getY() - $this->bMargin);
        $cellHeight = $h + $this->cellspacing;
        if ($spaceLeft < $cellHeight) {
            $this->AddPage();
        }
        $this->RoundedRect($this->getX() + $this->cellspacing / 2,
            $this->getY() + $this->cellspacing / 2,
            $w - $this->cellspacing, $h, 1, 'DF');
        $this->Cell($w, $cellHeight, $txt, $ln, 0, $align, $fill, $link);
    }

这篇关于FPDF表格单元格的边距和圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆