Back

tcpdf on Lumen Class 'PDF' not found


decibel83 posted 1 year ago

I created a new project with Lumen 5.4.7 and I added the TCPDF library from elibyy/tcpdf-laravel version 5.4.2 (with Lumen support):

composer require elibyy/tcpdf-laravel "5.4.2"

I enabled the Facades and Service Provider in bootstrap/app.php with

$app->withFacades();
$app->register(Elibyy\TCPDF\ServiceProvider::class);

And I created a basic Controller:

<?php

namespace App\Http\Controllers;
use \PDF;

class PdfController extends Controller
{
    /**
     * Create a test PDF file.
     *
     * @return void
     */
    public function createTestPdf()
    {
      PDF::SetTitle('Hello World');
      PDF::AddPage();
      PDF::Write(0, 'Hello World');
      PDF::Output('hello_world.pdf');

      echo "Fatto!";
    }
}

with a basic route:

$app->get('pdf', '[email protected]');
But when I try to access to /pdf I get the following error:

Fatal error: Class 'PDF' not found in /Users/m/Documents/Projects/lumen-pdf/app/Http/Controllers/PdfController.php on line 15

(1/1) FatalErrorException
Class 'PDF' not found
in PdfController.php (line 15)
at Application->handleShutdown()
in RegistersExceptionHandlers.php (line 54)
at Application->Laravel\Lumen\Concerns\{closure}()

Could you help me please?

decibel83 replied 1 year ago Solution

I solved my problem.

One line was missing into bootstrap/app.php to add a new class_alias for Elibyy\TCPDF\Facades\TCPDF to PDF:

class_alias('Elibyy\TCPDF\Facades\TCPDF', 'PDF');

Thanks!


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us