Logo

dev-resources.site

for different kinds of informations.

How to Generate Pdf in PHP CodeIgniter

Published at
3/28/2023
Categories
codeigniter
Author
gurpreetkait
Categories
1 categories in total
codeigniter
open
Author
12 person written this
gurpreetkait
open
How to Generate Pdf in PHP CodeIgniter

How to Generate Pdf in PHP CodeIgniter

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.

  1. 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.

Install The CodeIgniter4

 composer create-project codeigniter4/appstarter exportpdf
Enter fullscreen mode Exit fullscreen mode

Create View File

Now, Lets create a view file. in which we will use a text editor. And put some code there of a simple HTML5 form. As you can see in the code below:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="main">
        <form action="<?= route_to('export') ?>" method="POST">
    <textarea name="data" id="" cols="30" rows="10" placholder="type anything and generate a pdf..."></textarea>
    <input type="submit" value="submit">
    </form>

    </div>
<script src="https://cdn.tiny.cloud/1/mw7vesckxxfqg12c9ezwqls9euqwdnqp2pxvbm6s0plb85j6/tinymce/6/tinymce.min.js" referrerpolicy="origin"></script>
<script type="text/javascript">
    tinymce.init({
        selector: 'textarea',
        plugins: 'anchor autolink charmap codesample emoticons image link lists media searchreplace table visualblocks wordcount checklist mediaembed casechange export formatpainter pageembed linkchecker a11ychecker tinymcespellchecker permanentpen powerpaste advtable advcode editimage tinycomments tableofcontents footnotes mergetags autocorrect',
        toolbar: 'undo redo | blocks fontfamily fontsize | bold italic underline strikethrough | link image media table mergetags | addcomment showcomments | spellcheckdialog a11ycheck | align lineheight | checklist numlist bullist indent outdent | emoticons charmap | removeformat',
        tinycomments_mode: 'embedded',
        tinycomments_author: 'Author name',
        mergetags_list: [{
                value: 'First.Name',
                title: 'First Name'
            },
            {
                value: 'Email',
                title: 'Email'
            },
        ]
    });
</script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Now, we will have output as you can see below. You can customize according to you.


tinymic editor

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.

$routes->get('/', 'Home::index');
$routes->post('/export', 'Home::export',['as' => 'export']);
Enter fullscreen mode Exit fullscreen mode

We setup the routes and now if you will serve the code you can see the form on browser.

php spark serve
Enter fullscreen mode Exit fullscreen mode

Install the DOMPDF Package

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
Enter fullscreen mode Exit fullscreen mode

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('/');
    }
}

Enter fullscreen mode Exit fullscreen mode

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.

Read more about CodeIgniter here: CodeIgniter4

Conclusion

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.

The post How to Generate Pdf in PHP CodeIgniter appeared first on Larachamp.

codeigniter Article's
30 articles in total
Favicon
CodeIgniter Monitoring Library – Born from Understanding Real Developer Needs
Favicon
Is CodeIgniter Still Relevant in 2025?
Favicon
Laravel vs CodeIgniter: A Comprehensive Comparison for Web Development
Favicon
Introducing PHP Env Manager: Simplify Environment Management in PHP Applications
Favicon
Top 10 CodeIgniter Plugins and Libraries to Enhance Your Project
Favicon
Optimizing Performance in CodeIgniter: Tips and Best Practices
Favicon
Advantages Of Hiring A Codeigniter Developer And Key Considerations
Favicon
Top 3 PHP Frameworks: Speed, Response Time, and Efficiency Compared
Favicon
Top CodeIgniter Interview Questions and Answers for Freshers
Favicon
What are the key server requirements necessary for migrating a CodeIgniter 3 project to CodeIgniter 4?
Favicon
Why does PHP 5.2.4 lack the necessary elements to support CodeIgniter 4 & PHP compatibility for CodeIgniter 4?
Favicon
CodeIgniter Vs Laravel: Comprehensive Analysis to Find the Best PHP Framework
Favicon
CodeIgniter 4 Create Custom Validation Rule Tutorial
Favicon
CodeIgniter vs Laravel : Introduction To PHP Frameworks
Favicon
Which one is best calling model from controller or calling from views in ci4?
Favicon
Please Help Solve this Codeigniter Project Errors ...
Favicon
Introduction To CodeIgniter 4 | CodeIgniter4
Favicon
File Upload from Angular 14 to CodeIgniter Normal API Help me......
Favicon
Best PHP Frameworks in 2022
Favicon
How to Generate Pdf in PHP CodeIgniter
Favicon
Why does a PHP Developer need to use codeigniter framework?
Favicon
Membuat Grafik Dari Database Codeigniter 4
Favicon
Membuat Custom Helper Codeigniter 4
Favicon
Membuat Thumbnail Gambar Dengan Codeigniter 4
Favicon
Which PHP Framework Is Right for You? CodeIgniter vs. CakePHP vs. Laravel vs. Yii
Favicon
Menggunakan Filters Di Codeigniter 4 Sebagai Middleware
Favicon
Recommend books
Favicon
Why Should You Choose Develop website Using CodeIgniter and PHP
Favicon
Top Reasons to Opt for CodeIgniter Web Framework
Favicon
PHP CodeIgniter framework - Features that every developer should know

Featured ones: