Welcome Guest, Not a member yet? Register   Sign In
CI4 Dynamic DOMPDF view ,
#1

Hey All , 

I am getting a blank page/error when I try to load the dynamic data on the dompdf. If i write any PHP code inside it is showing an error . Please refer the code i written ,
       #viewpage data
        $html =  view($this->path_views . 'contract', $data);
        $this->dompdf->loadHtml($html);
        $this->dompdf->setPaper('A4','portrait');
        $this->dompdf->set_option('defaultMediaType', 'all');
        $this->dompdf->set_option('isFontSubsettingEnabled', true);
        $this->dompdf->set_option('isPhpEnabled', true);

        $this->dompdf->render();
        $this->dompdf->stream( $html, array("Attachment" => false)); 

Please help me to resolve this issue , how i can show dynamic pdf view in CI4 using dompdf. 
I really appreciate any help you can provide.
Reply
#2

This is an example of code that works for me:
Code:
$dompdf = new \Dompdf\Dompdf();
$dompdf->loadHtml(view('Modules\Views\Admin\pdf_view'));
$dompdf->setPaper('A4', 'landscape');
$dompdf->set_option('defaultMediaType', 'all');
$dompdf->set_option('isFontSubsettingEnabled', true);
$dompdf->set_option('isPhpEnabled', true);
$dompdf->render();
$dompdf->stream();
Reply
#3

Hi,
Thanks for your message , 
The static page is loaded for me, When I pass "$data" it is returning an error or the blank page.  The issue am facing is dynamic values loading on the view page. 
I really appreciate any help you can provide
Thank you,
Reply
#4

Try something simple like:
Code:
$data['testMessage'] = 'Testing domPDF with PHP code';
$dompdf = new \Dompdf\Dompdf();
$dompdf->loadHtml(view('Modules\Views\Admin\pdf_view', $data ));
$dompdf->setPaper('A4', 'landscape');
$dompdf->set_option('defaultMediaType', 'all');
$dompdf->set_option('isFontSubsettingEnabled', true);
$dompdf->set_option('isPhpEnabled', true);
$dompdf->render();
$dompdf->stream();

And a simple view:

Code:
<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Codeigniter 4 PDF Example - positronx.io</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
</head>

<body>
<div class="container mt-5">

    <h2>Generate PDF in Codeigniter from View</h2>

    <?php echo $testMessage;?>

</div>
</body>

</html>

Does your view load correctly on screen?
Reply
#5

Hi , 

Really appreciate for your help ,
My issue is resolved. 
Thank you,
Reply
#6
Question 

Hello and sorry for activating this old thread again... but I am running in a problem with CI 4.4.3 and Dompdf. I've created a view as proposed here, it works fine when I view it. But as soon as I send the view to DomPDF I get an (seemingly) empty pdf in browser. When I download it and open it with a text editor it contains the CI debug page with the error

Code:
ini_set(): Session ini settings cannot be changed after headers have already been sent

I've installed shield, tried to remove it from loaded filters already, but nothing helped yet.
Does anyone has an idea what's wrong?
Reply
#7

The error message tells you what's wrong. You have already sent a header before the ini_set() is called.
Reply
#8

@trunky This has nothing to do with Shield.

The code below should work fine.
PHP Code:
        
          $html 
= <<<EOD
            <!DOCTYPE html>
            <html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                <title>Codeigniter 4.4.3</title>
            </head>
            <body>
                <div style="margin: 0 auto;display: block;width: 500px;">
                    <table width="100%" border="1">
                        <tr>
                            <td>Name:</td>
                            <td><?= 'name' ?></td>
                        </tr>
                    </table>
                </div>
            </body>
            </html>
        EOD;

        
$dompdf = new \Dompdf\Dompdf();
        
$dompdf->loadHtml($html);
        
$dompdf->render();
        
$dompdf->stream('ci443.pdf', ['Attachment' => false]); 

But on the condition that you set mode CI_ENVIRONMENT = production. Otherwise, you will receive a white file (Error
Failed to load PDF document.).


@kenjis Dompdf is one of the popular and widely used libraries. Please check if you can why there is a problem in mode development.
Reply
#9

(This post was last modified: 11-29-2023, 02:53 PM by kenjis.)

@datamweb I cannot reproduce any error with "CI_ENVIRONMENT = development"

Code:
$ composer require dompdf/dompdf

PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{
    public function index()
    {
        $html = <<<'EOD'
                <!DOCTYPE html>
                <html>
                <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                    <title>Codeigniter 4.4.3</title>
                </head>
                <body>
                    <div style="margin: 0 auto;display: block;width: 500px;">
                        <table width="100%" border="1">
                            <tr>
                                <td>Name:</td>
                                <td><?= 'name' ?></td>
                            </tr>
                        </table>
                    </div>
                </body>
                </html>
            EOD;

        $dompdf = new \Dompdf\Dompdf();
        $dompdf->loadHtml($html);
        $dompdf->render();
        $dompdf->stream('ci443.pdf', ['Attachment' => false]);
    }

Reply
#10

@kenjis, can you upload the file(ci443.pdf) generated by the above code in mode development here?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB