how to generate the pdf but my idea of showing the stuff will be different. What I'll do is, I'll use a text editor to type something and simple I'll show you how we would have a pdf after submit
Gurpreet Kait
Author
Today we are going to learn about how we can generate a pdf in PHP, it can be any type of pdf you can modify sheet type and other in PHP itself. Let's move on the tutorial in which I assume that you know about the basic stuff about CodeIgniter so that it will be easy for you to get into.
Now, Let's install a fresh project and do some basic configurations.
Install the CodeIgniter4 Project
The whole concept I'm gonna explain in this article is how to generate the pdf but my idea of showing the stuff will be different. What I'll do is, I'll use a text editor to type something and simple I'll show you how we would have a pdf after submit.
Now, we will have output as you can see below. You can customize according to you.
Main Login of Generating the PDF
So, Now we are ready with our form and just need to put some logic code out there in the controller. So, the whole stuff we are going to do in the home controller. We just need to put the route in routes.php file.
Basically, we have to use this package if we want to install generate the pdf with PHP codeIgniter4. This is very easy to do. Now, We have to setup the controller stuff so that we can do final thing of generating the pdf.
Install the DomPDF package.
composer require dompdf/dompdf
After installing the package we have to setup the home controller file.
<?php
namespace App\Controllers;
use Dompdf\Dompdf;
class Home extends BaseController
{
public function index()
{
return view('exportpdf');
}
public function export(){
$data = $this->request->getVar('data');
// reference the Dompdf namespace
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml($data);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
return redirect('/');
}
}
Now you can easily generate the pdf. Go to the frontend and type something as I did It will be giving you the pdf file of text or whatever you will put in the Editor.
In this article we talked about we can generate the pdf In PHP CodeIgniter4. This is very easy to use and do any kind of stuff. You just have to use the package using 'use' keyword. After that you have initialize the package.
Using setpaper() method you can customize the sheet and some other stuff also.