FPDF table cells margin and rounded corners

2019-06-09 14:59发布

问题:

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

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

Is there any ways to do so?

回答1:

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

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

Example usage:

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

Example output:

Update

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


标签: php pdf fpdf