简体   繁体   English

FPDF 表格单元格边距和圆角

[英]FPDF table cells margin and rounded corners

I am working on the creating PDF report files with tables.我正在使用表格创建 PDF 报告文件。

And I need to have margin between each table cell and rounded corners for each cell also.而且我还需要在每个表格单元格和每个单元格的圆角之间留有边距。 Something like this: http://joxi.net/L21XyyLh8aRnLm像这样的东西: http : //joxi.net/L21XyyLh8aRnLm

Is there any ways to do so?有没有办法做到这一点?

This script from fpdf.org is capable of drawing rounded rectangles.这个来自 fpdf.org 的脚本能够绘制圆角矩形。 Let's extend it to add a $cellspacing variable:让我们扩展它以添加一个$cellspacing变量:

<?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 .除了$border之外,它应该接受与Cell相同的参数。

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

This is ported to Python 3 .这被移植到Python 3
Thanks to Christophe Prugnaud and Maxime Delorme and Matthias Wiehl for the original code.感谢Christophe PrugnaudMaxime DelormeMatthias Wiehl提供原始代码。

from fpdf import FPDF
import math

class ROUND_PDF(FPDF):
    def rounded_cell(self, w, h=0, txt='', border=0, ln=0, align='', fill=False, link='', radius = 1, corners =  (1,2,3,4), cellspacing = 1):
        style = 'S'
        if fill and border:
            style = 'FD'
        elif fill:
            style = 'F'
        self.rounded_rect(self.get_x() + (cellspacing / 2.0), 
            self.get_y() +  (cellspacing / 2.0),
            w - cellspacing, h, radius, corners, style)
        self.cell(w, h + cellspacing, txt, 0, ln, align, False, link)

    def rounded_rect(self, x, y, w, h, r, corners = (1,2,3,4), style = ''):
        k = self.k
        hp = self.h
        if style == 'F':
            op = 'f'
        elif style=='FD' or style == 'DF':
            op='B'
        else:
            op='S'
        my_arc = 4/3 * (math.sqrt(2) - 1)
        self._out('%.2F %.2F m' % ((x+r)*k,(hp-y)*k ))
        xc = x+w-r 
        yc = y+r
        self._out('%.2F %.2F l' % (xc*k,(hp-y)*k ))
        if 2 not in corners:
            self._out('%.2F %.2F l' % ((x+w)*k,(hp-y)*k ))
        else:
            self._arc(xc + r*my_arc, yc - r, xc + r, yc - r*my_arc, xc + r, yc)
        xc = x+w-r
        yc = y+h-r
        self._out('%.2F %.2F l' % ((x+w)*k,(hp-yc)*k))
        if 3 not in corners:
            self._out('%.2F %.2F l' % ((x+w)*k,(hp-(y+h))*k))
        else:
            self._arc(xc + r, yc + r*my_arc, xc + r*my_arc, yc + r, xc, yc + r)
        xc = x+r
        yc = y+h-r
        self._out('%.2F %.2F l' % (xc*k,(hp-(y+h))*k))
        if 4 not in corners:
            self._out('%.2F %.2F l' % ((x)*k,(hp-(y+h))*k))
        else:
            self._arc(xc - r*my_arc, yc + r, xc - r, yc + r*my_arc, xc - r, yc)
        xc = x+r
        yc = y+r
        self._out('%.2F %.2F l' % ((x)*k,(hp-yc)*k ))
        if 1 not in corners:
            self._out('%.2F %.2F l' % ((x)*k,(hp-y)*k ))
            self._out('%.2F %.2F l' % ((x+r)*k,(hp-y)*k ))
        else:
            self._arc(xc - r, yc - r*my_arc, xc - r*my_arc, yc - r, xc, yc - r)
        self._out(op)

    def _arc(self, x1, y1, x2, y2, x3, y3):
        h = self.h
        self._out('%.2F %.2F %.2F %.2F %.2F %.2F c ' % (x1*self.k, (h-y1)*self.k,x2*self.k, (h-y2)*self.k, x3*self.k, (h-y3)*self.k))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM