How to output PHP in DOMPDF loadhtml() function?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Output PHP in the DOMPDF loadhtml() Function: A Developer's Guide

As developers building applications, we often find ourselves at the intersection of dynamic data handling (PHP) and static document generation (like PDF creation via DOMPDF). When attempting to inject live PHP logic directly into an HTML string passed to functions like loadhtml(), a common pitfall emerges. This post will dissect why inline PHP fails in this context and provide the robust, correct methodologies for rendering dynamic content into your PDF documents.

The Pitfall of Inline PHP in HTML Strings

The core issue you are encountering stems from a fundamental misunderstanding of how string manipulation interacts with template engines versus raw code execution. When you pass a string to loadhtml(), DOMPDF treats that string as literal HTML content to be parsed and rendered. It does not execute the embedded PHP tags (<?php ... ?>) within that string.

When you write:

$pdf->loadHTML('<body onload="...">' . "Content... <?php echo $test; ?>...</body>");

The loadhtml function receives a single, static HTML string. The PHP tags are simply treated as text characters within the HTML document rather than executable code. Consequently, no dynamic output is generated into the PDF stream, leading to the static result you observed.

This is particularly true when dealing with libraries that expect pure HTML input. While some templating engines (like Blade in Laravel) handle this complexity beautifully by compiling views into plain strings before they hit the presentation layer, raw PHP string concatenation requires explicit handling.

The Correct Approach: Pre-processing Data Before Rendering

The solution is to separate the logic phase (calculating data and looping) from the presentation phase (generating the HTML string for the PDF). You must execute all your necessary PHP logic before constructing the final HTML string that you pass to DOMPDF.

This involves building the entire content dynamically within your PHP script, ensuring that only static HTML is passed to the PDF generator.

Example: Dynamic Content Generation

Instead of trying to solve the output inside the HTML block, build the HTML structure using PHP loops and concatenation first.

<?php
// 1. Define your dynamic data
$data = [
    'item1' => 'Value A',
    'item2' => 'Value B',
    'item3' => 'Value C'
];

// 2. Start building the HTML string dynamically
$htmlContent = '<html><head><title>Dynamic Report</title></head><body>';

// 3. Iterate and inject data into the HTML structure
foreach ($data as $key => $value) {
    // Use PHP to safely echo the variable within the context of an HTML tag
    $htmlContent .= "<p><strong>{$key}:</strong> " . htmlspecialchars($value) . "</p>";
}

$htmlContent .= '</body></html>';

// 4. Load the fully constructed string into DOMPDF
$pdf->loadHTML($htmlContent);
?>

Notice that the PHP execution happens entirely before the $pdf->loadHTML() call. We use standard concatenation (.=) and ensure security by using htmlspecialchars() when outputting user-provided data into HTML tags, which is a crucial best practice in modern application development, much like adhering to Laravel’s principles of secure data handling.

Handling Array Iteration for Complex Structures

When you need to iterate through an array to create repetitive HTML elements—exactly as you would do with Blade directives—you must perform the iteration in PHP and generate the resulting HTML strings.

For complex structures, it is often cleaner to build an array of HTML fragments first, and then join them together. This keeps your logic modular and highly readable.

// Example demonstrating array-based HTML building
$data = [
    ['name' => 'Apple', 'price' => 1.00],
    ['name' => 'Banana', 'price' => 0.50]
];

$tableRows = '';

foreach ($data as $row) {
    // Construct an HTML table row dynamically
    $tableRows .= '<tr><td>' . htmlspecialchars($row['name']) . '</td><td>$' . number_format($row['price'], 2) . '</td></tr>';
}

$htmlContent = '<table><thead><tr><th>Item</th><th>Price</th></tr></thead><tbody>' . $tableRows . '</tbody></table>';

$pdf->loadHTML($htmlContent);

This method ensures that the data processing is handled by PHP, and only the final, static HTML structure is fed into DOMPDF. Think of this separation as fundamental to creating maintainable code, a principle that mirrors robust architectural design seen in scalable systems like those promoted by the Laravel ecosystem.

Conclusion

The key takeaway is to treat your application logic (PHP) and your presentation layer (HTML for PDF generation) as separate concerns. Never attempt to embed dynamic PHP tags directly into an HTML string intended for a rendering library like DOMPDF. Instead, use PHP to calculate all necessary content, iterate over your data to construct the full HTML string, and then pass that finalized string to loadhtml(). This approach ensures accuracy, security, and maintainability in your document generation process.