Import library for Laravel 5

0

What I need

I need to import the FPDF library, modified for the project.

What I did

Code is below, but I created a "Libraries" folder inside "App" and put the library there (with the name FPDF.php), I put in Aliasses the name of the class and its path, besides including in the composer.json the folder path there in classmap . I also added namespace App\Libraries\FPDF; to the FPDF.php file within Libraries.

My folders and files structure

Whathappened

Well,youdonothavetobeageniustorealize:heisnotfindingtheclass.

config \ app.php

'aliases' => [
 ...
 ...

'FPDF' => 'App\Libraries\FPDF',

composer.json

"autoload": {
        "classmap": [
            "database",
            "app/Libraries"
        ],
        "psr-4": {
            "App\": "app/"
        }
    },

HomeController.php

public function gerarPdf() {
        if (Input::has('numeroDocumento') && Input::has('mesReferencia') && Input::has('anoReferencia')) {
            $fpdf = new FPDF();
            $fpdf->AddPage();
            $fpdf->SetFont('Arial','B',16);
            $fpdf->Cell(40,10,'Hello World!');
            $fpdf->Output();
            exit;

        }
        else {
            return redirect()->back()->with('message', 'Por favor, digite todos os campos !');
        }
    }
    
asked by anonymous 16.03.2015 / 19:01

2 answers

1

I believe that the FPDF class does not belong to any namespace, so in order to use it you would have to map in composer.json:

"autoload": {
    "classmap": [
        "database",
        "app/Libraries/FPDF"
    ]
},

And in the controllers you just need to do:

use FPDF;

and use:

$pdf = new FPDF();

documentation FPDF Library .

    
18.03.2015 / 00:23
1

The aliasses you are instantiating is a class FPDF within the namespace App\Libraries

in your config.php is like this

'aliases' => [
'FPDF' => 'App\Libraries\FPDF',

But what we want is to instantiate a class FPDF within the namespace App\Libraries\FPDF

It should look like this

'aliases' => [
'FPDF' => 'App\Libraries\FPDF\FPDF',

This way in your HomeController just give a use FPDF that should work

    
16.03.2015 / 21:39