2

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

João Dias
  • 1,078
  • 1
  • 15
  • 38

1 Answers1

3

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:

<link rel="stylesheet" href="screen.css" type="text/css" media="screen">

for viewing, and

<link rel="stylesheet" href="print.css" type="text/css" media="print">

for printing.

Style print.css to 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.

Community
  • 1
  • 1
stormdrain
  • 7,915
  • 4
  • 37
  • 76
  • Made some progress with the html2canvas, however it's still misses some elements on the convertion. How about converting it to PostScript first? Is this accomplishable? Does it even makes sense?? I'm considering other alternatives like Latex. – João Dias Nov 06 '12 at 21:52
  • Convert what to postscript? If you have a screenshot with missing elements, converting it to PS won't make them appear. Latex is a whole other ball of worms: http://stackoverflow.com/q/8577244/183254 but I suppose it might work. Seems like a ton of work... Maybe worth trying to hack html2canvas to get those last elements working. – stormdrain Nov 06 '12 at 22:05
  • I was talking about converting the HTML to PS. I don't know if that's possible. Anyway, you gave me the best answer I could have, so I think. Thank you! – João Dias Nov 06 '12 at 22:50