Codeigniter mpdf error undefined variable when parsing parameter to pdf file - codeigniter

I have created pdf file using mpdf in codeigniter. Next, I want to parsing a parameter from controller to view, so in pdf file there's have a title text and image. I just successfully parsing image to pdf, but I'm confused how to parsing text like title parameter.
Here's the controller code
class Createpdf extends MY_Controller
{
function index()
{
$image= $this->input->get('image_link');
$data = "Title Page";
$mpdf = new \Mpdf\Mpdf();
$html = $this->load->view('pages/pdfreport',$data,true);
$mpdf->WriteHTML($html);
$mpdf->Image($image, 0, 0, 21, 29, 'jpg', '', true);
$mpdf->Output();
}
}
Here's the view code
<center><h3> <?= $data; ?></h3></center>
In the pdf file, it shows error Undefined variable: data. Do you know how to fixed this ?
Thank you

class Createpdf extends MY_Controller
{
function index()
{
$image= $this->input->get('image_link');
$data = "Title Page";
$mpdf = new \Mpdf\Mpdf();
$html = $this->load->view('pages/pdfreport',array("data" => $data),true);
$mpdf->WriteHTML($html);
$mpdf->Image($image, 0, 0, 21, 29, 'jpg', '', true);
$mpdf->Output();
}
}

class Createpdf extends MY_Controller
{
function index()
{
$image= $this->input->get('image_link');
$data = ["data" => "Title Page"]; // <-- see here
$mpdf = new \Mpdf\Mpdf();
$html = $this->load->view('pages/pdfreport',$data,true);
$mpdf->WriteHTML($html);
$mpdf->Image($image, 0, 0, 21, 29, 'jpg', '', true);
$mpdf->Output();
}
}

Related

laravel pdf to view add watermark using( Barryvdh\DomPDF)

my problem(not show pdf contents when this url(127.0.0.1:8081/php_tutorial.pdf))
Controller
public function generatePDF()
{
$data = [
'title' => 'Welcome to ',
'pdf' => '127.0.0.1:8081/php_tutorial.pdf',
'date' => date('m/d/Y')
];
$pdf = PDF::loadView('pdf',$data);
$pdf->setPaper('L');
$pdf->output();
$canvas = $pdf->getDomPDF()->getCanvas();
$height = $canvas->get_height();
$width = $canvas->get_width();
$canvas->set_opacity(.2,"Multiply");
$canvas->page_text($width/5, $height/2, 'This Is Watermark text', null,
70, array(0,0,0),2,2,-30);
return $pdf->stream('result.pdf');
}
view (blade)
{{ $title }}
<iframe src="{{ $pdf }}" width="100%" height="500px">
Firstly You need to create view to load all your PDF Views.
Then in your controller query your data
public function PDFSlip($id)
$data = ModelName::get()
$customPaper = array(0,0,600.00,310.80);
$pdf = PDF::loadView('pdf\bookingDetailPDF', compact('data'))-
>setPaper($customPaper, 'portrait');;
return $pdf->download('PDFNAME.pdf');
}
Then your file will be downloaded in the local directory and also pdf is loaded into the new tab.

Try to use the codeigniter's file upload library as a general function from Helpers

Can anybody help as I am trying to use the codeigniter's upload library from the helpers folder but I keep getting the same error that I am not selecting an image to upload? Has any body tried this before?
class FileUpload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'file_uploading'));
$this->load->library('form_validation', 'upload');
}
public function index() {
$data = array('title' => 'File Upload');
$this->load->view('fileupload', $data);
}
public function doUpload() {
$submit = $this->input->post('submit');
if ( ! isset($submit)) {
echo "Form not submitted correctly";
} else { // Call the helper
if (isset($_FILES['image']['name'])) {
$result = doUpload($_FILES['image']);
if ($result) {
var_dump($result);
} else {
var_dump($result);
}
}
}
}
}
The Helper Function
<?php
function doUpload($param) {
$CI = &get_instance();
$CI->load->library('upload');
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|png|jpg|jpeg|png';
$config['file_name'] = date('YmdHms' . '_' . rand(1, 999999));
$CI->upload->initialize($config);
if ($CI->upload->do_upload($param['name'])) {
$uploaded = $CI->upload->data();
return $uploaded;
} else {
$uploaded = array('error' => $CI->upload->display_errors());
return $uploaded;
}
}
There are some minor mistakes in your code, please fix it as below,
$result = doUpload($_FILES['image']);
here you should pass the form field name, as per your code image is the name of file input.
so your code should be like
$result = doUpload('image');
then, inside the function doUpload you should update the code
from
$CI->upload->do_upload($param['name'])
to
$CI->upload->do_upload($param)
because Name of the form field should be pass to the do_upload function to make successful file upload.
NOTE
Make sure you added the enctype="multipart/form-data" in the form
element

