In this article, You will learn about how to create pdf file in Codeigniter from html using dompdf library . PDF is the most used format to generate PDF for invoice in the web application. PDF file provides a easy and user-friendly way to download the bunch of records in a pdf format file. Before download the web data records content in pdf format, the content needs to be converted from HTML to PDF. If you are working on Codeigniter then you get problem to how to do it. But in this post i am going to share you very simple example to html to pdf convert using DomPDF library.
Dompdf is a PHP library that helps to generate PDF file from HTML data content. It’s very easy to convert HTML to PDF in PHP with Dompdf.
Step 1: Download Fresh Codeigniter 3
In First step we need to download fresh version of Codeigniter 3, After Download successfully, extract clean new Codeigniter 3 application.
so if you haven’t download yet then download from here : Download Codeigniter 3.
Step 2: Download dompdf library from GitHub
Dompdf: let’s download dompdf library from here : Click Here to download dompdf. After download, extract it to your “application/libraries” folder and rename it to “dompdf”.
Create pdf.php file under “application/libraries” folder and paste the following code
Step 3: Add Controller Method
In this step we require to add “generatepdf” method on welcome controller, So let’s add with following code. you have to just copy of welcome.php controller file:
application/controllers/Welcome.php
Step-4: Add Route
let’s add following route on your routes.php file.
application/config/routes.php
Step 5: Add View File
Now at last step we require to create “generatepdf.php” view file for generate pdf file. So you have to copy bellow code and create on view folder:
application/views/generatepdf.php
Now you can open bellow URL on your browser:
http://localhost:8000/generatepdf
If you are on live sever:
http://domainname.com/generatepdf
Useful Methods of Dompdf
The following are some useful methods of Dompdf library to implement HTML to PDF conversion functionality.
- loadHtml(): Loads HTML content.
- $str (string) – Required. Specify the HTML to load.
- $encoding (string) – Optional. Specify encoding.
- loadHtmlFile(): Loads content from an HTML file.
- $file (string) – Required. Specify file path to load.
- output(): Returns the PDF as a string.
- $options (array) – Optional. Specify whether content stream compression will enable. (compress => 1 or 0)
- render(): Renders the HTML to PDF.
- setBasePath(): Sets the base path to include external stylesheets and images.
- $basePath (string) – The base path to be used when loading the external resources URLs.
- setPaper(): Sets the paper size & orientation.
- $size (string|array) – ‘letter’, ‘legal’, ‘A4’, etc.
- $orientation (string) – ‘portrait’ or ‘landscape’.
- stream(): Streams the PDF to the client.
- $filename (string) – Specify name of the file (without .pdf extension).
- $options (array) –
- ‘compress’ => 1 or 0 – enable content stream compression.
- ‘Attachment’ => 1 = download or 0 = preview
Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co