1

I'm trying to use inline PHP in the DOMPDF loadhtml() function. Below is my sample code:

$pdf->loadHTML('<html>

<head>
<title>My first PHP Page</title>
</head>
<body>
This is normal HTML code

<?php 
    echo $test;
?>

Back into normal HTML

</body>
</html>');

when I use $pdf->stream() to output this pdf from it only prints " This is normal HTML code Back into normal HTML ". echo $test; does not produce any output.

I have an array with some string values. I need to iterate through each of them just like it is done in the blade preprocessor.

Why am I not able to use inline PHP this way?

PS. I have tried using the render() function but it fails because it is a Protected method.

Adeel Ahmad
  • 1,033
  • 1
  • 11
  • 24
  • Read this answer you will solve you problem http://stackoverflow.com/questions/28216993/dompdf-loadview-error-undefined-variable-data – donwizzz Nov 16 '16 at 16:20

2 Answers2

1

The simple answer is because DOMPDF only interprets HTML (including CSS) not PHP. A browser doesn't interpret PHP either. It is the web server that interprets PHP (or in this case dynamic HTML that includes PHP references) then sends the resultant HTML to the browser. Similarly DOMPDFonly deals with HTML when rendering. Of course, because it is written for PHP scripts, you can use PHP to manipulate the HTML string that is passed to the $dompdf->loadHTML() method.

Arthur Nicoll
  • 391
  • 1
  • 2
  • 8
0

You can just include the value of $test by using the concatenation operator:

$pdf->loadHTML('<html>

<head>
<title>My first PHP Page</title>
</head>
<body>
This is normal HTML code

' . $test . '

Back into normal HTML

</body>
</html>');

PHP tags in strings are not evaluated.

Shira
  • 6,392
  • 2
  • 25
  • 27
  • My problem is a bit different. I have an array which is populated with some values. In the html I need to iterate through each of them just like you do in the blade preprocessor. – Adeel Ahmad Jun 27 '16 at 12:28
  • What's stopping you from iterating the array, rendering it into `$test` and then concatenating it with the string you are passing to `loadHtml()`? Or you could use a templating engine to render the whole HTML page and then pass the result to `loadHtml()`. – Shira Jun 27 '16 at 13:18
  • @AdeelAhmad I don't know laravel at all, so take this with a grain of salt, but laravel-pdf supports loading views (`$pdf->loadView()`). Perhaps you can use a view to generate your HTML? – BrianS Jul 08 '16 at 17:20