How to set different Fonts in TCPDF

How to set different Fonts in TCPDF

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Managing Multiple Fonts in TCPDF Without CSS

TCPDF operates on a different rendering model than standard web browsers, which creates confusion when developers try to apply standard CSS concepts like font-weight. The core issue stems from how TCPDF handles font definitions and state management. Unlike DOM-based libraries where styles cascade or reset automatically, TCPDF is stateless regarding the document object itself but relies heavily on the internal state of the current text element. When you call $pdf->SetFont(), you are setting the default font for the next text operation, not applying a persistent class-like attribute to a specific span.

The code snippet attempting to toggle between bold and regular fonts often fails because TCPDF does not support CSS selectors or inline weight attributes natively. The method SetFont() expects a full font family name that has been previously registered with the PDF core. You cannot simply pass 'verdana_bold' if you only loaded 'verdana'. Furthermore, calling $pdf->SetFont() twice in succession without an intervening text command resets the state for all subsequent output. If you write text after setting the bold font, it is bold. If you then set the regular font and write text, that text is regular. There is no "current style" stack that remembers the previous bold setting once you switch back to regular.

To achieve the visual effect of bold and regular text within a single document flow, you must explicitly load every variation you intend to use before rendering any content. TCPDF requires a distinct font file for every weight you want to utilize. You cannot generate a "bold" version on the fly from a regular one; the engine needs the actual TTF or OTF files mapped to specific internal names.

Here is the correct workflow for handling multiple weights:

  1. Register Font Variations: Call $pdf->AddFont() for every weight you need. You must provide a unique name for each variation.

    // Register Verdana Bold
    $pdf->AddFont('verdana', '', 'verdanabold.ttf'); 
    
    // Register Verdana Regular
    $pdf->AddFont('verdana', '', 'verdanaregular.ttf'); 
    
  2. Set the Font Before Writing: Explicitly set the font family and weight immediately before writing any text.

    $pdf->SetFont('verdana', 'B', 12); // Bold
    $pdf->Cell(40, 10, 'This is bold text.');
    
    $pdf->SetFont('verdana', '', 12);  // Regular (Note: B or I implies bold/italic)
    $pdf->Cell(40, 10, 'This is regular text.');
    

If you are working within a Laravel application and need to manage these fonts dynamically across multiple views, creating a dedicated class to handle TCPDF initialization can prevent state leakage between documents. You might instantiate the PDF object once in your controller or service layer, ensure all font families are added during initialization, and then pass the fully configured instance to your view. This approach ensures that when you switch from bold to regular, the engine knows exactly which file to call for each weight.

A common pitfall is assuming that changing the font-family string alone resets the weight. If you register a font as 'verdana_bold' and another as 'verdana', switching the family name in the SetFont() call without explicitly defining the style argument (the second parameter) might default to an unexpected state depending on how the font was added. Always be explicit about the style: use 'B' for bold, 'I' for italic, or leave it empty for regular.

For complex layouts where you need fine-grained control over text appearance without reloading fonts constantly, consider whether TCPDF is the right tool for that specific section of your document. However, for standard document generation where you need distinct weights, explicit registration and explicit setting remain the only reliable method. If you require advanced styling features like automatic CSS-to-PDF conversion or complex typography handling beyond simple font weights, exploring a library designed with CSS-in-JS or direct CSS parsing in mind might offer a more ergonomic workflow than TCPDF's imperative API.

Our network