matthew-elisha/fpdf-easytable

FPDF EasyTable

dev-master 2017-08-17 14:15 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:03:29 UTC


README

这是一个PHP类,它提供了一种简单的方法来生成由FPDF库生成的PDF文档中的表格。

使用此类制作PDF文档的表格就像制作HTML表格一样简单灵活。我们可以使用CSS风格的字符串以相同的方式自定义表格的外观,就像HTML表格使用CSS进行样式化一样。

无需编写混乱的代码,无需理解属性和文本的复杂数组。

无需复杂的配置文件。

使用easyTable构建和样式化表格简单、干净且快速。

 $table=new easyTable($pdf, 3, 'width:100;');
 
 $table->easyCell('Text 1', 'rowspan:2; valign:T'); 
 $table->easyCell('Text 2', 'bgcolor:#b3ccff; rowspan:2');
 $table->easyCell('Text 3');
 $table->printRow();
 
 $table->rowStyle('min-height:20');
 $table->easyCell('Text 4', 'bgcolor:#3377ff; rowspan:2');
 $table->printRow();

 $table->easyCell('Text 5', 'bgcolor:#99bbff;colspan:2');
 $table->printRow();
 
 $table->endTable();

内容

功能

  • 表格和列宽度可以用毫米或百分比定义

  • 每个单元格都是可完全自定义的(字体族、字体大小、字体颜色、背景颜色、文本位置、垂直填充、水平填充...)

  • 单元格可以跨越多个列和行

  • 表格在页面分割时会自动拆分

  • 有一组标题行,因此可以在每一页自动添加标题

  • 属性值可以在全局、表格级别(影响表格中的所有单元格)、行级别(只影响该行中的单元格)或本地单元格级别定义

  • 如果行不适合当前页面,可以设置行在页面分割时拆分,或打印在下一页

  • 可以向表格单元格添加图片

  • 可以在单元格中的图片上方或下方添加文本

  • UTF8支持

比较

easyTable 与 kind-of-HTML-to-PDF的比较

使用HTML代码来制作PDF文档的表格是一种马虎的解决方案。首先,要将HTML代码转换为类似FPDF的格式,需要解析器,这意味着性能上有所损失;其次,结果非常糟糕。

HTML代码

<table align:"right" style="border-collapse:collapse">
  <tr>
    <td rowspan="2" style="width:30%; background:#ffb3ec;">Text 1</td>
    <td colspan="2" style="background:#FF66AA;">Text 2</td>
  </tr>
  <tr>
    <td style="width:35%; background:#33ffff;">Text 3 </td>
    <td style="width:35%; background:#ffff33;">Text 4 </td>
  </tr>
</table>

easyTable代码

$table=new easyTable($pdf, '%{30, 35, 35}', 'align:R; border:1');
  $table->easyCell('Text 1', 'rowspan:2; bgcolor:#ffb3ec');
  $table->easyCell('Text 2', 'colspan:2; bgcolor:#FF66AA');
  $table->printRow();

  $table->easyCell('Text 3', 'bgcolor:#33ffff');
  $table->easyCell('Text 4', 'bgcolor:#ffff33');
  $table->printRow();
$table->endTable(5);

示例

要求

  • PHP 5.6或更高版本
  • FPDF 1.81.
  • exfpdf.php(包含在本项目中)

手动安装

下载EasyTable类和FPDF类,并将内容放入项目结构中的目录中。确保您使用的是FPDF 1.81

快速入门

  • 使用exfpdf类(FPDF类的扩展)创建一个FPDF对象
  • 创建一个easyTable对象
    $table=new easyTable($pdf, 3, 'border:1');
  • 添加一些行
    $table->easyCell('Text 1', 'valign:T'); 
    $table->easyCell('Text 2', 'bgcolor:#b3ccff;');
    $table->easyCell('Text 3');
    $table->printRow();

    $table->rowStyle('min-height:20; align:{C}');   // let's adjust the height of this row
    $table->easyCell('Text 4', 'colspan:3');
    $table->printRow();
  • 完成后,不要忘记结束表格
    $table->endTable(4);

结果

文档

function __construct( FPDF-object $fpdf_obj, Mix $num_cols[, string $style = '' ])

描述

构造easyTable对象

参数

fpdf_obj

the current FPDF object (constructed with the FPDF library)  
that is being used to write the current PDF document

num_cols

this parameter can be a positive integer (the number of columns)
or a string of the following form

I) a positive integer, the number of columns for the table. The width of every 
column will be equal to the width of the table (given by the width property) divided by 
the number of columns ($num_cols)

