Marty
Marty

Reputation: 153

FPDF - Class 'App\Http\Controllers\FPDF' not found

For my project I have to generate a PDF file. Stackoverflow told me to use FPDF. So, I followed the tutorial but it doesn't seem to work.

public function makePdf(Request $request){
    require('fpdf181/fpdf.php');
    $pdf = new FPDF('p', 'mm', 'A4');
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Hello World!');
    $pdf->Output();   
  }

This is completely following the tutorial but it doesn't work.

I tried this as well:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
require('fpdf181/fpdf.php');

But than again, I get the same error.

Upvotes: 3

Views: 3786

Answers (2)

Octaaf
Octaaf

Reputation: 175

You are not sending the correct headers if you see this error.

Doing it something like this should help:

$headers = array('Content-Type' => 'application/pdf');

return Response::make(PDF::load($html, 'A4', 'portrait')->show('my_pdf'), 200, $headers);

You get the error because a pdf cannot open in a HTML page without the proper header.

Upvotes: 1

FULL STACK DEV
FULL STACK DEV

Reputation: 15971

change this:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
require('fpdf181/fpdf.php');

to this:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use FPDF;

because of PSR-4 autoloading namespaces you dont have to include it explicitly.

Upvotes: 5

Related Questions