We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Integrating fpdf 1.81 with phalcon

I'm trying to make a simple test integrating fpdf with phalcon, however, I am facing a problem that says that php class is not found:

I have placed fpdf folder in library folder .

 /library/fpdf181

In config file libraryDir points to library folder...

'application' => [
        'appDir'         => APP_PATH . '/',
        'controllersDir' => APP_PATH . '/controllers/',
        'modelsDir'      => APP_PATH . '/models/',
        'migrationsDir'  => APP_PATH . '/migrations/',
        'viewsDir'       => APP_PATH . '/views/',
        'pluginsDir'     => APP_PATH . '/plugins/',
        'libraryDir'     => APP_PATH . '/library/',
        'cacheDir'       => BASE_PATH . '/cache/',

In loader I have registered library folder :

$loader->registerDirs(
    [
        $config->application->controllersDir,
        $config->application->modelsDir,
        $config->application->libraryDir,

    ]
)->register();

A simple test view, showpdf.phtml


    <?php

    error_reporting(E_ALL);
    echo 'aaaaa';
    $pdf = new FPDF(); // Class FPDF located in /library/fpdf181/fpdf.php file
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,' Hello PDF World');
    $pdf->Output();
    echo 'proba4';
    ?>

echoed string 'aaaaa' is printed but pdf is not created ...

Console log says :

PHP Fatal error:  Uncaught Error: Class 'FPDF' not found in /var/www/html/phonebook/app/views/index/showpdf.phtml:9

What am I doing wrong? please , any help is welcome..

Thanks in advance



85.5k
edited Jun '18

make sure lib is installed composer require setasign/fpdf:1.8.1

and add this at the beginning


require 'vendor/autoload.php'
$loader->registerDirs(
    [
        $config->application->controllersDir,
        $config->application->modelsDir,
        $config->application->libraryDir,

    ]
)->register();

registerDirs is NOT recursive, so it will only register classes in that folder, but none in child directories. Do it as @Izopi4a recommended, use composer to download the lib and then require the vendor autoload in the loader.php to use it in your app.



2.9k
edited Jun '18

Thank you for the quick respond.

I installed fpdf by the composer and my vendor folder is placed inside app fodler :

testapp/app/vendor   //phalcon folder 

as you proposed.

in index.php I added code as you proposed

<?php
use Phalcon\Di\FactoryDefault;

error_reporting(E_ALL);

define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');

require 'vendor/autoload.php';

Now, my application does not work at all.

What's wrong ? sorry, I am not an experienced user. Can you help me. Thanks in advance.

make sure lib is installed composer require setasign/fpdf:1.8.1

and add this at the beginning


require 'vendor/autoload.php'

Preferred way to include Composer's autoloader in Phalcon:

//Include composer autoloader - Phalcon way as of 3.0.x
$loader->registerFiles([APP_PATH . '/vendor/autoload.php']);

your index.php (if placed in public or so dir) shall look like:

//Define APP_PATH
define('APP_PATH', realpath('..'));


2.9k
edited Jun '18

Thank you for your inputs..

My loader.php looks like this :

<?php

$loader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader->registerDirs(
    [
        $config->application->controllersDir,
        $config->application->modelsDir,
        $config->application->libraryDir,

    ]
)->register();

$loader->registerFiles([APP_PATH . '/vendor/autoload.php']);

By CTRL + clicking in my PHPStorm inside showpdf.php on FPDF() Class declaration i see the component source

$pdf = new FPDF();

However, when i run showpdf.php i still see only echoed 'aaaaa' from the code i posted earlier. ..

Am I missing something again ?

I'm looking forward for your help. Definitelly thanks in advance. :)

Preferred way to include Composer's autoloader in Phalcon:

//Include composer autoloader - Phalcon way as of 3.0.x
$loader->registerFiles([APP_PATH . '/vendor/autoload.php']);

your index.php (if placed in public or so dir) shall look like:

//Define APP_PATH
define('APP_PATH', realpath('..'));
edited Jun '18

FYI: this lib (FPDF) sux when it comes to performance.

edited Jun '18

Here's working Phalcon Micro example with your code:

<?php
use Phalcon\Mvc\Micro;

$loader = new Phalcon\Loader();

