Saint Robson
Saint Robson

Reputation: 5525

The Path and Class Name is Correct, But Failed To Use Inherited PHP Class

I'm using FPDF and WriteHTML Scripts (http://www.fpdf.org/en/script/script42.php) to convert my HTML into PDF.

I've downloaded the FPDF library on my /lib/fpdf folder along with WriteHTML script.

so what inside /lib/fpdf folder is :

/lib/fpdf/fpdf.php
/lib/fpdf/html2pdf.php

then I write this code :

require_once ('html2pdf.php');
$html_code = ''; // some HTML code here

$pdf = new PDF_HTML();
$pdf->AddPage();
$pdf->WriteHTML($html_code);
$pdf->Output(); 

this simple code gives me this error :

Fatal error: Call to undefined method PDF_HTML::FPDF() in /home/***/html2pdf.php on line 52

and here's what line 52 really is :

$this->FPDF($orientation,$unit,$format);

Why I got that error message while I'm following the tutorial correctly. Why the html2pdf script failed to recognize the method? thank you

Upvotes: 0

Views: 1379

Answers (2)

Holt
Holt

Reputation: 37706

You should simply rename the function PDF_HTML in html2pdf.php to __construct (to make it a new style PHP constructor) and replace $this->FPDF by parent::__construct (to call the right function):

// THIS: 
function PDF_HTML($orientation='P', $unit='mm', $format='A4')
{
    //Call parent constructor
    $this->FPDF($orientation,$unit,$format);

// BECOMES THAT:
function __construct($orientation='P', $unit='mm', $format='A4')
{
    //Call parent constructor
    parent::__construct($orientation,$unit,$format);

Explanation:

The problem is that the latest version of fpdf.php (1.8) uses new style constructor (__construct) while PDF_HTML in script42.php has not been updated to uses new style constructors (old style constructor is ClassName).

Since PDF_HTML inherit from FPDF, $this->FPDF should be call to a parent constructor (see Calling PHP Parent Constructors With Old/New Syntax), which would work if FPDF was using old-style constructor:

class FPDF {
    // Constructor (in PHP < 5.3.3), Deprecated in PHP 7+
    public function FPDF () {

    }
}

class PDF_HTML {
    // Constructor (in PHP < 5.3.3)
    public function PDF_HTML () {
        $this->FPDF(); // Call to parent constructor
    }
}

You clearly see in script42.php that PDF_HTML uses the old style constructor:

function PDF_HTML($orientation='P', $unit='mm', $format='A4')

Unfortunately, FPDF 1.8 uses new style constructor:

class FPDF {
    public function __construct () { }
}

So you cannot call a parent constructor defined with the new style using the old style.


Note: If you really want to use the code of script42.php as it is now, you should try to find an old version of fpdf.php (FPDF 1.7 uses old style constructors) and use a PHP version < 7 because old style constructor are deprecated in PHP 7+ (and are considered standard function in PHP 5.3.3+).

Upvotes: 2

maxhb
maxhb

Reputation: 8865

The error says it all. You try to call a method that simply does not exist. I looked at the source of PDF_HTML class and there really is no such method.

Methods available are (+ methods defined by FPDF class):

  • PDF_HTML() The construciotr of that class
  • WriteHTML()
  • OpenTag()
  • CloseTag()
  • SetStyle()
  • PutLink()

Looks like you either instantiate the wrong class or call the wrong function.

Upvotes: 0

Related Questions