I want to create a pdf based on one view, generated by Codeigniter. However, this view has a lot of styled elements, and media queries associated with them, so the rendered pdf doesn’t correspond at all, with the desired final document.
What’s the best way to acomplish this? Right now I’m using mPDF. Is there anyway to take a snapshot of the view and then generate a pdf with it, so the associated styles render correctly on the document?
Right now my code looks like this
public function generatePDF()
{
$this->session_expire();
$this->load->helper('url');
$user_id=$this->session->userdata('user_id');
$this->user_model=$this->db_interface->db_get_user($user_id);
// $this->load->helper('url');
//echo('error aki <br />');
$data['user'] = $this->user_model;
$data['objective'] = $this->db_interface->db_get_objectives($this->user_model->get_id());
$data['list_work'] = $this->db_interface->db_get_list_work_experience($this->user_model->get_id());
$data['list_education'] = $this->db_interface->db_get_list_education($this->user_model->get_id());
$data['list_skills'] = $this->db_interface->db_get_skills($this->user_model->get_id());
$data['list_personal'] = $this->db_interface->db_get_personal_skills($this->user_model->get_id());
$data['list_links'] = $this->db_interface->get_user_links($this->user_model->get_id());
$html = $this->load->view('display_user',$data,TRUE);
//$html = $this->load->view('display_user',$data,TRUE);
// $this->load->view('display_user', $html);
$this->mpdf->WriteHTML($html);
$this->mpdf->Output();
}
Thanks in advance
Short answer: kind of.
Long answer: you have 2 options. You can a) take a screenshot (See Website screenshots using PHP ); or b) you can create a custom stylesheet for print.
The latter will likely be much easier to do. There are limits on what the library you are using can do insofar as CSS is concerned, so read up: http://www.mpdf1.com/mpdf/enhancements
Best bet is probably to create separate stylesheets for screen and printing:
for viewing, and
for printing.
Style
print.cssto look good. It is highly unlikely you will be able to mimic the screen exactly, but it should be easy enough to present the information you need to print coherently.