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!

Answers


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.


Need Your Help

Fastest method of MFCC extraction on linux machine

signal-processing speech-recognition raspberry-pi mfcc

What is the fastest way of extracting mfcc from audio files in linux (Raspberry Pi in my case). I tried sphinx3 but it was slow for large files (on Raspberry Pi). SFS (speech filing system) was quite

Swipe Introduction Splash Screen for First Time Users - Android

android android-studio splash-screen

I want to implement a swipe introduction screen in my Android application that is displayed only when the app is opened for the first time.