II) a string of the form '{c1, c2, c3,... cN}'. In this case every element in the curly 
brackets is a positive numeric value that represent the width of a column. Thus, 
the n-th numeric value is the width of the n-th colum. If the sum of all the width of 
the columns is bigger than the width of the table but less than the width of the document, 
the table will stretch to the sum of the columns width. However, if the sum of the columns 
is bigger than the width of the document, the width of every column will be reduce proportionally 
to make the total sum equal to the width of the document. 

III) a string of the form '%{c1, c2, c3,... cN}'. Similar to the previous case, but this time 
every element represents a percentage of the width of the table. In this case it the sum of 
this percentages is bigger than 100, the execution will be terminated.

style

the global style for the table (see documentation) a semicolon-separated string of attribute 
values that defines the default layout of the table and all the cells and their contents 
(see Documentation section in README.md)

示例

    $table= new easyTable($fpdf, 3);
    $table= new easyTable($fpdf, '{35, 45, 55}', 'width:135;');
    $table= new easyTable($fpdf, '%{35, 45, 55}', 'width:190;');

返回值

easyTable对象

function rowStyle( string $style )

描述

设置或覆盖当前行中所有单元格的样式。

参数

style

一个分号分隔的字符串,定义了当前行中所有单元格的布局及其内容(请参阅README.md中的“文档”部分)

返回值

注意

该函数应在当前行的第一个单元格之前调用

function easyCell( string $data [, string $style = '' ])

描述

Makes a cell in the table

参数

data
相应单元格的内容

style(可选)用分号分隔的属性值字符串,用于定义单元格及其内容的布局(请参阅README.md中的文档部分)

返回值

void

function printRow ( [ bool $setAsHeader = false ] )

描述

该函数表示当前行的结束。

参数

setAsHeader(可选)当设置为true时,将当前行设置为表格的标题;这意味着当前行将在表格拆分到的每一页上作为表格的第一行(表头)打印。注意:1. 为了工作,应将表格属性split-row设置为true。2. 仅当此参数设置为true的第一个行才用作标题,其他行将按正常行打印。3. 对于跨越多个行的行标题,应将最后一个参数设置在组的最后一行中。参见示例2

返回值

注意

只要以下条件成立,该函数就会打印当前行

      total_rowspan=0

其中total_rowspan设置为

      total_rowspan=max(total_rowspan, max(rowspan of cell in the current row))-1;

function endTable( [ int $bottomMargin = 2 ])

描述

取消设置easyTable对象的全部数据成员

参数

bottomMargin(可选)

可选。指定在表格最后一行之后的空白行数。默认2。

如果它是负数,则垂直位置将在表格末尾之前设置。

返回值

样式字符串

与内联CSS样式类似,Easy Table使用由分号分隔的属性/值对字符串来定义应用于表格不同部分的样式。在表级别设置的属性值将继承到表中的所有行(因此所有单元格)。在行级别设置的属性值将覆盖从表继承的值,并将传递给该行中的所有单元格,除非单元格为该属性定义了自己的值。

以下内容中,我们将使用以下表示法

C=cell
R=row
T=table

属性 [C/R/T] 表示属性属性可以在单元格、行、表中设置。

属性完整列表

width [T]

width属性设置表格的宽度。此属性可以定义为毫米或文档宽度的百分比。

语法

width:mm|%;

示例

width:145;
width:70%;

默认:文档宽度减去左右边距。

border [C/R/T]

border属性指示是否在单元格或单元格周围绘制边框。值可以是数字

0: no border
1: frame

或包含以下字符(顺序不限)的字符串:

L: left border
T: top border
R: right border
B: bottom border

默认值:0。

border-color [C/R/T]

border-color属性用于设置绘制在单元格周围的边框的颜色。值可以是:十六进制颜色代码或RGB颜色代码。

语法

border-color: Hex |RGB;

示例

border-color:#ABCABC;
border-color:#ABC;
border-color:79,140, 200;

默认值:文档中设置的当前绘制颜色

注意:当在单元格级别设置此属性时,由于单元格的边框相互重叠,结果可能与预期不符,在相邻的具有不同边框颜色的单元格上。

border-width [T]

border-width属性用于设置边框由线条组成的宽度。

语法

border-width:0.5;

默认值:文档当前的绘图线宽度。

注意:如果border-width设置为厚,边框可能会覆盖单元格的内容。在这种情况下,您必须设置适当的paddingX和paddingY在单元格上。(请参阅下面的paddingX和paddingY属性)。

split-row [T]

此属性指示页面底部的一行在达到底部边距时是否应分割,除非该行包含跨越多行的单元格,在这种情况下行将分割。默认情况下,任何无法适应页面的行将打印在下一页。将属性设置为false,将在页面之间分割任何行。

示例

split-row:false;

l-margin [T]

此属性指示表格应开始的位置与左页边距的距离。

语法

l-maring:mm;

示例

l-maring:45;

默认值:0。

min-height [R]

min-height属性设置特定行中所有单元格(具有rowspan:1)的最小高度。

语法

min-height:mm;

示例

min-height:35;

默认值:0。

align [C/R/T]

此属性指示设置该属性的元素的水平对齐方式。值可以是

L: to the left
C: at the centre
R: to the right
J: justified (applicable just on cells)

表格的语法是

align:A;
align:A{A-1A-2A-3A-4...};

说明:第一个字符表示表格的水平对齐方式(除非设置了l-margin),可选字符串是形式为:{A-1A-2A-3A-4...}(包括花括号)的字符串,其中A-1、A-2、A-3、A-4等可以是L、C、R或J,A-n字母表示第n行中所有单元格内容的水平对齐方式。如果行数大于可选字符串的长度,则溢出的行将具有默认左对齐(L)对齐。

示例:(10行表格)

align:R{CCCLLJ};

表示表格对齐到文档的右侧,前三行的单元格内容将居中对齐,第四行和第五行的单元格内容将左对齐,第六行的单元格内容将右对齐,而剩余行的单元格内容将对齐到左侧。

行的语法是

align:{A-1A-2A-3A-4...};

其中A-1、A-2等与表格情况相同,具有相同的功能:A-n字符表示相应行中第n列单元格的对齐方式。

示例

align:{LRCCRRLJ}

单元格的语法是

align:A;

其中A可以是L、C、R、J。

默认值:L。

valign [C/R/T]

此属性定义单元格内容的垂直对齐方式。值可以是

T: top
M: middle
B: bottom

示例

valign:M; 

默认值:T。

备注:当在设置了图像属性的单元格上使用valign属性(见下文)时,如果单元格没有文本,valign的行为如预期,即,图像根据valign的值定位。但是,如果单元格包含文本,则图像和文本将在单元格中间valign,而top(T)或middle(M)valign将文本设置在图像上方,而valign:B将文本设置在图像下方。

bgcolor [C/R/T]

bgcolor属性定义单元格的背景颜色。值可以是:十六进制颜色代码或RGB颜色代码。

语法

bgcolor:Hex | RGB;

示例

bgcolor:#ABCABC;
bgcolor:#ABC;    
bgcolor:79,140, 200;

默认值:文档中设置的当前填充颜色

font-family [C/R/T]

可以是使用AddFont()方法定义的名称或标准字体之一(不区分大小写)

Courier (fixed-width)
Helvetica or Arial (synonymous; sans serif)
Times (serif)
Symbol (symbolic)
ZapfDingbats (symbolic)

也可以传递一个空字符串。在这种情况下,将保留当前家族。

示例

font-family:times;

默认值:文档中设置的font-family。

font-style [C/R/T]

可能的值是(不区分大小写)

empty string: regular
B: bold
I: italic
U: underline

或任何组合。默认值是常规。粗体和斜体样式不适用于Symbol和ZapfDingbats。

示例

font-style:IBU

默认值:空;

font-size [C/R/T]

字体大小以点为单位。

示例

font-size:16;

默认值:文档中当前字体大小。

font-color [C/R/T]

此属性定义单元格中字体颜色。值可以是:十六进制颜色代码或RGB颜色代码。

语法

font-color:Hex |RGB;

示例

font-color:#ABCABC;
font-color:#ABC;    
font-color:79,140, 200;

默认值:文档中设置的当前字体颜色。

行高 [C/R/T]

行高属性指定行高。

语法

line-height:number;

示例

line-height:1.2;

默认值:1。

paddingX [C/R/T]

paddingX属性设置单元格的左右内边距(空间)。

语法

paddingX:mm;

示例

paddingX:4;

默认:0.5。

paddingY [C/R/T]

paddingY属性设置单元格的上下内边距(空间)。

语法

paddingY:mm;

示例

paddingY:3;

默认:1。

colspan [C]

colspan属性定义单元格应跨越的列数。

语法

colspan:4;

默认 1

rowspan [C] rowspan属性定义单元格应跨越的行数。

语法

rowspan:2;

默认 1

img [C] img属性定义单元格中的图像及其尺寸。

语法

img:image.png,w80,h50;
img:image.png,h50;
img:image.png;

如果没有指定尺寸,图像尺寸将按比例计算以适应单元格宽度。如果只指定两个尺寸中的一个(宽度或高度),而未指定另一个,则未指定的尺寸将按比例计算。默认值:空。

