php pdf conversion

doctorbill
doctorbill used Ask the Experts™
on
I am using a version of dom pdf  which is throwing up the following error with php version 7.1.29:

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Cpdf has a deprecated constructor in C:\xampp7\htdocs\Inventas\Sites\includes\pdf\lib\class.pdf.php on line 49
I have checked the dompdf_config.inc.php file and see:
class  Cpdf {

This is what I have managed to find re. the version:
@contributor Helmut Tischer <htischer@weihenstephan.org>
* @version 0.5.1.htischer.20090507
Does anyone know how to get around this or as an alternative - another version of dom pdf?
class.pdf.php
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David FavorFractional CTO
Distinguished Expert 2018

Commented:
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP...

Is a warning, not a Fatal in PHP-7.1 + will eventually raise a Fatal at some point.

As I recall, this becomes a Fatal in PHP-8.0, and best if you read docs closely to find the exact PHP version where this warning promotes to a Fatal.

You just have to make sure your code is updated to fix this problem before you upgrade to a PHP version which promotes this warning to a Fatal.

Author

Commented:
Yes but how do I update the code
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Hey doctorbill,

When using Classes, you need to instantiate them (create new instances) and when you do that, the code in the constructor (ctor) gets called automatically. In previous versions of PHP the ctor for a class was simply a method with the same name as the class:

class MyClass {
    function MyClass() {
        // this is the Constructor for the class
    }
}

Open in new window

That naming convention for ctors is now deprecated in favour of the new way of doing things - the ctor is a method called __construct():

class MyClass {
    function __construct() {
        // this is the Constructor for the class
    }
}

Open in new window

You have a couple of choices here. Firstly, you can try renaming the ctor from your existing class.pdf.php file. You'll find it on line 321:

function  Cpdf ($pageSize = array(0, 0, 612, 792), $isUnicode = false, $fontcache = '', $tmp = '') {

Open in new window

Change the method name to __construct:

function  __construct ($pageSize = array(0, 0, 612, 792), $isUnicode = false, $fontcache = '', $tmp = '') {

Open in new window

That would be enough to prevent the deprecation error you're getting. The issue with doing this is that your class may also be using other deprecated methods.

Probably the better way is to update the Package to a later version if one exists. Alternatively, use a different, more up-to-date library (DomPDF for example - it's available on GitHub or as a Composer package), although that would likely mean you'd have to change your code.

Author

Commented:
Thanks all
Chris - I tried renaming the Cpdf and it worked but  it then showed up other errors - as you said it would
I will get a later version of DomPDF and try it

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial