Reputation: 153
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
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
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