PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 19, 2022

[FIXED] Export & download html to pdf in codeigniter 3 using mPDF erro:Class 'Mpdf\Mpdf' not found

 February 19, 2022     codeigniter, mpdf, php     No comments   

Issue

I am trying to export and download the html view incidentView.php to pdf by pressing print button in incidentView.php, using mPDF.I am able to convert to pdf now even though the loading is very slow,but now I am getting error regarding the data that I am trying to get on pdf.Basically I can not convert the Dynamic data can you please help me how I should get them?

I have download mPDF by using:

composer require mpdf/mpdf

config.php

$config['composer_autoload'] = TRUE;

Incident.php Controller

function get_pdf_test($id)
  {
    $data['incidents'] = $this->incidents_model->getByIdPdf($id);
    $data['incidents_history'] = $this->incidents_model->getById_historyPdf($id); 
    $data['company_name'] = $this->incidents_model->getAllCompanyNamePdf();
    require_once (APPPATH. 'vendor/autoload.php');
    $path = '/tmp/mpdf'; 
     if (!file_exists($path)) {
    mkdir($path, 0777, true);
     }
     $mpdf = new \Mpdf\Mpdf(['tempDir' => $path]);
     $html = $this->load->view('admin/incidents/incidentsPdf',[],true);
     $mpdf->WriteHTML($html);
     $mpdf->Output(); 
     $mpdf->Output('incidentsPdf.pdf','D'); 

}

incidentPdf.php view

<div class="TaskView" > 
    <h2 class="heading" ><?php echo "ASGB_IN".str_pad($incidents->incidents_id, '4', '0', STR_PAD_LEFT);?></h2>
      <!-- <form action="<?php echo base_url('get_pdf_test/'.$incidents->id);?>" method="post" >       -->
    <div class="row">
           <div class="form-group col-md-4">
              <label for="email">Incidents ID</label>
              <input type="text" class="form-control" id="T_id" placeholder="Name" name="T_id" value="<?php echo "ASGB_IN".str_pad($incidents->incidents_id, '4', '0', STR_PAD_LEFT);?>" readonly>
            </div>
           <div class="form-group col-md-4">
              <label for="email">Incidents Name</label>
              <input type="text" class="form-control" id="Tname" placeholder="Name" name="Tname" value="<?php echo $incidents->incident_name;?>" readonly>
            </div>
            <div class="form-group col-md-4">
              <label for="pwd">Company Name</label>
              <input  type="text"class="form-control" id="Cname" name="Cname" value="<?php echo $incidents->company_name;?>"readonly>
            </div>
            <div class="form-group col-md-4">
              <label for="pwd">Project Name</label>
              <input type="text" class="form-control" id="Pname" name="Pname" value="<?php echo $incidents->project_name;?>" readonly>
            </div>

incident_model.php

 function getById_history_pdf($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_array(); 
} 


function getAllCompanyNamePdf()
{
  $query = $this->db->get('company_details');
  $query = $this->db->query('SELECT company_name FROM company_details where delete_flag =0');
  return $query->result_array();
}

function getByIdPdf($id) 
{
   return $this->db->get_where('incidents',array('id'=>$id))->row_array();
}

vendor location

enter image description here

pdf enter image description here


Solution

You are using

require_once __DIR__ . '/vendor/autoload.php';

inside your controller file. __DIR__ evaluates to the directory of the file that used it. Since this is a controller file, you are trying to load

/opt/lampp/htdocs/ticketing_tool_v2/application/controllers/admin/vendor/autoload.php

as __DIR__ evaluated to /opt/lampp/htdocs/ticketing_tool_v2/application/controllers/admin which is not the correct path.

A correct code would be

require_once __DIR__ . '/../../vendor/autoload.php';

A better solution is to use APPPATH so you wouldn't have to know the relative path.

require_once APPPATH.'vendor/autoload.php';

You can also

$config['composer_autoload'] = true; 

You can find more info here.

Lastly, in the GitHb readme.md you can see this.

It is recommended to set one's own temporary directory via tempDir configuration variable. The directory must have write permissions (mode 775 is recommended) for users using mPDF (typically cli, webserver, fpm).

This means that you need a writable folder for temporary operations. You can initialize the temp location to any place you prefer using

$mpdf = new \Mpdf\Mpdf(['tempDir' => '/tmp']);

/tmp is always writable. However, you can make any folder and path the path for it as a parameter like

$path = '/tmp/mpdf'; // You should change this as prefered.
if (!file_exists($path)) {
    mkdir($path, 0777, true);
}
$mpdf = new \Mpdf\Mpdf(['tempDir' => $path]);


Answered By - Abkarino
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing