stollr / tcpdf-extension
该库扩展了TCPDF库,例如提供了创建表格的智能API。
v2.0.0
2023-12-20 16:35 UTC
Requires
- php: >=5.3.3
- tecnickcom/tcpdf: ^6.2
README
TCPDF 是一个用于生成PDF文档的PHP库。它功能丰富,但使用起来不太容易。
该库建立在TCPDF库之上。在当前状态下,它只提供了一个智能API来 以舒适的方式创建表格。
原始仓库
此仓库是 naitsirch/tcpdf-extension 的分支。版本 2.0.0
与 1.1.0
兼容,除了目录结构。现在它遵循 PSR-4 标准。
功能
- 更舒适的表格创建
安装
使用 Composer 安装最新版本
composer require stollr/tcpdf-extension
用法
创建表格
首先,您需要创建TCPDF的一个实例。
use TCPDF; $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false); $pdf->SetTitle('My PDF file'); $pdf->SetMargins(20, 20, 20); $pdf->SetPrintHeader(false); $pdf->SetPrintFooter(false); $pdf->SetAutoPageBreak(true, 9); $pdf->SetFont('dejavusans', '', 10);
如果您对TCPDF类不熟悉,请查阅文档。
在下一步中,您可以创建表格。
use Tcpdf\Extension\Table\Table; $pdf->AddPage(); // add a new page to the document $table = new Table($pdf); $table ->newRow() ->newCell() ->setText('Last Name') ->setFontWeight('bold') ->end() ->newCell() ->setText('First Name') ->setFontWeight('bold') ->end() ->newCell() ->setText('DOB') ->setFontWeight('bold') ->end() ->newCell() ->setText('Email') ->setFontWeight('bold') ->end() ->end() ->newRow() ->newCell('Foo')->end() ->newCell('John')->end() ->newCell('1956-04-14')->end() ->newCell('johnny@example.com')->end() ->end() ; $table->end(); // this prints the table to the PDF. Don't forget!
上述代码展示了如何创建一个非常简单的表格。但是,您可以用不同的方式自定义表格单元格
$table ->newRow() ->newCell('Last Name') // you can set the cell content like this ->setText('Override Text') // or like this ->setFontWeight('bold') // set font weight 'bold' or 'normal' ->setAlign('L') // text alignment ('L', 'C', 'R' or 'J') ->setVerticalAlign('top') // vertical alignment ('top', 'bottom' or 'middle') ->setBorder(1) // border format (like in TCPDF::MultiCell) ->setRowspan(1) // rowspan like in HTML ->setColspan(2) // colspan like in HTML ->setFontSize(10) // unit for font size is same as defined in TCPDF ->setMinHeight(10) // defining min-height of the cell like in CSS ->setPadding(2, 4) // setting cell padding (inner margin) like in CSS ->setPadding(2, 4, 5, 6) // or like this ->setWidth(125) // unit for width is same as defined in TCPDF ->end()
背景
定义背景颜色
$table ->newRow() ->newCell('Last Name') ->setBackgroundColor('#ff4400') // hexadecimal RGB color code ->setBackgroundColor(array(250, 80, 10) // decimal RGB color array ->end() ->end()
可以为每个表格单元格定义一个背景图像。
$table ->newRow() ->newCell('Last Name') ->setBackgroundDpi(300) // define the resolution for the printing ->setBackgroundImage('/path/to/my/image.png') // pass the path to your image ->setBackgroundImage($binaryImageString) // or pass the binary file content of your image ->end() ->end()