Skip to content

Write() with align R aligns to the cursor instead of the right margin once setX() has moved it [7.x] #877

Description

@wayne530

Describe the bug

Write($h, $txt, '', false, 'R', true) right-aligns to the right margin — unless the cursor has been
moved to the right with setX() first, in which case the text is right-aligned to the cursor
instead.

Legacy TCPDF 6 aligned to the right margin wherever the cursor happened to be. A header column
positioned with setX() and then written right-aligned now lands in the middle of the page: in our
letters it moved 95.95mm.

Left-aligned writes are unaffected, and so is a right-aligned write whose cursor is still at the left
margin — which is why this can go unnoticed in a document where most lines are fine.

Expected behavior

The cursor limits where a right-aligned line may start; it does not move the edge the line is
aligned to. All three writes below end flush at the right margin, as they do in 6.x.

Actual behavior

The write that follows setX(107.95) ends at 107.95mm — the cursor — rather than at the right
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(11, 11, 11);
$pdf->AddPage();
$pdf->setFont('helvetica', '', 9);
$h = $pdf->getCellHeight($pdf->getFontSize());

$pdf->Write($h, 'A: plain right-aligned', '', false, 'R', true);
$pdf->setX(107.95);
$pdf->Write($h, 'B: right-aligned after setX', '', false, 'R', true);
$pdf->setX(11);
$pdf->Write($h, 'C: right-aligned after setX to the left margin', '', false, 'R', true);

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

Output

Run positions read back out of the content stream. The page is 215.90mm wide with 11mm margins, so
the right margin is at 204.90mm:

                                       start      end
TCPDF 6.x
  A: plain right-aligned              174.96    203.90
  B: right-aligned after setX         168.08    203.90
  C: after setX to the left margin    143.37    203.90

tecnickcom/tcpdf 7.0.6 + tc-lib-pdf 8.71.2
  A: plain right-aligned              174.96    203.90
  B: right-aligned after setX          72.13    107.95   <-- ends at the cursor
  C: after setX to the left margin    143.37    203.90

B is displaced by 95.95mm.

Environment

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

Additional context

Centred writes are affected as well. With the same margins, the centre of the text area is 107.95mm.
A plain Write(..., 'C') centres its text there correctly; the same call after setX(107.95)
centres at 155.92mm, which is the midpoint between the cursor and the right margin:

   96.92 .. 118.98   centre 107.95   A: plain centred
  141.45 .. 170.40   centre 155.92   B: centred after setX

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