字体和UTF8支持

  1. 获取您想要使用的字体ttf文件,并将它们保存在Fonts目录下。

    注意:该字体必须包含您想要使用的字符

  2. 使用FPDF库的makefont.php脚本部分(在makefont目录中)

    me@laptop:/path/to/FPDF/makefont$ php makefont.php /path/to/Fonts/My_font.ttf ENCODE
    

    注意:使用正确的编码以使用utf-8符号 FPDF:教程7

    注意:该字体必须包含与所选编码对应的字符。

  3. 最后一个命令将在目录/path/to/FPDF/makefont中创建My_font.php和My_font.z文件,将这些文件移动到目录/path/to/FPDF/font

  4. 您现在可以在脚本中使用您的字体

    $pdf = new PDF();
    $pdf->AddFont('Cool-font','','My_font.php');  // Define the new font to use in the PDF object
    
    // more code
    
    $table=new easyTable($pdf, ...);
    $table->easyCell(iconv("UTF-8", "ENCODE",'Hello World'), 'font-color:#66686b;font-family:Cool-font');
    //etc...
    
  5. 示例:我们获取一个ttf字体文件(my_font.ttf),该文件支持我们想要使用的语言和符号。在这个例子中,我们使用俄语。我们为俄语使用的编码是KOI8-R

    php makefont.php /path/to/font_ttf/my_font.ttf KOI8-R
    

    然后将生成的文件复制到FPDF库的字体目录。

    然后,在我们的脚本中

    $pdf = new PDF();
    
    $pdf->AddFont('FontUTF8','','my_font.php'); 
    
    $pdf->SetFont('FontUTF8','',8); // set default font for the document 
    
    $table=new easyTable($pdf, ...);
    
    $Table->easyCell(iconv("UTF-8", "KOI8-R", "дебет дефинитионес цу")); // Notice the encode KOI8-R
    

    $pdf = new PDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    
    $pdf->AddFont('FontUTF8','','my_font.php'); 
    
    $table=new easyTable($pdf, 5, '...');	
    $Table->easyCell(iconv("UTF-8", "KOI8-R", "дебет дефинитионес цу"), 'font-family:FontUTF8;'); 
    

    注意:有关正确编码的更多信息,请参阅 FPDF:教程7php inconv

与FPDI一起使用

如果您的项目需要easyTable和 FPDI,您应该这样做。假设fpdf.php、easyTable.php、exfpdf.php、fpdi.php以及这些库中的任何其他文件都在同一目录中。

类exfpdf应扩展类fpdi而不是类fpdf。所以,在exfpdf.php中

<?php

class exFPDF extends FPDI
{
etc
etc

并且,在您的项目中

<?php
include 'fpdf.php';
include 'fpdi.php';
include 'exfpdf.php';
include 'easyTable.php';

//$pdf = new FPDI(); remember exfpdf is extending the fpdi class
$pdf=new exFPDF(); // so we initiate exFPDF instead of FPDI

// add a page
$pdf->AddPage();
// set the source file
$pdf->setSourceFile("example-2.pdf");
// import page 1
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at point 10,10 with a width of 100 mm
$pdf->useTemplate($tplIdx, 10, 10, 100);

//add another page
$pdf->AddPage();
$pdf->SetFont('helvetica','',10);

$table1=new easyTable($pdf, 2);
$table1->easyCell('Sales Invoice', 'font-size:30; font-style:B; font-color:#00bfff;');
$table1->easyCell('', 'img:fpdf.png, w80; align:R;');
$table1->printRow();
//etc
//etc

联系

欢迎您的评论和问题: easytable@yandex.com(主题:EasyTable)

捐赠

我喜欢咖啡,我的意思是高质量的咖啡。所以如果你喜欢这个类,也许你愿意帮助我买咖啡:-)

Donate

许可证

这是一个免费且不受限制的软件,已发布到公共领域。

任何人都可以自由复制、修改、发布、使用、编译、销售或分发此软件,无论是源代码形式还是编译的二进制形式,出于任何目的,无论是商业目的还是非商业目的,以及任何方式。

在承认版权法的司法管辖区,该软件的作者或作者将此软件的所有版权利益放弃给公共领域。我们做出这一奉献,是为了公共利益,损害我们的继承人及后继者。我们有意将此奉献视为永久放弃根据版权法对软件的所有现有和未来权利的公开行为。

本软件按“原样”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于对适销性、特定用途适用性和非侵权的保证。在任何情况下,作者不对任何索赔、损害或其他责任承担责任,无论源于合同行为、侵权或其他,与软件或软件的使用或其他交易有关。

如需更多信息,请参阅http://unlicense.org/