I'm currently utilizing CodeIgniter 3 and dompdf to convert an HTML view into a PDF. While I am able to successfully convert the HTML to PDF, the proper styling is not being applied. All necessary CSS files have been included as custom design in the view file.
What else do I need to do to properly include the CSS styles and images?
Here is the controller:
public function get_pdf_test($id)
{
// print_r($_REQUEST);
// die;
$data['incidents'] = $this->incidents_model->getById($id);
$data['incidents_history'] = $this->incidents_model->getById_history($id);
$data['company_name'] = $this->incidents_model->getAllCompanyName();
generate_pdf("admin/incidents/incidentsView.pdf", "admin/incidents/incidentsView.php", $data);
}
incident_model.php
function getById($id)
{
return $this->db->get_where('incidents',array('id'=>$id))->row();
}
function getById_history($id)
{
$query=$this->db->query("SELECT incidents_id FROM incidents WHERE id = $id");
$get_row = $query->row();
$incident_id = $get_row->incidents_id;
$this->db->select('*');
$this->db->where(array('incidents.incidents_id'=>$incident_id));
$this->db->join('incident_status', 'incidents.id = incident_status.incident_id');
return $this->db->get('incidents')->result();
}
function getAllCompanyName()
{
$query = $this->db->get('company_details');
$query = $this->db->query('SELECT company_name FROM company_details where delete_flag =0');
return $query->result();
}
Pdf.php file in library:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
*
* Convert HTML to PDF in CodeIgniter applications.
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author Hostmystory
* @link https://www.hostmystory.com
*/
// Dompdf namespace
use Dompdf\Dompdf;
class Pdf
{
public function __construct(){
// require_once autoloader
require_once dirname(__FILE__).'/dompdf/autoload.inc.php';
$pdf = new DOMPDF();
$CI =& get_instance();
$CI->dompdf = $pdf;
}
}
?>
new_helper.php in helper:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
// generate pdf
function generate_pdf($name, $tpl, $data)
{
$ci = &get_instance();
$data['data'] = $data;
$ci->load->view($tpl, $data);
// Get output html
$html = $ci->output->get_output();
// add external css library
$html .= '<link href="' . base_url() . 'assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">';
// Load pdf library
$ci->load->library('pdf');
$ci->dompdf->loadHtml($html);
// setup size
$ci->dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$ci->dompdf->render();
// Output PDF (1 = download and 0 = preview)
$ci->dompdf->stream($name, array("Attachment" => 0));
}
?>
here is the output of dompdf :