Skip to content

Write() drops the left cell padding once the cursor has been moved [7.x] #874

Description

@wayne530

Describe the bug

The horizontal gap Write() leaves between the cursor and the first glyph depends on where the
cursor is. At the left margin the left cell padding is applied; after a setX() past the margin it
is not, and the text starts flush at the cursor.

Legacy TCPDF 6 applied the padding in both cases, so anything positioned by arithmetic on GetX()
indented blocks, hanging labels, cc: lists, signature blocks — now starts one padding further left
than it used to. With the default 1.00mm padding that is a 1.00mm shift on every such element.

Expected behavior

The distance between the cursor and the first glyph is the same wherever the cursor is, so
setX($x) followed by Write() starts the text at a consistent offset from $x.

Actual behavior

The padding is applied only when the cursor is at the left margin.

To Reproduce

<?php
require __DIR__ . '/vendor/autoload.php';
define('K_PATH_FONTS', __DIR__ . '/vendor/tecnickcom/tc-lib-pdf-font/target/fonts/');

$pdf = new TCPDF('P', 'mm', 'LETTER', true, 'UTF-8');
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->setMargins(20, 20, 20);
$pdf->AddPage();
$pdf->setFont('helvetica', '', 11);

printf("left cell padding: %.2f mm\n", $pdf->getCellPaddings()['L']);

$pdf->Write(5, "first\n");     // cursor at the left margin, 20.00
$pdf->setX(40.0);              // an explicit indent
$pdf->Write(5, "second\n");

file_put_contents(__DIR__ . '/cursor-padding.pdf', $pdf->Output(null, 'S'));

Both lines should sit at the same offset from their cursor. Reading the Td operators back out of
the page content stream gives the two text origins.

Output

left cell padding      : 1.00 mm

cursor 20.00 at the margin -> text at 21.00  (padding applied: 1.00)
cursor 40.00 after setX()  -> text at 40.00  (padding applied: 0.00)

The second line is expected at 41.00mm, which is where TCPDF 6.x drew it.

Environment

  • tecnickcom/tcpdf 7.0.6
  • tecnickcom/tc-lib-pdf 8.71.2
  • PHP 8.5.9

Additional context

MultiCell() and writeHTMLCell() were not examined, so I cannot say whether they behave the same
way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions