Search code examples
phplaravellaravel-5fpdf

Laravel - FPDF error: Undefined font


What I'm doing

I’ve been rewriting a barcode generating system using Laravel.

What's wrong

I’m instancing the classes normally, but using the metod $pdf->SetFont('Arial','B',$fontSize); I receive an error message FPDF error: Undefined font: helvetica B. As you can see below, the paste “font” is already included into the library paste.

enter image description here

My Code

<?php namespace App\Http\Controllers;

//Elements declaration of layout, libraries and model
use View, Input, Validator, FPDF, eFPDF, BarcodeClass, BarcodeEAN, DB, App\Models\Barcode;

class HomeController extends Controller {

public function index() {
    return view('frontend.home');
}

public function gerarPdf() {

    //some code

    //Creates a new PDF
    $pdf = new eFPDF('P', 'pt');

    //Modify PDF font
    $pdf->SetFont('Arial','B',$fontSize);

Inside FPDF.php

function _getfontpath()
{
    if(!defined('FPDF_FONTPATH') && is_dir(dirname(__FILE__).'font/'))
        define('FPDF_FONTPATH',dirname(__FILE__).'/font/');
    return defined('FPDF_FONTPATH') ? FPDF_FONTPATH : '';
}

Solution

  • Firtly, the getPath method was being required however the define wasn’t receiving the hole paste’s folder. Its dirname wasn’t working so I switched to app_path().'/Libraries/FPDF/font/' and, in the ending of the method, I’ve put exit; after $pdf->Output. If not, the browser would show the following characters:

    %PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream x�e��N�0��} �R<��?W$@�g(�g^�� ��r���~��w&