Roshan Kumar
Roshan Kumar

Reputation: 187

How to pass data to pdf in just DOMPDF?

I am stuck in a weird situation. How can I send PHP data to dompdf's pdf ?

Here is my code

index.php

<?php

require_once 'dompdf/autoload.inc.php';  
use Dompdf\Dompdf; 
use Dompdf\Options;

function runPDF () {
    $options = new Options();
    $options->set('isPhpEnabled', true);
    $dompdf = new Dompdf($options);

    $data = array('name'=>'John Smith', 'date'=>'1/29/15');

    $html = file_get_contents("pdf.php"); 
    $dompdf->loadHTML($html); 
    
    $dompdf->setPaper('A4', 'portrait'); 
    $dompdf->render(); 
    $dompdf->stream("niceshipest", array("Attachment" => 0));

}

if (isset($_GET['pdf'])) {
    runPDF();
  }
?>

<a href='?pdf=true' target="_blank">print</a>

pdf.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Value To PDF</title>
</head>
<body>
    <h1>
        Hello <?php echo $data['name']; ?>
    </h1>
</body>
</html>

So how can I access that $data value in generated pdf?

I have tried to change isPhpEnabled = false; to isPhpEnabled = true;

But got no results.

Framworke version of dompdf like laravel-dompdf provides feature to pass data in HTML like loadView(); but there is only loadHTML();

Any help really appreciate?

Thanks

Upvotes: 0

Views: 1724

Answers (1)

Ilham Elmo
Ilham Elmo

Reputation: 1

The solution it's very simple, you should just add the with() like this :

$dompdf->loadHTML($html)->with('data', $data);

And you should delete this :

require_once 'dompdf/autoload.inc.php'; 

That's it.

Upvotes: -1

Related Questions