We help IT Professionals succeed at work.

Can you BOLD part of text you want to show in a PDF using FPDF and PDF_Label?

Medium Priority
2,849 Views
Last Modified: 2018-04-08
I am creating postage labels and recording them into a pdf file using FPDF and PDF_Label - all work fine, see code sample below ...
$text = sprintf("%s\n%s\n%s\n%s %s %s\n%s", "   6         022, "Name","Address", "Location", "State", "PostCode", "Country");
$pdf->Add_Label($text);

As you can see, I am using sprintf to format text for the label and using Add_Label from PDF_Label class to create the label.

I would like to BOLD the "Name" portion within the sprintf, if possible. As far as I can tell this can't be done. Tried using html tags inside it - no good, just prints the tags.

Any help or suggestions appreciated.
Comment
Watch Question

Commented:
Try this as a test,

Change something like this:
printf("TextToBold");

to this:
printf("\e1mTextToBold\e[0m");

For your example, show edited how to bold name:
$text = sprintf("%s\n \e1m%s\e[0 \n%s\n%s %s %s\n%s", "   6         022, "Name","Address", "Location", "State", "PostCode", "Country");
AIGSNetwork Administrator

Author

Commented:
Martin
Testing your sample: printf("\e1mTextToBold\e[0m");
All I get with the sample is "1mTextToBold" on the page. The same happens when I apply your suggestion to my actual code.
AIGS
AIGSNetwork Administrator

Author

Commented:
I think the problem is the PDF_Label class, where it uses multicell to insert the text into the pdf, formatted by sprintf.
Is there a way to bold a specific line or text using multicell?
AIGS
AIGSNetwork Administrator

Author

Commented:
After looking at code in FPDF class - I see that mulicell() uses cell() to write each line within the multicell().
Perhaps that is where I need to try something?
Network Administrator
Commented:
I have edited the FPDF Class function cell() to identify if the <b></b> tags are supplied as part of the string, and if so reset BOLD for the PDF on ad then off. This works ok for the entire string (whole line) passed to cell() from multicell().

Now the trick is to detect the word (text) within the string to isolate them so that only the words encased within the <b></b> tags are set as bold, not the entire line.

Commented:
It looks like you have this solved.  Promoting question for closure.