Search code examples
phpcodeigniterdompdf

Saving DOMPDF to specified directory and then sending as email


Hi I am trying to save dompdf to a specific directory in codeigniter file and then taking the saved file and sending it as a email. I am using codeigniter to do all this work. Here is the function.

public function generateCreditAggreement(){
        $this->load->helper('file'); 
        require_once(APPPATH.'third_party/dompdf/dompdf_config.inc.php');

        $pdfroot  = dirname(dirname(__FILE__));
        $pdfroot .= '/third_party/pdf/';

        $dompdf = new Dompdf();


        $msg = $this->load->view('credit_agreement_view', '', true);
        $html = mb_convert_encoding($msg, 'HTML-ENTITIES', 'UTF-8');
        $dompdf->load_html($html);

        $paper_orientation = 'Potrait'; 
        $dompdf->set_paper($paper_orientation);

            // Render the HTML as PDF
        $dompdf->render();

            // Output the generated PDF to Download folder

//          $dompdf->stream('document.pdf');

            //save the pdf file on the server
            $pdf_string =   $dompdf->output();
            file_put_contents($pdfroot, $pdf_string ); 



    }

The error I am getting is that Message: file_put_contents(C:\Apache24\htdocs\faceloan\faceloan\application/third_party/pdf/): failed to open stream: No such file or directory But the directory does exist , and the path name is right.


Solution

  • The path .../third_party/pdf/ is probably a directory and not a file. PHP can't save your data as a directory. Specify a filename as first parameter of file_put_contents(), e.g. third_party/pdf/my_document.pdf.