How to Generate Pdf in PHP CodeIgniter
Tags: codeigniter
Published: 2023-03-28 07:01:01

---
title: How to Generate Pdf in PHP CodeIgniter
published: true
date: 2022-10-01 14:53:26 UTC
tags: CodeIgniter,codeIgniter
canonical_url: https://larachamp.com/how-to-generate-pdf-in-php-codeigniter/
---

[![How to Generate Pdf in PHP CodeIgniter](https://larachamp.com/wp-content/uploads/2022/10/Yellow-Black-Earn-Money-YouTube-Thumbnail.png)](https://larachamp.com/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
```

## 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>
```

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

![](https://larachamp.com/wp-content/uploads/2022/10/Export-PDF-1024x320.png)
_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']);
```

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

```
php spark serve
```

## Install the [DOMPDF Package](https://github.com/dompdf/dompdf)

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.

Read more about CodeIgniter here: [CodeIgniter4](https://larachamp.com/category/codeigniter/)

## 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](https://larachamp.com/how-to-generate-pdf-in-php-codeigniter/) appeared first on [Larachamp](https://larachamp.com).