Welcome Guest, Not a member yet? Register   Sign In
mpdf with dynamic data
#11

[eluser]Brad K Morse[/eluser]
I used this library for domPDF in CI https://github.com/iamfiscus/Codeigniter-DOMPDF

Code:
// controller

function send_pdf($id)
{
  $this->load->model('student_model');
  $data['student'] = $this->student_model->getStudentInfo($id);

  $html = $this->load->view('pdf/document-view', $data, true);

  $pdf = $this->generate_pdf($html);

  if($pdf)
  {
     // another function that uses the CI email library and attaches the pdf, no need to include it here - you understand that part
     $this->send_pdf_email($pdf);
  }

}

private function generate_pdf($html) {
  $this->load->helper('file');
  $this->load->helper('dompdf');
  
  $file_name = rand(5, 15000);

  $pdf = pdf_create($html, '', false);

  $full_path = "/path/to/upload_directory/".$file_name.".pdf";

  if(!write_file($full_path, $pdf))
    return false;

  return $full_path;
}

// view (pdf/document-view)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;&lt;?=$student->first_name?&gt; &lt;?=$student->last_name?&gt;&lt;/title&gt;

&lt;/head&gt;
&lt;body&gt;

<div class="container_12 header">

<h1>Page Title</h1>

</div>

<div class="container_12 content">

<div class="split">
  <p><span>Current Name:</span> &lt;?=$student->first_name?&gt; &lt;?=$student->last_name?&gt;</p>
</div>

&lt;/body&gt;
&lt;/html&gt;

You need to use inline styles in the view, I removed them in my example I pasted here, so it is easier to read.

I have the private function generate_pdf to take the output send_pdf function creates, it also generates a filename for the pdf and writes it to the designated upload directory.

Hope this helps!


Messages In This Thread
mpdf with dynamic data - by El Forum - 10-17-2012, 09:42 AM
mpdf with dynamic data - by El Forum - 10-19-2012, 12:26 PM
mpdf with dynamic data - by El Forum - 10-19-2012, 01:28 PM
mpdf with dynamic data - by El Forum - 10-19-2012, 01:34 PM
mpdf with dynamic data - by El Forum - 10-19-2012, 07:50 PM
mpdf with dynamic data - by El Forum - 10-19-2012, 10:03 PM
mpdf with dynamic data - by El Forum - 10-20-2012, 04:59 PM
mpdf with dynamic data - by El Forum - 10-21-2012, 09:11 PM
mpdf with dynamic data - by El Forum - 10-22-2012, 06:48 AM
mpdf with dynamic data - by El Forum - 10-22-2012, 10:20 AM
mpdf with dynamic data - by El Forum - 10-22-2012, 10:38 AM
mpdf with dynamic data - by El Forum - 10-22-2012, 10:55 AM
mpdf with dynamic data - by El Forum - 10-22-2012, 05:14 PM
mpdf with dynamic data - by El Forum - 10-22-2012, 05:15 PM



Theme © iAndrew 2016 - Forum software by © MyBB