Nowadays PDF is the most used format in various web applications. There are multiple libraries like TCPDF, Snappy, mPDF etc can be used to create PDF in a web application. DomPDF is one of the most popular libraries to convert your data into PDF. Here are very easy steps to create HTML to PDF:
We are using default views/welcome_message.php
with controller/welcome.php
to generate pdf that is easy to understand. You can add your model & include it into the controller.
Step 1: Download DOMPDF from here. Paste it into application\libraries folder
Step 2: Create a file name named Pdf.php into application\libraries folder and paste below code.
Step 3 : Now create an controller that renders html view to PDF.
load->view('welcome_message');
$html = $this->output->get_output();
// Load pdf library
$this->load->library('pdf');
$this->pdf->loadHtml($html);
$this->pdf->setPaper('A4', 'landscape');
$this->pdf->render();
// Output the generated PDF (1 = download and 0 = preview)
$this->pdf->stream("html_contents.pdf", array("Attachment"=> 0));
}
}
Step 4 :In above controller we have used default welcome.php, you can replace with YOUR_VIEW.php file. Create a model if you are using custom view in step4 and load it into controller.
Welcome to CodeIgniter
Welcome to CodeIgniter!
The page you are looking at is being generated dynamically by CodeIgniter.
If you would like to edit this page you'll find it located at:
application/views/welcome_message.php
The corresponding controller for this page is found at:
application/controllers/Welcome.php
If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.
Page rendered in {elapsed_time} seconds. ' . CI_VERSION . '' : '' ?>
Editor
That’s it your rendered html in view file will looks like above image. Check it by http://YOUR_SITE_URL/welcome/generatepdf
So this is How to Convert HTML to PDF in CodeIgniter using Dompdf, If you are looking to create a unique, feature-rich and error-free web application development then you need to hire a dedicated developer from Techcompose Solutions who work solely on your project who will make sure your project is developed within the time frame and it meets your business requirement. Our Developers are experienced and had executed several small to complex development projects. We strictly believe in proper communication at the beginning which makes sure that your requirement is totally understood and we both are in the right track. Our developers know very well what your business may require to enable you to stand out in competition. Contact us to hire dedicated developers for developing a project on Codeigniter or you can reach us at inquiry@techcompose.com