简体   繁体   English

FPDF - 找不到类'App \\ Http \\ Controllers \\ FPDF'

[英]FPDF - Class 'App\Http\Controllers\FPDF' not found

For my project I have to generate a PDF file. 对于我的项目,我必须生成一个PDF文件。 Stackoverflow told me to use FPDF. Stackoverflow告诉我使用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. 但是,我再次得到同样的错误。

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. 因为PSR-4自动加载命名空间你不必明确地包含它。

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. 您收到错误是因为如果没有正确的标题,pdf无法在HTML页面中打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM