Error generating a PDF by PHP

Asked

Viewed 298 times

2

I’m having a problem generating pdf, but is giving this error:

Notice: Undefined variable: mdpf in C:\xampp\htdocs\devweb\sistemas\painel\gerar-pdf.php on line 9

Fatal error: Uncaught Error: Call to a member function WriteHTML() on null in C:\xampp\htdocs\devweb\sistemas\painel\gerar-pdf.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\devweb\sistemas\painel\gerar-pdf.php on line 9

This is code I wrote:

<?php
    ob_start();
    include('templateFinanceiro.php');
    $conteudo = ob_get_contents();
    ob_end_clean();
    require ('../vendor/autoload.php');

    $mpdf = new \Mpdf\Mpdf();
    $mdpf->WriteHTML($conteudo);
    $mdpf->Output();
?>

If anyone can help, I’d really appreciate it.

1 answer

0


It’s a typo, you wrote it first $mpdf and then $mdpf, changed the p for d:

$mpdf = new \Mpdf\Mpdf();    ---> Aqui esta M P D F
$mdpf->WriteHTML($conteudo); ---> Aqui esta M D P F
$mdpf->Output();

The right thing would be:

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($conteudo);
$mpdf->Output();

It’s not necessary use to "instantiate", use is a way to make a nickname for a namespace and class, for example:

This:

use Foo\Bar\Baz;

Would be equivalent:

use Foo\Bar\Baz as Baz;

You can use new \Mpdf\Mpdf or the nickname, it makes no difference, Aliais if it was error of instantiating the error message nor would be:

Notice: Undefined variable: mdpf

The mistake would be:

PHP Fatal error: Uncaught Error: Class 'Mpdf' not found

  • arranged here.. it worked. thank you very much!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.