Seems like a similar question has been asked before but none solves the issue that I am facing.
I have a view called job_invoice which is loading properly in browser in html format. In that html page, there is a button called Send invoice, when you click on that button, it calls the below function sitting in controller file (through the routes.php). Then a pdf file is being generated and emailed to the email address but in very distored format. Looks like css is not loading properly.
public function send_invoice_email($id)
{
$data[ 'id' ] = $id;
$data[ 'model_obj' ] = $this->User_model;
$html = $this->load->view( 'adminpages/print_invoice', $data , true);
$this->load->library('pdf');
$pdf = new Dompdf\Dompdf(array('enable_remote' => true));
$pdf->load_html($html);
$pdf->render();
$output = $pdf->output();-
$pdfFilePath = FCPATH . "invoices/invoice" . $id . ".pdf";
file_put_contents($pdfFilePath, $output);
$this->load->library('email');
$job = $this->User_model->getdata(array('id'=>$id),'jobs')->row_array();
$emailbody = "Hi <br><br>Please see attached the invoice";
$this->email->set_newline("\r\n");
$this->email->from('admin@gmail.com', 'Admin');
$this->email->to($job['user_email']);
$this->email->subject('Invoice');
$this->email->message($emailbody);
$this->email->attach($pdfFilePath);
$this->email->send();
$this->email->clear($pdfFilePath);
$this->session->set_flashdata( 'success', 'Invoice Email send successfully' );
unlink($pdfFilePath);
redirect(base_url('invoice/').$id);
}
dompdf library structure is ../application/libraries/vendor/dompdf/dompdf
In the library folder, there is Pdf.pdf file as well
with this code
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter PDF Library
*
* Generate PDF's in your CodeIgniter applications.
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author Chris Harvey
* @license MIT License
* @link https://github.com/chrisnharvey/CodeIgniter-PDF-Generator-Library
*/
require_once(dirname(__FILE__) . '/vendor/autoload.php');
use Dompdf\Dompdf;
class Pdf extends Dompdf
{
/**
* Get an instance of CodeIgniter
*
* @access protected
* @return void
*/
protected function ci()
{
return get_instance();
}
/**
* Load a CodeIgniter view into domPDF
*
* @access public
* @param string $view The view to load
* @param array $data The view data
* @return void
*/
public function load_view($view, $data = array())
{
$html = $this->ci()->load->view($view, $data, TRUE);
$this->load_html($html);
}
}