//Include composer autoloader - Phalcon way as of 3.0.x
$loader->registerFiles([realpath('.') . '/vendor/autoload.php']);
// Register autoloader
$loader->register();

$app = new Micro();

$app->get(
    '/',
    function () {
    echo 'aaaaa';
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,' Hello PDF World');
    $pdf->Output();
    echo 'proba4';

return 0xff;
    }
);

$app->handle();

Wait what? Are you from hungary? :D

echo 'proba4';

Here's working Phalcon Micro example with your code:

<?php
use Phalcon\Mvc\Micro;

$loader = new Phalcon\Loader();

//Include composer autoloader - Phalcon way as of 3.0.x
$loader->registerFiles([realpath('.') . '/vendor/autoload.php']);
// Register autoloader
$loader->register();

$app = new Micro();

$app->get(
   '/',
   function () {
   echo 'aaaaa';
   $pdf = new FPDF();
   $pdf->AddPage();
   $pdf->SetFont('Arial','B',16);
   $pdf->Cell(40,10,' Hello PDF World');
   $pdf->Output();
   echo 'proba4';

return 0xff;
   }
);

$app->handle();
edited Jun '18

Root cause of your problem was wrong Loader usage:

$loader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader->registerDirs(
    [
        $config->application->controllersDir,
        $config->application->modelsDir,
        $config->application->libraryDir,

    ]
)->register();

//here you're trying to register Composer AFTER your loader have been registered above ^^
$loader->registerFiles([APP_PATH . '/vendor/autoload.php']);

Instead, your code must look like:

$loader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader->registerDirs(
    [
        $config->application->controllersDir,
        $config->application->modelsDir,
        $config->application->libraryDir,

    ]
); //register() method call removed here

$loader->registerFiles([APP_PATH . '/vendor/autoload.php']);

// Register autoloader at the very end
$loader->register();

Hi Lajos :)

I'm not from HU, but it seems that word shares the same meaning across our languages. :)

I just used example provided by OP.

Cheers

Wait what? Are you from hungary? :D

echo 'proba4';

Also it's better to use wkhtmltopdf anyway.

edited Jun '18

@dschissler Why would we ever need to do that? Since ages composer recommends to use it that way: https://getcomposer.org/doc/01-basic-usage.md#autoloading

The Phalcon Loader takes care to autoload both Phalcon, custom classes and files, and last but not least simply composer autoloader. That's KISS to me.

Well, vast majority of teams/projects will include Composer that way, so that's like a de-facto standard AFAIK.

I do appreciate more optimal way you're describing though.

And yes, Phalcon really does not do any black magic when it comes to including files via registerFiles() etc.

But it's just that it's clean that way, KISS and maintainable.



2.9k

Thank you all for both your time and patience to contribute the answering on the topic. :)

I have made it that code works, however when trying to open the rendered pdf document in either Firefox or Chrome I cant do that because the system says that pdf document is damaged. What could be wrong that rendered pdf is damaged ? Thanks in advance



2.9k

Hi Lajos,

YI it's Croatian language , and we share the same word with the same meaning :)

Here's working Phalcon Micro example with your code:

<?php
use Phalcon\Mvc\Micro;

$loader = new Phalcon\Loader();

//Include composer autoloader - Phalcon way as of 3.0.x
$loader->registerFiles([realpath('.') . '/vendor/autoload.php']);
// Register autoloader
$loader->register();

$app = new Micro();

$app->get(
   '/',
   function () {
   echo 'aaaaa';
   $pdf = new FPDF();
   $pdf->AddPage();
   $pdf->SetFont('Arial','B',16);
   $pdf->Cell(40,10,' Hello PDF World');
   $pdf->Output();
   echo 'proba4';

return 0xff;
   }
);

$app->handle();


2.9k

I put echoes just to control execution of php code.

Probably some error messages or echo statements are being included in the output. Why are you echoing aaaaa and proba4? Also I don't like returning 0xff and I think that a decent PDF generation library should be able to terminate the file though a method.

yes write quetion
Java. Java decreased in popularity by about 6,000 job postings in 2018 compared to 2017, but is still extremely well-established. ... Python. Python grew in popularity by about 5,000 job postings over 2017. ... JavaScript. ... C++ ... C# ... PHP. ... Perl.