Generate PDF from HTML PHP

Generate PDF from HTML PHP

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

Stefan Bogdanescu

Founder & Senior Architect

The Reality of HTML Controls in Server-Side PDF Generation

You cannot simply pass raw HTML containing <input>, <textarea>, or complex form controls directly to libraries like FPDF, TCPDF, or DOMPDF and expect them to render as interactive fields. These libraries generate static vector graphics or raster images for the final document. When you feed an <input> tag into these engines, they typically treat it as text content that needs to be drawn on a canvas. Because standard HTML/CSS lacks the precise coordinate system required to position form elements within a fixed-width PDF page, the renderer often ignores the tags entirely or collapses them into plain text strings.

To achieve the visual result you are looking for—where a user sees a box with a label and perhaps a cursor in a browser—you must understand that PDF generation is fundamentally different from HTML rendering. The solution lies in abandoning the semantic <input> elements for the final document and replacing them with their visual equivalents using standard CSS styling or specialized PDF drawing commands.

Switching to Visual Simulations

The most robust approach when using libraries like TCPDF or FPDF is to simulate the look of a form control rather than rendering the actual control. You construct a div or a table cell that mimics the input field and apply inline CSS to style it as a border-box with padding.

In TCPDF, you would not use an <input> tag inside your HTML string passed to writeHTML(). Instead, you build the structure using standard HTML tags that the library understands:

$html = '<table width="100%">';
$html .= '    <tr>';
$html .= '        <td width="20%"><b>Username:</b></td>';
$html .= '        <td width="80%">';
// Simulate an input field using a styled div
$html .= '            <div style="border: 1px solid #ccc; padding: 4px; font-family: Arial;">';
$html .= '                ' . htmlspecialchars($username) . '
            </div>';
$html .= '        </td>';
$html .= '    </tr>';
$html .= '</table>';

$pdf->writeHTML($html, true, false, true, false, '');

Here, the div with a border and padding creates the visual illusion of an input box. The PHP variable $username is inserted inside this div. This method works because TCPDF parses standard HTML tags but renders the content within them as text, respecting the CSS borders you define.

Handling Text Areas and Dynamic Content

Text areas present a similar challenge. A real <textarea> implies multiline input with potential height adjustments, which static PDF generators cannot handle dynamically in the same way browsers do. To replicate this, you use a div or a <p> tag with explicit line-heights and padding.

If your source material includes CSS attached to the page, you must ensure that library-specific limitations are respected. DOMPDF is generally more lenient with CSS than FPDF, but it still struggles with complex form layouts because it renders HTML into a PDF context where many modern CSS properties (like :focus states or hover effects) are irrelevant and potentially ignored.

The critical trade-off here is flexibility versus fidelity. Libraries like Dompdf allow you to pass a full HTML string with extensive CSS, making them easier to configure for complex layouts without writing low-level drawing code. However, FPDF and TCPDF offer more granular control over every line drawn. If your goal is strictly visual representation of data (e.g., an invoice or a report), the simulation approach described above is superior. It ensures that the resulting PDF is lightweight, searchable, and compatible with any standard viewer without relying on the browser's rendering engine for the document layout.

Why Direct HTML Controls Fail

It is important to acknowledge why your initial attempts likely failed. When you attach CSS in the same file and try to render <input> or <textarea>, the PDF engine encounters a disconnect between the semantic meaning of the tag (an interactive form element) and the target medium (a static document). The engines lack a model for "interactive states." They do not know how to calculate the space required for an unknown length of text inside a textarea unless you explicitly define the height.

If you require actual editable fields in a PDF, you are moving beyond standard HTML-to-PDF conversion libraries. You would need to utilize AcroForms within Adobe Acrobat or specific libraries that support form field creation (like spipu.pdf or specialized wrappers around Ghostscript). However, for generating reports where the data is already populated and needs to be displayed statically, simulating the controls via CSS-styled divs remains the industry standard for PHP-based PDF generation.

For developers looking to streamline this process within a framework ecosystem, resources like https://laravelcompany.com often highlight best practices for integrating these libraries into modern Laravel applications, specifically regarding how to handle view rendering and asset piping before passing data to the PDF engine.

Our network