PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 16, 2022

[FIXED] How to load html file inside cakephp using dompdf

 January 16, 2022     cakephp, cakephp-3.0, dompdf, pdf-generation, php     No comments   

Issue

I am using dompdf with cakephp 3.6 to generate prints from HTML to PDF

I have a function name test inside drivercontroller and i want to load a ctp file which is created inside src\Template\DriverDetails and filename is testpdf.ctp

Can u plz help me to suggest how to pass complete file.

Below is my code

public function test()  {
            $html = file_get_contents("testpdf.ctp");            
            $dompdf = new Dompdf();
            $dompdf->loadHtml($html);
            $dompdf->setPaper('A4', 'portrait');
            $dompdf->render();
            $dompdf->stream("invoice.pdf", array("Attachment" =>0));
            exit(0);
        }

src\Template\DriverDetails\testpdf.ctp

<html>
    <body>
        <table>
            <tr><td align="center"><?php echo $id; ?></td></tr>
            <tr><td align="center">Demo Data</td></tr>
        </table>
    </body>
</html>

Solution

You can try like that way.

public function test()
{
    $data = "This can be accessible in view file";
    $builder = $this->viewBuilder();
    $builder->autoLayout(false);

    // set your template path [don't use .ctp]
    $builder->template('Users/add');

    //Pass data into view file
    $view = $builder->build(['data' => $data]);

    //Render view file content and store into variable
    $viewContent = $view->render();
    var_dump($viewContent);
}


Answered By - Sohel Rana
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing