FPDF - Inline Bold Text

I am trying to create a PDF file with PHP and for legal reasons we need to make part of our BOLD disclaimer and the disclaimer.

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 this instead of MultiCell, but how do I create a border?

So, I have 2 options.

native FPDF functions

PROS: specify a parameter for the frame

CONS: an easy way to make inline text in bold

WriteHTML Extension Class

PROS: Allows me to easily add bold inline text

CONS: Do not know how to create a border

Suggestions?

+9
html php border fpdf
source share
6 answers

You can rewrite part of the extension, but it might be easier for you to use the writeHTML extension, and then draw a cell (empty text) with a border above the cell that you created with writeHTML. Correctly adjusting its cells, it should work.

Remember to use SetY and then SetX to place 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'); } 
+8
source share

Use Write () & Rect () .

Example:

 $pdf->Rect($pdf->GetX(),$pdf->GetY(),2,0.1); $pdf->SetFont('Arial','',8); $pdf->Write(0.1,"this is not bold, but this "); $pdf->SetFont('Arial','B',8); $pdf->Write(0.1,"is bold."); $pdf->SetFont('Arial','',8); $pdf->Ln(); 

You have to play with the width & height of the Rect () parameters. In this case, I set width = 2 and height 0.1 Custom units .

+5
source share

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 the font, then another cell with the width of the text you want to highlight in bold, etc.

It seems that there are better tools for creating PDF files using HTML / Blade templates, etc., so you might consider using them.

+1
source share

I find another solution.

http://fpdf.de/downloads/add-ons/tag-based-formatting.html

Copy and paste the write_tag code snippet into the fpdf.php file, call the write_tag function while the pdf was being created, so I can make the format in text to pdf.

+1
source share

Alternatively, TCPDF supports writing a cell containing HTML via writeHTMLCell () , the syntax is here .

 $pdf->writeHTMLCell($width, $height, $x, $y, "<b>bold</b> and <u>underlined</u> text."); 
0
source share

TCPDF is more useful than FPDF

In TCPDF

If you set the $ ishtml parameter to true in multicell mode, you can adapt the html properties to your texts.

 $text = "It <b>must</b> be like that"; $this->MultiCell($w, $h, $text, $border, $align, $fill, $ln, $x, $y, $reseth, $stretch, true, $autopadding, $maxh); 

Default function;

 MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0) 

Function to do

 MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=true, $autopadding=true, $maxh=0) 

I hope this helps ..

0
source share

All Articles