Getting blank pages in laravel while using dom-pdf

I am using laravel 4.2 with dompdf.
My controller:
public function getLedgerReport() {
$pdf = PDF::loadView('sales.ledger_report')->setPaper('a4');
$pdf->stream();
}
My view:
<?php
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
$html = '';
$html .= '<html><body>hi</body></html>';
echo $html;
?>
The code which gives a 'hi' in the Pdf file.
public function getLedgerReport() {
$pdf = PDF::loadView('sales.ledger_report')->setPaper('a4');
$pdf->stream();
$invoice_path = 'downloads/invoices/invoice.pdf';
$pdf->save($invoice_path);
}
I get a blank page instead of getting a 'hi' text.
But when I save the same as a pdf file I do see a 'hi' in the file.

how to get data from database throught model using codeigniter with for each loop

http error occured while calling data from model using function
model
public function getProductCombo() {
$q = $this->db->get_where('products', array('type' => 'combo'));
if ($q->num_rows() > 0) {
foreach (($q->result()) as $row) {
$data[] = $row;
}
return $data;
}
}
controller
function sets() {
$this->sma->checkPermissions();
$this->load->helper('security');
$this->data['error'] = (validation_errors() ? validation_errors() :
$this->session->flashdata('error'));
// problem in this line also
$this->data['showcombo'] = $this->load->sales_model->getComboProduct();
$bc = array(array('link' => base_url(),
'page' => lang('home')),
array('link' => site_url('sales'),
'page' => lang('products')),
array('link' => '#', 'page' => "sets")
);
$meta = array('page_title' => "Add Sets", 'bc' => $bc);
$this->page_construct('sales/sets', $meta, $this->data);
}
First of all, No need to include the curly braces for $q->result
foreach ($q->result as $row)
{
$data[] = $row;
}
No need to use validation_errors in your php file.You can directly load your form page.Use validation_errors() in view page.
In your Controller, do this
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
Then in your formpage you can echo
<?php echo validation_errors(); ?>
change this line to
$this->data['showcombo'] = $this->load->sales_model->getComboProduct();
this
$this->data['showcombo'] = $this->load->sales_model->getProductCombo();
Because your
model name is
public function getProductCombo()
{
}
Firstly you load model in controller. And then called function, which you have defined in model..
$this->load->model('sales_model','sales'); // sales is alias name of model name
$this->data['showcombo'] = $this->sales->getComboProduct();

calling function from same controller not passing $data

I am building an HTML mobile app using Jquery Mobile. Its amazing so far.
However, In building my dynamnic template, I ran into a bit of a snag.
Heres my controller:
function index()
{
$data['page'] = "home";
$page['head'] = $this->load->view('template/head',$data,TRUE);
$page['header'] = $this->load->view('template/header', $data, TRUE);
$page['footer'] = $this->load->view('template/footer', $data, TRUE);
$page['nav'] = $this->nav($data['page']);
$this->load->view("pages/home", $page);
}
function nav($page)
{
$data['page'] = $page;
$page['header'] = $this->load->view('template/header', $data, TRUE);
$page['footer'] = $this->load->view('template/footer', $data, TRUE);
return $this->load->view('template/nav',$page,TRUE);
}
Regardless as to why I have it set up this way, any reason why, within the function nav($page) the view would return Undefined variable: header and Undefined variable: footer errors?
It's because you're trying to overwrite a string based variable with an array index.
You're passing in $page to the method, but then trying to create an array called $page to pass to the view. You need to rename the array to pass to the view:
function nav($page)
{
$data['page'] = $page;
// you should rename the array to pass to the view (from $page to $my_page)
$my_page['header'] = $this->load->view('template/header', $data, TRUE);
$my_page['footer'] = $this->load->view('template/footer', $data, TRUE);
return $this->load->view('template/nav',$my_page,TRUE);
}

Resources