4

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

Roxana Slj
  • 311
  • 1
  • 5
  • 23

4 Answers4

4

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]);
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
Abkarino
  • 1,426
  • 1
  • 12
  • 19
  • Amm it seems that this is a complete clone of Vickel's answer... it would be nice if you had another solution, but a exact copy of an older answer wouldn't be so nice... – U13-Forward Jan 18 '21 at 00:37
  • @U11-Forward Vickel's answer didn't go through why issue and went straight into a proposed solution. I tried to cover the reason of the issue and how to solve it. Also if you check the chat, his problem wasn't over. – Abkarino Jan 18 '21 at 00:55
  • Also why did you edit a triple quote block with space padded block? You only did that in the first one and frankly I see no difference, so if I am missing something, it would be great to let me understand. – Abkarino Jan 18 '21 at 00:57
  • I have used : require_once APPPATH.'vendor/autoload.php'; and now getting error :Temporary files directory "/opt/lampp/htdocs/ticketing_tool_v2/application/vendor/mpdf/mpdf/src/Config/../../tmp/mpdf" is not writable – Roxana Slj Jan 18 '21 at 03:55
  • I have tried to make the file writable in FTP but can not find this specific file – Roxana Slj Jan 18 '21 at 03:55
  • @RoxanaSlj you need to make a folder writable. You can use the the last code segment to create a writable one. Just change the `$path` variable to any path that you prefer. – Abkarino Jan 18 '21 at 09:51
  • yes already did that , I have edit the question please have a look – Roxana Slj Jan 18 '21 at 10:12
  • @RoxanaSlj the last error is logical error, you try to access `$incidents->incidents_id` in your view but you used `->result_array();` which returns array and not object. – Abkarino Jan 18 '21 at 10:31
2

Make sure your composer/manual mpdf installation is located in the directory

application\vendor

set in your config\config.php

$config['composer_autoload'] = true; 

or use in your controller:

require_once (APPPATH. 'vendor/autoload.php');

Depending on the version of mpdf installed, you can use namespaces (V.7+) like:

$mpdf = new \Mpdf\Mpdf();

Other versions (V.6.x) use:

$mpdf = new mPDF();

so just change your function like this:

function DownloadPdf()
{
    //require_once (APPPATH. 'vendor/autoload.php'); //-> you don't need this line if set in config.php
    $mpdf = new mPDF();
    $mpdf->WriteHTML('<h1>Hello world!</h1>');
    $mpdf->Output();    
}

issue documented

Vickel
  • 7,879
  • 6
  • 35
  • 56
0

Just run composer dump-autoload in order to update the auto-loader... then \Mpdf\Mpdf should become known. If that shouldn't work out, check & fix the file-systems permissions and try again (while vendor/autoload.php cannot be written, it obviously cannot add it into the auto-loader).

For XAMPP on Linux, ownership should likely be (there's no 777 required, nor recommended):

chown -R daemon:daemon /opt/lampp/htdocs

And by adding another user into group daemon, they'll share the group's permissions.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • error:Temporary files directory "/opt/lampp/htdocs/ticketing_tool_v2/application/vendor/mpdf/mpdf/src/Config/../../tmp/mpdf" is not writable how can I make it writable ? I have used 777 in ftp but still same – Roxana Slj Jan 18 '21 at 04:02
  • Use SSH2 instead of FTP (with a user that is allowed to write there). `chmod 777` is always a bad idea; rather `chown -R user:group /opt/lampp/htdocs`. On Linux, using such LAMPP distribution is also kind of pretty useless, because one can install all of that with the package-manager; out-of-the-box. But based upon the error message, one can assume that my guess was correct. – Martin Zeitler Jan 18 '21 at 14:17
  • If you're able to run `composer dump-autoload` offline and then upload the resulting files via FTP, it also may work out. I'm not exactly certain, since I do not use any LAMPP ...but I think user & group are called `daemon` there (the config file should provide certainty). – Martin Zeitler Jan 18 '21 at 14:28
0

From the screenshot, the Issue doesn't seem to be the mPDF, it seems to be the data. Error 1: You have undefined incidents, this is causing the issue to create the PDF.

Error 2: You are passing an Object, however, mPDF requires you to pass an Array. You will need to convert the Model Return data using asArray or simply using return type of array for the model.

Reference: https://codeigniter4.github.io/userguide/models/model.html

Also, you don't need to explicitly define the autoload in your controller. Another thing to notice from the screenshot is it is creating the PDF, however, because the errors are throw, you can't see the PDF properly.

UPDATE: Potential Issue maybe that data is not passed to the View at all. Following code must be passing empty data

 $html = $this->load->view('admin/incidents/incidentsPdf',[],true);

Here [] is passing the empty data, so the View may not be receiving any data even though it is set in the controller.

Dhaval Chheda
  • 844
  • 1
  • 8
  • 15