Content Index
- What is Dompdf and why use it in CodeIgniter 4?
- Install Dompdf in CodeIgniter
- Create a PDF Controller
- Generate a dynamic PDF from a view in CodeIgniter 4
- Passing data to the view
- Practical example: inventory report in PDF
- Calculation of dynamic totals
- Movements table
- Common errors when using Dompdf in CodeIgniter 4
- Best practices for generating professional PDFs
- Frequently Asked Questions
- Conclusion
PDFs, which stands for Portable Document Format, is a file format widely used to present and share digital documents securely and reliably. They have the characteristic of not being directly editable, as is the case with a text document; they can contain text and images and can be viewed on most devices and operating systems through programs like Adobe or as easily as web browsers.
In CodeIgniter, although we don't have many specific packages for the framework, we are able to use regular PHP packages; in the case of PDFs, it's no exception, and in principle, we could use any PHP package for CodeIgniter by using composer for this task.
In this tutorial, I'm going to show you how to integrate Dompdf into CodeIgniter 4 to generate dynamic PDFs from HTML views, using real data from a database.
What is Dompdf and why use it in CodeIgniter 4?
Dompdf is an HTML-to-PDF converter. At its core, dompdf is (primarily) a CSS 2.1-compliant HTML layout and rendering engine written in PHP. It is a style-based renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements.
The following example shows how to use Dompdf to convert HTML and generate a PDF with minimal configuration.
- Specify the HTML content in the loadHtml() method of the Dompdf class.
- Render HTML as PDF using the render() method.
- Send the generated PDF to the browser using the stream() method.
Ideally, the PDF format is used for reading information in document form. The PDF file is indicated by the .pdf file extension and stands for Portable Document Format. A PDF document manifests information for eBooks, application forms, user manuals, and other documents.
In this tutorial, we assume you are a CodeIgniter developer and want to know how to create a PDF file from an HTML view template using the domPDF library.
Many novice developers get stuck when it comes to HTML-to-PDF conversion. Don't worry, we're going to make it easy for you. By the end of this tutorial, you can generate a PDF file from an HTML layout with the DomPDF library.
In this post, we are going to use Dompdf because of its ease of use and time on the market; it is an ideal solution for when we want to work with PDFs in PHP; its use is simple:
We specify the HTML we want to convert to PDF:
$dompdf->loadHTML('<h1>Hello World</h1><br><p>Other content</p>');We indicate the paper size:
$dompdf->setPaper('A4', 'portrait');We generate the PDF:
$dompdf->render();We start the download:
$dompdf->stream();Install Dompdf in CodeIgniter
In CodeIgniter 4, we don't need framework-specific packages. We can install any PHP library via Composer.
composer require dompdf/dompdfWith this, we already have the library available in our project.
Create a PDF Controller
With this done, we are completely ready to use Dompdf in our application, starting by creating an instance:
$dompdf = new Dompdf();And we generate the PDF and render it:
$dompdf->loadHTML('<h1>Hello World</h1><br><p>Other content</p>');
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream();In the case of CodeIgniter 4, we have the view functions that return the HTML in the controller; so we can use them perfectly instead of an HTML established from the controller:
$dompdf = new Dompdf();
$dompdf->loadHTML(view("dashboard/product/trace_pdf", $data));
$productModel = new ProductModel();
$product = $productModel->asObject()->find($productId);Generate a dynamic PDF from a view in CodeIgniter 4
This is where things really get interesting.
In my CodeIgniter 4 course (link at the end of the post), while developing a warehouse app, we needed to generate a complete product traceability report. It wasn't just showing data: there were joins, calculations, and cumulative totals.
$dompdf = new Dompdf();
//$dompdf->loadHTML('<h1>Hello World</h1><br><p>Other content</p>');
$productModel = new ProductModel();
$product = $productModel->asObject()->find($productId);
$query = $productModel->asObject()->select("pc.*, u.email, puc.description, puc.direction")
->join('products_control as pc', 'pc.product_id = products.id')
->join('users as u', 'pc.user_id = u.id')
->join('products_users_control as puc', 'pc.id = puc.product_control_id');
$data = [
'product' => $product,
'trace' => $query->where('products.id', $productId)
->findAll()
];
$dompdf->loadHTML(
view("dashboard/product/trace_pdf", $data)
);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream();Passing data to the view
$data = [
'product' => $product,
'trace' => $query->where('products.id', $productId)->findAll()
];
$dompdf->loadHTML(view("dashboard/product/trace_pdf", $data));Rendering the PDF
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream();The view looks like this:
<style>
* {
color:blue
}
</style>
<p>
Sales and purchases of <?= $product->name ?>
</p>
<ul>
<li>
Price <?= $product->price ?>
</li>
<li>
Last Entry <?= $product->entry ?>
</li>
<li>
Last Exit <?= $product->exit ?>
</li>
</ul>
<table>
<thead>
<tr>
<th>
ID
</th>
<th>
Date
</th>
<th>
Type
</th>
<th>
Quantity
</th>
<th>
User
</th>
<th>
Description
</th>
<th>Address</th>
<th>
Price
</th>
<th>
Total
</th>
</tr>
</thead>
<tbody>
<?php $total = 0 ?>
<?php foreach ($trace as $key => $t) : ?>
<tr>
<td>
<?= $t->id ?>
</td>
<td>
<?= $t->created_at ?>
</td>
<td>
<?= $t->type ?>
</td>
<td>
<?= $t->count ?>
</td>
<td>
<?= $t->email ?>
</td>
<td>
<?= $t->description ?>
</td>
<td>
<?= $t->direction ?>
</td>
<td>
<?= $product->price ?>
</td>
<td>
<?php $total += $product->price * $t->count ?>
<?= $product->price * $t->count ?>
</td>
</tr>
<?php endforeach ?>
<tr>
<td colspan="8">Total</td>
<td><?= $total ?></td>
</tr>
</tbody>
</table>And that's it! The important thing here is that we can easily extend the framework with any PHP package using composer, which is the recommended way when installing CodeIgniter 4.
Practical example: inventory report in PDF
This is where we go beyond most basic tutorials.
The view can have a structure like this:
<p>Sales and purchases of <?= $product->name ?></p>
<ul>
<li>Price <?= $product->price ?></li>
<li>Last Entry <?= $product->entry ?></li>
<li>Last Exit <?= $product->exit ?></li>
</ul>Calculation of dynamic totals
In the project I developed, I needed to calculate the total within the loop:
<?php $total = 0 ?>
<?php foreach ($trace as $t) : ?>
<?php $total += $product->price * $t->count ?>
<?php endforeach ?>This detail makes the difference between a static PDF and a professional report.
Movements table
The use of tables is fully compatible with Dompdf, as long as the HTML is clean and well-structured.
Practical tip based on experience:
Avoid very complex CSS or advanced modern properties. Dompdf works best with simple styles.
Common errors when using Dompdf in CodeIgniter 4
- Not using use Dompdf\Dompdf;
- Namespace problems
- Not loading the view correctly
- Attempting to render before obtaining data
- Using incompatible CSS
Many developers get stuck here, especially when the PDF comes out blank. In most cases, the problem lies in malformed HTML.
Best practices for generating professional PDFs
- Process all logic in the controller.
- Keep the view clean.
- Use simple styles.
- Correctly define paper size.
- If it's a large report, evaluate performance.
In real projects, generating dynamic PDFs can impact memory if the dataset is very large.
Frequently Asked Questions
- How to generate a PDF from a view in CodeIgniter 4?
- By using view() to generate the HTML and passing it to Dompdf's loadHTML().
- Can any PDF library be used in CodeIgniter 4?
- Yes, thanks to Composer you can integrate any PHP package.
- Does Dompdf support modern CSS?
- Partially. It is mainly based on CSS 2.1.
Conclusion
Integrating Dompdf into CodeIgniter 4 is technically simple, but doing it well (with real data, complex queries, and a clean structure) is what makes the difference.
In my experience working with inventory reports, the key is not in Dompdf, but in how you structure your MVC application before rendering the PDF.
If you understand that, you can generate:
- Invoices
- Reports
- Account statements
- Traceability
- Dynamic exports
And all using normal HTML views.