phpfpdf

Order of AddFont calls in tFDPF generates error


I am using tFPDF library (extended fpdf to include unicode fonts).

I have run into a strange issue when adding multiple fonts. The following code does not produce an error:

$pdf->AddFont('DejaVu','','DejaVuSans.ttf',true);
$pdf->AddFont('DejaVuBold','','DejaVuSans-Bold.ttf',true);
$pdf->AddFont('DejaVuBoldOblique','','DejaVuSans-Oblique.ttf',true);
$pdf->AddFont('DejaVuOblique','','DejaVuSans-BoldOblique.ttf',true);

But as you can see, I accidentally added a font called "DejaVuBoldOblique" which is actually only oblique, with "DejaVuOblique" being both both and oblique. So, when I rendered the PDF the text wasn't displaying as expected. However, I get an error when I fix the code to:

$pdf->AddFont('DejaVu','','DejaVuSans.ttf',true);
$pdf->AddFont('DejaVuBold','','DejaVuSans-Bold.ttf',true);
$pdf->AddFont('DejaVuOblique','','DejaVuSans-Oblique.ttf',true);
$pdf->AddFont('DejaVuBoldOblique','','DejaVuSans-BoldOblique.ttf',true);

FPDF error: Undefined font: dejavuoblique

Any ideas here, why the ordering would make a difference?


Solution

  • I've reviewed your question what i understood you're trying to add DejaVu and DejaVu Bold font. Your Code:

    $pdf->AddFont('DejaVu','','DejaVuSans.ttf',true);
    $pdf->AddFont('DejaVuBold','','DejaVuSans-Bold.ttf',true);
    

    use below code instead of above:

    // Add a Unicode font (uses UTF-8)
    $tfpdf->AddFont('DejaVu', '', 'DejaVuSansCondensed.ttf', true);
    $tfpdf->AddFont('DejaVu', 'B', 'DejaVuSansCondensed-Bold.ttf', true);
    

    DejaVuSansCondensed fonts available in tFPDF latest version i.e. ( http://www.fpdf.org/en/script/script92.php )

    After downloaded you've to made fews changes in the tfpdf.php file function "SetFont". paste below code in line number 619.

    /* Modified by Vinod Patidar due to font key does not match in dejavu bold.*/
    if ( $family == 'dejavu' && !empty($style) && ($style == 'B' || $style == 'b') ) {
        $fontkey = $family.' '.strtolower($style);
    } else {
        $fontkey = $family.$style;  
    }
    /* Modified end here*/
    

    If anything is unclear please let me know i'm glad to help you!