TCPDF - How to use GetStringWidth when text is bold
Is there a way to use GetStringWidth on text that has formatting? Right now if I have <b>Jarod</b>, the GetStringWidth treats the tags as if they are text to display and returns the string width including the tags. If I take them out, then the String Width returned is for non-bold text and we know that bold text is bigger than non-formatted text, so I'm trying to figure out how to measure formatted text width in TCPDF. Thanks!
According TCPDF Doc you can send parameters to the GetStringWidth() function, including font style. In your example:
$text_width = $pdf->GetStringWidth("Text To Be Measured",'','B');
Just figure this out. In TCPDF, we always use SetFont('Times','',12); where we have Font family or name, next is the '' where we can put I or B or U, etc. and then the font size. The problem with GetStringWith that I was having is that I was using the default font setting to measure my font when I needed to measure bold. To the Fix:
//Temporarily set the font to Bold. $pdf->SetFont('Times','B',12); //Now measure the text that needs to be measured; $text_width = $pdf->GetStringWidth("Text To Be Measured"); //Now reset the font back to normal so the rest of the document isn't messed up. $pdf->SetFont('Times','',12);
I hope this helps someone else.