Programming Advisory: TCPDF Cell Alignment July 26, 2019

The TCPDF library has a set of functions that write to a document in a cell. Two common functions of this type are WriteHTMLCell and Cell.

There is also the WriteHTML function that flows the text within the available document space.

Both WriteHTMLCell and Cell are "cell functions".

Both Cell and WriteHTML are spaced by SetMargins whereas WriteHTMLCell is not bound by the document position cursor.

Consider the following scenario:

$pdf->setMargins(30,60,30);

$pdf->Cell(100,20,'ABC from Cell');

$pdf->WriteHTML('ABC from WriteHTML');

$pdf->WriteHTMLCell(100,20,30,100,'ABC from WriteHTMLCell');

The cell functions have a default inner padding of 3 points. Subsequently the output lines are misaligned:

A common programming mistake when aligning cell and non-cell functions is to consider the cell functions to be accurate. It is simply wrong to add 3 points to the global margin. Instead, 3 points should be taken away from the cell functions. In addition, setMargins should be called before and after Cell so that the lost points are localized:

$pdf->setMargins(30-3,60,30);

$pdf->Cell(100,20,'ABC from Cell');

$pdf->setMargins(30,60,30);

$pdf->WriteHTML('ABC from WriteHTML');

$pdf->WriteHTMLCell(100,20,30-3,100,'ABC from WriteHTMLCell');

It is also beneficial to leave the "-3" in the code so that the intent of offset is self-documented.

Our Services

Targeted Crawlers

Crawlers for content extraction, restoration and competitive intelligence gathering.

Learn More

Gyroscope™ ERP Solutions

Fully integrated enterprise solutions for rapid and steady growth.

Learn More

E-Commerce

Self-updating websites with product catalog and payment processing.

Learn More
Chat Now!
First Name*:
Last Name*:
Email: optional