Reputation: 331
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
Upvotes: 4
Views: 8737
Reputation: 76859
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.
Upvotes: 0
Reputation: 924
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.
Upvotes: 0
Reputation: 1446
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]);
Upvotes: 4
Reputation: 8007
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();
}
Upvotes: 2