Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FPDF - Inline bold text

I am trying to create a PDF from PHP and for legal reason we need to make part of our disclaimer BOLD and the disclaimer needs to be outlined.

My current code uses:

if(isset($_POST['optout']) && $_POST['optout'] == "yes"){
    $pdf->Ln(5);
    $pdf->SetFont('Arial','I',12);
    $pdf->SetTextColor(128);
    $pdf->MultiCell(0,4,'This is my disclaimer. THESE WORDS NEED TO BE BOLD. These words do not need to be bold.',1,'C');
}

I am currently using WriteHTML for other parts of the document, and I could easily use that instead of MultiCell, but then how do I create the border?

So I have 2 options..

native FPDF functions

PROS: Give an option for a border

CONS: No easy way to make inline text bold

WriteHTML extension class

PROS: Lets me easily add bold text inline

CONS: Not sure how to create the border

Suggestions?

like image 879
I wrestled a bear once. Avatar asked Mar 27 '14 19:03

I wrestled a bear once.


People also ask

How do I bold text in a fillable PDF?

Bold Text in Fillable PDF Form You can also bold text in the text filed for a fillable PDF form by opening the "Text" panel. Select the text content and bold text in the fillable PDF form by clicking on the "Bold" icon.

How do you bold a line in PHP?

We use the html tag <b>... </b> to bold text inside the PHP scripts.

How do I print bold text in PHP?

In order to get bold text, you need to use font-weight and to get the simple text you can use <span> tag. All this gets added in the echo.


2 Answers

Here's how I solved it:

$pdf->SetFont('Arial','',10);
$cell = 'This is my disclaimer.';
$pdf->Cell($pdf->GetStringWidth($cell),3,$cell, 0, 'L');
$pdf->SetFont('Arial','B',10);
$boldCell = "THESE WORDS NEED TO BE BOLD.";
$pdf->Cell($pdf->GetStringWidth($boldCell),3,$boldCell, 0, 'L');
$pdf->SetFont('Arial','',10);
$cell = 'These words do not need to be bold.';
$pdf->Cell($pdf->GetStringWidth($cell),3,$cell, 0, 'L');

Essentially, make one cell, change font, then another cell with the width of the text you want to bold, and so on.

There seem to be better tools out there for making PDFs that use HTML / Blade templates and such, though, so you might want to consider using that.

like image 181
mkrell Avatar answered Oct 08 '22 18:10

mkrell


You could rewrite a part of the extension but it may be easier for you to use the extension writeHTML, then draw a cell (empty text) with a border over the cell you created with writeHTML. By adjusting your cells properly it should work.

Don't forget to use SetY then SetX to position your cells.

Example:

if(isset($_POST['optout']) && $_POST['optout'] == "yes"){
   $pdf->Ln(5);
   $pdf->SetFont('Arial','I',12);
   $pdf->SetTextColor(128);

   //Your text cell
   $pdf->SetY($pos_Y);
   $pdf->SetX($pos_X);
   $pdf->writeHTML('This is my disclaimer. <b>THESE WORDS NEED TO BE BOLD.</b> These words do not need to be bold.');

   //Your bordered cell
   $pdf->SetY($pos_Y);
   $pdf->SetX($pos_X);
   $pdf->Cell($width, $height, '', 1, 0, 'C');
 } 
like image 41
Venom Avatar answered Oct 08 '22 18:10

Venom