FPDF - Inline bold text
Generating a PDF disclaimer in PHP often presents a specific set of constraints, particularly when legal requirements dictate that certain text must be both bolded and outlined. The core challenge lies in reconciling two distinct capabilities: applying inline formatting to specific words within a paragraph and drawing a border around that same block of text.
Using native FPDF functions like MultiCell gives you direct control over borders, but it treats the cell as an atomic unit. Once you set the font properties for the entire cell, you cannot selectively bold a substring without breaking the flow or requiring complex manual character-by-character rendering. Conversely, switching to a helper class like WriteHTML simplifies inline styling via tags like <b>, but standard HTML cells in FPDF do not support border drawing in the same way MultiCell does with its fourth parameter.
To solve this, you must decouple the visual boundary from the text rendering logic. The most robust approach is to manually define the geometry of the bordered box before injecting the content. This involves calculating the exact width and height required by your specific text string, setting up a clipping region or drawing a rectangle explicitly, and then executing the write operation.
Here is a practical implementation strategy using native FPDF that achieves both goals without external libraries:
First, determine the dimensions of the unstyled text. You can use GetTextWidth() to measure the string based on your desired font settings. This measurement is critical because borders must encompass the full extent of the content, including any padding you might want for readability.
// Define the base font and size
$pdf->SetFont('Arial', '', 12);
// Define the text components
$normalText = 'These words do not need to be bold.';
$boldText = '<b>THESE WORDS NEED TO BE BOLD.</b>'; // Note: requires HTML mode or manual handling
// Calculate width for border sizing
$pdf->SetFont('Arial', '', 12);
$width = $pdf->GetTextWidth($normalText) + $pdf->GetTextWidth($boldText) + 20;
// Add padding here manually to ensure the border is not tight against the text
// Draw the border rectangle
$pdf->Ln(5); // Move down and set Y position
$pdf->SetX(); // Reset X to current cursor or desired start
$x = $pdf->GetX();
$y = $pdf->GetY();
$pdf->SetXY($x, $y + 4); // Offset Y slightly if drawing inside, or keep at top edge
$pdf->Rect($x, $y - 5, $width, 10, 'B'); // 'B' draws border, 'S' for stroke only
// Now write the content inside the calculated box
$pdf->SetXY($x + 2, $y); // Add internal padding
$pdf->SetTextColor(128);
$pdf->SetFont('Arial', '', 12);
// Write normal text first
$pdf->Write(4, $normalText, null, 'L'); // L = Left align
// To handle the bold part inline within a MultiCell-like flow is impossible without HTML mode.
// If using pure string manipulation:
$pdf->SetFont('Arial', 'B', 12); // Set bold font for the specific words
$pdf->Write(0, $boldText, null, 'L');
// Reset font if needed after
$pdf->SetFont('Arial', '', 12);
If you prefer the cleaner syntax of WriteHTML, you can still achieve the border by drawing a rectangle that encompasses the entire cell area defined in your HTML string. You must estimate the height or use MultiCell logic to determine the bottom Y-coordinate before switching to HTML mode for the content.
The trade-off here is increased code complexity versus library convenience. Native FPDF requires you to manage coordinates and font states manually, which increases the risk of layout shifts if not handled carefully. However, it offers total control over the visual boundaries without relying on third-party extensions that might behave differently across versions.
For high-volume legal documents where consistency is paramount, sticking to native functions ensures predictable rendering. If you choose this route, remember that MultiCell with a border argument ('B' or 'S') is the primary tool for outlining cells, but it inherently limits inline font switching. To bypass this limitation while keeping borders, manual coordinate management is the only reliable path forward.