Custom Module from mPDF to TCPDF

Hi,
Please, I need a little help with this, I’m not programmer :frowning:
I’m trying to modify a custom module for TCPDF using the mPDF custom module created by sid (HTML to PDF Action - NodeJS & PHP [Open Source]) with no luck.
The mPDF module works greats but I need a specific function missing in mPDF.

The mPDF custom module is this:

<?php

namespace modules;

use \lib\core\Module;

require_once __DIR__ . '/../../../vendor/autoload.php';

class HtmlToPDF extends Module

{

    public function ConvertToPDF($options, $name)

    {

I already installed tcpdf with composer but when I call this class:

$pdf = new TCPDF($orientation, PDF_UNIT, $pageLayout, true, 'UTF-8', false);

I receive this message:

message: "Class \"modules\\TCPDF\" not found"

And for what I see in my unknowlegment is that I need to call the class TCPDF first.
How can I do to solve this, thanks.

Show the whole file you’re calling this class, it’s easier to see if there’s anything missing :slight_smile:

Also, you said you’ve installed through Composer, so make sure to do composer dump-autoload as well. And do you have the require_once autoload.php line?

Edit: Try to add the following line:

use TCPDF;
1 Like

Thanks, works perfectly!

Sorry, I’m just need a bit more of help;
I need to use the variable $backgroundImage from “class HtmlToPDF” inside “class MYPDF”, I don’t know if this is possible, because $backgroundImage comes from ‘hjson’ file from same module.

<?php
namespace modules;

use \lib\core\Module;
use TCPDF;

require_once __DIR__ . '/../../../vendor/autoload.php';

class MYPDF extends TCPDF {
    public function Header() {
       
        $bMargin = $this->getBreakMargin();
        $auto_page_break = $this->AutoPageBreak;
        $this->SetAutoPageBreak(false, 0);

        $img_file = __DIR__.'/../../../assets/certificados/plantillas/CertificadoAsistenciaCITEDIS-I.png';
        $this->Image($img_file, 0, 0, 297, 210, '', '', '', false, 300, '', false, false, 0);

        $this->SetAutoPageBreak($auto_page_break, $bMargin);
        $this->setPageMark();
    }
}

class HtmlToPDF extends Module
{
    public function ConvertToPDF($options, $name)
    {
        $pageHeight = $this->app->parseObject($options->pageHeight);
        $pageWidth = $this->app->parseObject($options->pageWidth);
        $fileName = $this->app->parseObject($options->fileName);
        $folderName = $this->app->parseObject($options->folderName);

        // I need to call this:
        $backgroundImage = $this->app->parseObject($options->backgroundImage);

The reason is that I need to replace $img_file with $backgroundImage to be dynamical using wappler DataBindings that comes ‘.hjson’ file of the same module HtmlToPDF.

Thanks in advance.

This exceeds my knowledge about PHP, but I’m going to throw some ideas anyway.

Backup your files, and then start using the following base:

<?php
namespace modules;

use \lib\core\Module;

require_once __DIR__ . '/../../../vendor/autoload.php';

class HtmlToPDF extends Module
{
    use TCPDF;
    public $backgroundImage = "";

    public function Header() {
       
        // $this refers to this class called "HtmlToPdf"
        // $pdf refers to class TCPDF
        // https://tcpdf.org/examples/example_001/
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

        $bMargin = $pdf->getBreakMargin();
        $auto_page_break = $pdf->AutoPageBreak;
        $pdf->SetAutoPageBreak(false, 0);

        //$img_file = __DIR__.'/../../../assets/certificados/plantillas/CertificadoAsistenciaCITEDIS-I.png';
        $img_file = $this->backgroundImage; // ConvertToPDF($options, $name) needs to be called first (by Wappler), so $this->backgroundImage is populated
        $pdf->Image($img_file, 0, 0, 297, 210, '', '', '', false, 300, '', false, false, 0);

        $pdf->SetAutoPageBreak($auto_page_break, $bMargin);
        $pdf->setPageMark();
    }

    public function ConvertToPDF($options, $name)
    {
        $pageHeight = $this->app->parseObject($options->pageHeight);
        $pageWidth = $this->app->parseObject($options->pageWidth);
        $fileName = $this->app->parseObject($options->fileName);
        $folderName = $this->app->parseObject($options->folderName);

        // I need to call this:
        $backgroundImage = $this->app->parseObject($options->backgroundImage);
        $this->backgroundImage = $backgroundImage; // Put $backgroundImage in class variable $this->backgroundImage

// FILL IN CONTENTS FROM THE REST OF THE ORIGINAL FILE

I got rid of class MYPDF extends TCPDF, because I’m not really sure how you would end up calling that within Wappler. I have moved your function Header() to inside class HtmlToPDF, so there you could have access to $backgroundImage after calling ConvertToPDF.

You have to realize you still can’t call Header() from Wappler. It would need to be like:

public function Header($options, $name)

And you would need to edit the .hjson to add the ability for Wappler to call that function.

Or just put all your custom code inside function ConvertToPDF? Maybe easiest option if you don’t need the original function :slight_smile:

I get this message error:

message: "Trait \"modules\\TCPDF\" not found"

Try:

use \TCPDF;

Now I get:
message: "modules\\HtmlToPDF cannot use TCPDF - it is not a trait"

Maybe try this:

<?php
namespace modules;

use \lib\core\Module;

require_once __DIR__ . '/../../../vendor/autoload.php';

use TCPDF; // Try here
// or use \TCPDF;

class HtmlToPDF extends Module
{
    // use TCPDF;
    public $backgroundImage = "";

Both options they throw:
message: "Undefined variable $pdf"

How so?

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

We’re defining $pdf there

Edit: Unless new TCPDF is erroring, in which case it’s possible $pdf is not created. But there must be another error before that then

Yes, this same with your modification, but nothing.:

<?php
namespace modules;

use \lib\core\Module;

require_once __DIR__ . '/../../../vendor/autoload.php';

use TCPDF; // Try here
// or use \TCPDF;

class HtmlToPDF extends Module
{
    // use TCPDF;
    public $backgroundImage = "";
    public function Header() {
       
        // $this refers to this class called "HtmlToPdf"
        // $pdf refers to class TCPDF
        // https://tcpdf.org/examples/example_001/
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

        $bMargin = $pdf->getBreakMargin();
        $auto_page_break = $pdf->AutoPageBreak;
        $pdf->SetAutoPageBreak(false, 0);

        //$img_file = __DIR__.'/../../../assets/certificados/plantillas/CertificadoAsistenciaCITEDIS-I.png';
        $img_file = $this->backgroundImage; // ConvertToPDF($options, $name) needs to be called first (by Wappler), so $this->backgroundImage is populated
        $pdf->Image($img_file, 0, 0, 297, 210, '', '', '', false, 300, '', false, false, 0);

        $pdf->SetAutoPageBreak($auto_page_break, $bMargin);
        $pdf->setPageMark();
    }

Edit:
Sorry you were right, code after that was calling $pdf
Now I don’t get any error, just it doesn’t generate any output file. I will trying to debug and if this becomes mission impossible for me I will back to bother you again from here.
I really appreciate your help.

No problem, and good luck! I believe this is as far as I can go, as I don’t know how to help you further. You can try talking to sid if you experience further issues