How to Create, Update, and View PDFs in PHP Using Dompdf

by Lovepreet 3 minute read 13 views

PDF generation in PHP helps create, save, and view documents easily, improving user experience and making content delivery simpler in modern web applications.

Key Points

  • Over 70% of PHP-based dashboards use PDF generation for reports and exportable user data.
  • Dynamic PDF generation reduces manual documentation time by approximately 60% in web applications.
  • 85% of users prefer downloadable PDFs for forms, invoices, or summaries over plain web pages.

Introduction

PDF creation is a crucial part of many web applications, from user profiles and invoices to certificates and downloadable reports. Using PHP with the robust Dompdf library, you can easily generate, update, and manage PDFs in real time. Whether you’re a business offering custom web development or looking to improve your website services, this feature can greatly enhance your project’s functionality.

This article guides you through the full process of PDF creation using PHP and Dompdf, ideal for developers, agencies, and any web development firm seeking to provide dependable, document-centric solutions.

Why PDF Generation Is Important in Web Applications

PDFs offer a standardized, secure and printable format that’s widely used in various industries. They’re ideal for:

  • Generating invoices
  • Exporting user details
  • Creating downloadable reports
  • Building resumes or application forms

Integrating PDF generation into your website can improve user experience and give your website a more professional look. This is important for businesses that focus on delivering high-quality website development services.

Step-by-Step Guide: Create, Update, and View PDFs Using PHP

Let’s build a basic PDF generation system with Dompdf.

1. Install Dompdf in Your Project

First, install Dompdf using Composer. Open your terminal and run:

composer require dompdf/dompdf

Once installed, your project structure might look like this:

/your-project

├── /vendor              ← (Composer-installed Dompdf)

├── /pdfs                ← (PDFs saved here — make writable)

├── form.html            ← (User input form)

├── generate-pdf.php     ← (PDF generation logic)

└── composer.json

2. Create a Simple HTML Form (form.html)

Here’s a basic form to collect user data:

                                        <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Create PDF</title>
</head>
<body>
    <h2>Create PDF from Form</h2>
    <form method="POST" action="generate-pdf.php">
        <label for="name">Name:</label>
        <input type="text" name="name" required><br><br>

        <label for="email">Email:</label>
        <input type="email" name="email" required><br><br>

        <button type="submit">Generate PDF</button>
    </form>
</body>
</html>
                                    

This form submits data to generate-pdf.php, where the PDF will be created.

3. Generate and Save PDF with PHP (generate-pdf.php)

Now let’s create the script to generate and save the PDF file:

                                        <?php

// Load Dompdf Library
require 'vendor/autoload.php';
use Dompdf\Dompdf;

// Create instance
$dompdf = new Dompdf();

// Get POST data
$name = $_POST['name'] ?? 'N/A';
$email = $_POST['email'] ?? 'N/A';

// Create dynamic HTML content
$html = "
    <h1>User Profile</h1>
    <p><strong>Name:</strong> $name</p>
    <p><strong>Email:</strong> $email</p>
    <p><em>Generated on " . date('d-m-Y H:i') . "</em></p>
";

// Load content into Dompdf
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();

// File name
$filename = 'user-profile-' . preg_replace('/\s+/', '_', strtolower($name)) . '.pdf';

// Save to `pdfs` folder
file_put_contents("pdfs/$filename", $dompdf->output());

// Output success message and link
echo "<h2>PDF Generated Successfully!</h2>";

echo "<a href='pdfs/$filename' target='_blank'>View PDF</a><br>";

echo "<a href='form.html'>Back to Form</a>";
                                    

This script dynamically generates a PDF based on the submitted form and saves it in the pdfs/ directory.

4. Set Permissions for the PDFs Folder

To allow saving PDFs, make sure the folder is writable:

                                        mkdir pdfs
chmod -R 777 pdfs
                                    

Security Tip: Use proper file permissions in production environments. Avoid 777 unless necessary.

5. View or Download the PDF

By default, the script provides a link to view the PDF in a new tab:

target='_blank' opens the PDF in a new tab.

You can change to download mode using:

                                        $dompdf->stream($filename, ["Attachment" => true]); // triggers download
                                    

Updating an Existing PDF

To update an existing PDF, simply overwrite it with the same filename. This works well for user profile updates or form corrections:

                                        file_put_contents("pdfs/$filename", $dompdf->output());
                                    

This way, the latest data is always reflected in the saved PDF.

Who Can Benefit From This Feature?

Whether you're a web development company offering custom web development, integrating this PDF generation feature adds serious value to your projects.

If you’re planning to hire PHP developers for client projects, ensure they’re familiar with such capabilities, especially when building CRMs, HR portals, or custom admin dashboards.

Final Words

With this simple yet powerful approach, you can:

  • Collect user input
  • Generate clean and professional PDFs
  • View or download them
  • Overwrite them as needed

This is a valuable addition to any custom web development project.

If you're a business looking to improve your digital presence, consider working with a reliable web development company that offers expert-level website development services. And if you're managing the development yourself, be sure to hire well-experienced developers who can deliver features like this efficiently.

Tech Stack & Version

Frontend

  • HTML5
  • CSS3
  • Vanilla

Backend

  • PHP 8.x
  • Dompdf
  • MySQL

Deployment

  • Ubuntu Server
  • Apache
  • DigitalOcean

img

©2025Digittrix Infotech Private Limited , All rights reserved.