Search code examples
phpfontsfpdf

tFPDF add multiple DejaVu fonts


How would one use multiple styles of DejaVu fonts when using tFPDF?

I would like to use the DejaVuSans font for some text and DejaVuSansMono for other.

The tutorial shows you can include font like this:$pdf -> AddFont('DejaVu','','DejaVuSans.ttf',true); and then use it like $pdf -> SetFont("DejaVu","",10);

But if I add additional font $pdf->AddFont('DejaVu','','DejaVuSansMono.ttf',true); nothing changes.

So how do I specify where I want to use the Mono font and where the default one?

My code currently looks like this (doesn't work):

$pdf->AddFont('DejaVu','','DejaVuSans.ttf',true);
$pdf->SetFont("DejaVu","",10);
$pdf->Cell(10,10 , "sometext");

$pdf->AddFont('DejaVu','','DejaVuSansMono.ttf',true);
$pdf->SetFont("DejaVu","",10);
$pdf->Cell(10,10 , "sometext");

Solution

  • For anyone still interested in an answer

    You only need to AddFont once:

    $pdf->AddFont('dejavusans', '', 'DejaVuSans.ttf', true);
    $pdf->AddFont('dejavusans', 'B', 'DejaVuSans-Bold.ttf', true);
    $pdf->AddFont('dejavusans', 'I', 'DejaVuSans-Oblique.ttf', true);
    $pdf->AddFont('dejavusans', 'BI', 'DejaVuSans-BoldOblique.ttf', true);
    

    Then, when using a font for a piece of text, use it like this:

    $pdf->SetFont('dejavusans', '', 9);
    

    or

    $pdf->SetFont('dejavusans', 'I', 10);
    

    So, only AddFont once, and then SetFont them where you need them. I tried using other names than 'dejavusans', but that did not seem to work.