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!
#12

[eluser]soyut[/eluser]
Thanks. I will try as soon as i got home, in an hour
#13

[eluser]soyut[/eluser]
Hi, Happy News!!

OK, Now Id like to explain where have I done mistakes.

I have used mpdf library.

I had 2 functions in controller function reporting(), and function topdf()
In my search page after i choose the options i simply click the submit button and that passes these form values to function reporting(), and in this function i call my model, in the model queries run, ....etc and finally results returned to the same function as an array. I use this array when i load the view function and Everything works fine, i get nice reporting page.

Here is my mistake;

I put a link in this page so that when i click it i can print it out as a pdf file. However in the anchor I called another function, function topdf(). Obviously im not passing any values to this function. Thats why i cant see the variables in pdf file.

I copied all the content in the function topdf() into function reporting(), and when i click submit button i get pdf file automatically with the variables i need it.

at this moment Im happy as I understood my mistakes. Now I need to find a way to pass any values from foreach function to controller function using anchor.



Thanks for your help.
#14

[eluser]soyut[/eluser]
forgot to add the function, sorry

Code:
public function report(){
        
      if ($this->input->post('submit'))
          {
            if($this->input->post('date1')){
                if($this->input->post('date2')){
          $data['reports'] = $this->blog_model2->reporting();
          $reports =array();    
     //below the code i have copied from function topdf()
         $html = $this->load->view('report_view2', $data, TRUE);
    
        $mpdf=new mPDF();
            
        $mpdf=new mPDF('c','A4','','',32,25,27,25,16,13);
    
    
    
        $mpdf->WriteHTML($html,2);
    
        $mpdf->WriteHTML($this->output->get_output(),2);
        $mpdf->Output('mpdf.pdf','I');
    
        }else{
            
            
            echo "Please enter a valid end date!";
        }
            
       }
       else{
            echo "Please enter a valid start date!";
        }
        
        
        } //submit
        else {
        
        $this->load->view('header_view');
        $this->load->view('nav_view');
        $this->load->view('report_view');
        $this->load->view('footer_view');
        
        } //report_view
    } //report




Theme © iAndrew 2016 - Forum software by © MyBB