Skip to content

Is there an easy and reliable way to vertically align context when using writeHTMLCell() or writeHTML()? #844

@nbro10

Description

@nbro10

I'm using TCPDF 6.10.0.

I know that writeHTMLCell() doesn't expose the valign parameter that e.g. MultiCell() exposes.

Under the hood, writeHTMLCell() actually calls MultiCell() as follows

public function writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=false, $reseth=true, $align='', $autopadding=true) {
	return $this->MultiCell($w, $h, $html, $border, $align, $fill, $ln, $x, $y, $reseth, 0, true, $autopadding, 0, 'T', false);
}

So, with valign = 'T'.

Anyway, if you look at the MultiCell() implementation, the valign is not used when ishtml is true, so manually changing the call to MultiCell has no effect.

if ($ishtml) { // ******* Write HTML text
	$this->writeHTML($txt, true, false, $reseth, true, $align);
	$nl = 1;
} else { // ******* Write simple text
         ...
	// vertical alignment
	if ($maxh > 0) {
                 ...
		if ($text_height < $maxh) {
			if ($valign == 'M') {
				// text vertically centered
				$this->y += (($maxh - $text_height) / 2);
			} elseif ($valign == 'B') {
				// text vertically aligned on bottom
				$this->y += ($maxh - $text_height);
			}
		}
	}
        ...
}

Obviously, this sucks because vertically aligning text is a very commonly needed operation. Right now, I need to use writeHTMLCell() because I need to set a different color to certain parts of the text, so I use a <span> , like A <span color="#B3DDDE">&</span> B.

I don't know any other way to set different colors to different parts of the text than using HTML code. However, I also need to vertically align the text in the box. But I can't apparently do it. I also tried to use a table (like others have suggested on the web), but so far that has not worked. I will not even share what I tried because it doesn't work.

So, before I lose like 1 year to inspect what writeHTML() does in practice, do you guys know a reliable way of doing what I need, i.e. color different parts of the texts differently and, at the same time, vertically align all the text? I'm OK with a solution where I just need to modify the HTML content I pass, as long as it reliably works. I'm also OK with a workaround code solution. Note: I want to vertically align/center the text inside the border/box, not move the y coordinate of the box/border.

I also checked

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions