# Output a HTML Table to file PDF use FPDF

## Informations

Class: PDFTable
Author: vietcom
License: Freeware
Version: 1.95 (15 Mar 2012) View log file
In this class, I use class HTMLParser from Jose Solorzano and class FPDF from Olivier PLATHEY
Special thanks to Vincent Tomi for ver 1.3

## Description

This script allows you to print a HTML table to file PDF.

#### It supports the follow tags:

- <table border align width height bgcolor></table>

  Attribute | Value | Description
  border | x | the thick of border table
  align | left,center,right | Table will be aligned with the current page
  width, height | integer | the width or the height of this table (in milimet by default)
  bgcolor | #xxxxxx | Background color of this table in hexadecimal

- <tr bgcolor repeat pbr knext></tr>

  Attribute | Value | Description
  bgcolor | #xxxxxx | Background color of this row in hexadecimal
  repeat |  | If a row has this property, it will be rewrite on each page if the table is broken on many page. A table can have many row repeatly. It's useful for a long table with title and the title will be rewrite on each page.
  pbr |  | (page-break-before) A page-break will be inserted before this row. "pbr" is ignored if "repeat" is actived.
  knext |  | (keep-with-next-row) This row and the row immediately below is drawn on the same page. "knext" is ignored if "repeat" is actived.

  Note: A row with "pbr" or "knext" property causes a page-break, some cells which have "rowspan" property may be drawn not good.
- <td border align valign width height bgcolor colspan rowspan nowrap family size color></td>

  Attribute | Value | Description
  border | 0,trbl,x | Border can be 0 for a cell with no border. If you want to customise your cell, use trbl to define: t=0|1=top, r=0|1=right, b=0|1=bottom and l=0|1=left. Example: - border='1010': Draw only border top and bottom - border='0101': Draw only border left and right - border='1001': Draw only border top and left - border='1': Draw all border 0.1mm - border='2.5': Draw all border 2.5mm
  align | left,center,right | Text will be align inside the cell in horizontal, defaut is left
  valign | top,middle,bottom | Text will be align inside the cell in vertical, defaut is middle
  width, height | integer | the width or the height of this table (in milimet by default)
  bgcolor | #xxxxxx | Background color of this table in hexadecimal
  colspan | integer | Table cells can span across more than one column or row. The attributes COLSPAN ("how many across") and ROWSPAN ("how many down") indicate how many columns or rows a cell should take up.
  rowspan | integer
  nowrap |  | Indicates that text should not wrap in the cell
  family | Verdana,Times,..... | Family font
  style | bold,italic,underline | Style of the font, delimiter=",", without space, case sensitive
  size | 12,14... | Size of font

- <font family size color></font> A cell can have many "font" tag.

  Attribute | Value | Description
  family | Verdana,Times,..... | Family font
  style | bold,italic,underline | Style of the font, delimiter=",", without space, case sensitive
  size | 12,14... | Size of font

- <img src='/var/example.jpg' width='100' height='100'> Support draw only an image in a cell
- <br> forced line break
- &nbsp; space character
- &lt; less than character
- Any other tag which isn't supported will be understood as tag <br>.

#### Doesn't support yet:

- Table inside other table :
  I don't know what it will be if there are more than one table!

#### Special note:

- Header :
  If you rewrite function Header() for generate header of each page, you should be insert parent::Header(); in the first line of your Header() function!
- Multipage :
  One table can be written on many page.
  The follow page has same size with the current page.
  Any row which has the height great than (height page - height of repeat rows) will be replaced by phrase: Height of this row is great than page height!
- Close HTML tag:
  It is not necessary to have a close tag for an open tag. Thus, you don't need use in your variable these tags: </td>, </tr> and </table>.

## Example

<?php $html = <<<MYTABLE <table border="1" align="center"> <tr> <td rowspan="2" valign="middle" border="0">rowspan=2, valign=middle</td> <td>Normal</td> <td>Normal</td> <td>Normal</td> <td colspan="2" rowspan="2" valign="bottom" bgcolor="#FF00FF">colspan=2<br>rowspan=2<br>valign=bottom</td> </tr> <tr> <td height="15">Normal</td> <td rowspan="2" align="right" bgcolor="#aaaaaa" border="0">rowspan=2</td> <td border="0">border=0</td> </tr> <tr> <td>Normal</td> <td>Normal</td> <td>Normal</td> <td rowspan="3" valign="top" bgcolor="#CC3366">rowspan=3</td> <td>Normal</td> </tr> <tr bgcolor="#cccccc"> <td>Normal</td> <td colspan="3" align="center">align center, colspan=3</td> <td>Normal</td> </tr> <tr> <td align="right" valign="bottom">align=right<br>valign=bottom</td> <td>Normal</td> <td>&nbsp;</td> <td>Normal</td> <td height="20">height=20</td> </tr> </table> MYTABLE; define('FPDF_FONTPATH','font/'); require('pdftable.inc.php'); $p = new PDFTable(); $p->AddPage(); $p->setfont('times','',12); $p->htmltable($html); $p->output('','I'); ?>

Result in my browser (opera 7.5):
Result in your browser:
rowspan=2, valign=middle | Normal | Normal | Normal | colspan=2 rowspan=2 valign=bottom
Normal | rowspan=2 | border=0
Normal | Normal | Normal | rowspan=3 | Normal
Normal | align center, colspan=3 | Normal
align=right valign=bottom | Normal |  | Normal | height=20
Result in file pdf: example.pdf (Note: HTML dosen't support border='0' for each <td>)

## Example 2

This file has a long table wrote on 3 pages: example2.pdf

## Example 3

File with some fonts, colors, styles and Vietnamese font: example3.pdf

## Source

Dowload here: pdftable v1.95