How Can I add HTML And CSS Into PDF

How Can I add HTML And CSS Into PDF

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Converting HTML to PDF: Moving Beyond Broken Tables and Out-of-Memory Errors

Generating a PDF from an existing HTML document is a deceptively difficult task. The core challenge lies in the fundamental difference between rendering engines: browsers use JavaScript-based layouts with complex font hinting, anti-aliasing, and dynamic content loading, whereas PDF generators often rely on simplified renderers that strip away these capabilities. When you attempt to convert standard HTML/CSS into a PDF format, the result is rarely a one-to-one pixel match.

In many legacy PHP environments, developers have relied on libraries like DOMPDF or HTML2PDF. Based on historical usage patterns, DOMPDF frequently struggles with complex table structures and image loading, often leading to out-of-memory errors when processing large nested tables. These issues stem from the library's internal rendering engine, which can consume excessive memory (often hitting 128MB limits in older PHP configurations) before crashing or producing malformed layouts. Similarly, tools like HTML2PDF may handle images better but often fail with obscure node_type() errors when encountering specific CSS complexities, indicating a mismatch between the document's DOM structure and the library's parser capabilities.

Older utilities like Htmldoc represent a different class of problem: they lack modern CSS support entirely. If your document relies on external stylesheets or advanced CSS selectors, these tools are obsolete for production use. While client-side wrappers like Html2Pdf Pilot offered better results in Windows environments, the requirement to run on-demand via PHP on a Linux server necessitates a shift toward more robust, headless browser solutions.

The modern standard for this problem is wkhtmltopdf. Unlike DOMPDF, which attempts to render HTML directly into a PDF grid using a simplified model, wkhtmltopdf utilizes the WebKit rendering engine—the same technology behind Safari and older versions of Chrome. This approach provides significantly better support for CSS3, complex layouts, and image handling.

To implement this on a Linux server, you first need to ensure the dependency is met:

sudo apt-get install libfontconfig1 wget
wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.5-1/debian/0.12.5-1_all.deb -O wkhtmltopdf.deb
sudo dpkg -i wkhtmltopdf.deb

Once installed, you can invoke it directly via a command line or wrap it in a PHP script. Here is a basic implementation example:

<?php

$url = 'http://example.com/page.html';
$output = '/path/to/output.pdf';

$command = "wkhtmltohtml {$url} {$output}"; // Note: verify correct output flag for your version
// Or more commonly used direct conversion syntax depending on installation
$command = "wkhtmltopdf {$url} {$output}";

exec($command, $output_of_command, $return_code);

if ($return_code === 0) {
    echo "PDF generated successfully.";
} else {
    echo "Failed to generate PDF. Return code: " . $return_code;
}

?>

However, relying solely on the default configuration often leads to layout shifts because WebKit does not always respect the exact CSS media queries intended for screen display. You may need to pass specific options to force compatibility, such as --enable-local-file-access if your HTML references local assets or uses relative paths that the server cannot resolve correctly from the web root.

Another critical consideration is font embedding. By default, wkhtmltopdf might substitute missing fonts with system defaults, breaking your design. You can embed custom fonts directly into the PDF generation process:

wkhtmltopdf --enable-local-file-access --use-system-fonts http://example.com/index.html output.pdf

If you are working within a Laravel project and need to automate this process for specific routes or views, integrating wkhtmltopdf via a service class allows you to keep the logic decoupled from your controller. This approach ensures that the heavy lifting of rendering happens outside the request loop, preventing timeouts on complex pages.

While DOMPDF remains an option for static, simple documents where memory is constrained, wkhtmltopdf offers the fidelity required for most modern web applications. The trade-off involves managing the binary installation and ensuring the underlying WebKit version matches your security requirements. For high-security environments, consider using a headless browser like Puppeteer or Playwright instead of wkhtmltopdf, as these provide full JavaScript execution and modern CSS support, effectively solving the "broken table" and "missing image" issues that plagued earlier PHP libraries.

If you require further guidance on integrating PDF generation into your Laravel application or need assistance optimizing your rendering pipeline for production environments, you can explore resources at https://laravelcompany.com.

Our network