How to convert HTML to PDF in PHP Codeigniter - php

I have a HTML form data getting from database.
I use PHP framework codeigniter . we want generate a PDF file from this HTML form data. tell me how to do.

You may wanna look at html2Pdf. It is a php class based on FPDF and it allows you to create PDF file from HTML. So just format your text using html and then create its pdf. Its very flexible and give greate control.
SorceForge Link
Or You can also look at this: Convert HTML + CSS to PDF with PHP?

Look at DomPDF https://github.com/dompdf/dompdf
Examples: http://pxd.me/dompdf/www/examples.php
By the way, on my project I use Zend Framework and ZendParadoxPDF to ge generate pdf. But it requires Java, so ASAP I'll redevelop it using DomPDF.

You can also use mPDF Library to create pdf in codeignator. create the mpdf.php file. After that put it into the CodeIgniter library directory.
copy this code to mpdf.php
if (!defined('BASEPATH')) exit('No direct script access allowed');
include_once APPPATH.'/third_party/mpdf/mpdf.php';
class M_pdf {
public $param;
public $pdf;
public function __construct($param = "'c', 'A4-L'")
{
$this->param =$param;
$this->pdf = new mPDF($this->param);
}
}
Then, download mPDF Library from github or mpdf website.After downloading, Extract it and put the mpdf folder into CodeIgniter third_party directory.Then,
Open your controller(here,i am using Welcome controller) ,create a method on that controller as following
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
function my_mPDF(){
$filename = time()."_order.pdf";
$html = $this->load->view('unpaid_voucher',$data,true);
// unpaid_voucher is unpaid_voucher.php file in view directory and $data variable has infor mation that you want to render on view.
$this->load->library('M_pdf');
$this->m_pdf->pdf->WriteHTML($html);
//download it D save F.
$this->m_pdf->pdf->Output("./uploads/".$filename, "F");
}
}
?>
Here, my_ mPDF function of Welcome controller will generate pdf file of common/template view file.
It’s done. I hope it will help you.

Related

How to use PHPSpreadsheet in CodeIgniter 3 to read data from Excel (.xlsx and .xls) file?

Recently in one of my CodeIgniter based project, I need to read data from Excel file ( .xlsx and .xls ) and insert those data into MySQL. Unfortunately, I did not use PHPSpreadsheet before (as I did not require to work with Excel :( ).
So far what I did was, download the PHPSpreadsheet from Github and extract it to the root directory of my CodeIgniter Project.
PHPSpreadsheet in CodeIgniter root directory.
File Structure of PHPSpreadsheet.
So far I've tried to import the official docs example into my CI apps:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use PhpOffice\PhpSpreadsheet\IOFactory;
class Welcome extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index()
{
$inputFileType = 'Xlsx';
$inputFileName = 'test.xlsx';
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $reader->load($inputFileName);
print_r( $spreadsheet );
}
}
But it shows the following error!!
Can anyone tell me how can I use PHPSpreadsheet in CodeIgniter to read data from Excel file and store them into MySQL database?
Thanks
The easiest way to include PhpSpreadsheet is to use Composer - the documentation at https://phpspreadsheet.readthedocs.io/en/latest/ explains how to do this.
Otherwise you will need a PSR4 autoloader to load the class specified by your use statement or to include the files yourself. If you include files yourself, to read an Excel file you will need to include PhpSpreadsheet/src/PhpSpreadsheet/IOFactory.php

tcpdf codeigniter getting error

I am trying to implement tcpdf library in codeigniter but getting error
Fatal error: Cannot redeclare class Pdf in C:\xampp\htdocs\project\system\libraries\Pdf.php on line 6
here code example
path : "project\system\libraries\Pdf.php"
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once dirname(__FILE__) . '/tcpdf/tcpdf.php';
class Pdf extends TCPDF
{
function __construct()
{
parent::__construct();
}
}
/* End of file Pdf.php */
/* Location: ./application/libraries/Pdf.php */
also Download latest
TCPDF
also want to know which is best TCPDF or DOMPDF to generate quick PDF ?
When using/making custom libraries, smart move would be (as CI makers advise too) to put it in APPPATH.'libraries/' because of upgrading version could overwrite/delete added files from system directory. That is why application folder is used for - to make any kind of custom files related to CI framework not touching default files/directories.
Other than that, error you've got there says there is more than one or better say there is two classes with same name. PHP doesn't allow that and every used file and/or class must have unique name. Possible solution should be renaming your file to
BASEPATH.'Pdf_lib.php'
and class name accordingly.
But again, if you are not limited with something else, move your class to
APPPATH.'libraries/Pdf_lib.php'
with following directories and subdirectories and files of tcpdf and use it from there.
I fix this error using bellow method. its happen coz same class is call from other TCPDF included file here's the solution
path : "project\system\libraries\Pdf.php"
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once '/tcpdf/tcpdf.php';
if (!class_exists('Pdf')) {
class Pdf extends TCPDF
{
function __construct()
{
parent::__construct();
}
}
}

How to use core php into Codeigniter controller files?

I would like to use http://demo.hazzardweb.net/imgPicker/docs/. I have downloaded all the files.
How can I convert it into a Codeigniter third party library or how can I call this class file in a Codeigniter controller? Is it possible to call the core php files directly in a controller to achieve this?
For example:
require dirname(__FILE__) . '/ImgPicker.php';
You can create a new library for your project.
Your library classes should be placed within your application/libraries directory, as this is where CodeIgniter will look for them when they are initialized.
Be sure that:
File names must be capitalized. For example: ImgPicker.php
Class declarations must be capitalized. For example: class ImgPicker
Class names and file names must match.
Your ImgPicker class file
Classes in general should have this basic prototype:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class ImgPicker {
// all stuff that your php library does
public function some_method()
{
. . .
}
}
Then from your controller (or model) you can load your library with $this->load->library('someclass'); and you can pass vars to the constructor dynamically like so:
$params = array('type' => 'large', 'color' => 'red');
$this->load->library('ImgPicker', $params);
You can find a lot of useful information how to create your own libraries (or convert existing php classes to CI libraries) at CI documentation.

class PDF extends FPDF not working

I am using FPDF to generate PDF invoices for osCommerce. My pdfinvoice.php file contains the following code to enhance the standard FPDF class:
class PDF extends FPDF {
}
For some reason unknown to me all functions defined within the PDF class are not working. To be able to successfully generate a PDF file I have go back to:
$pdf = new FPDF('P', 'pt', array(500,233));
instead of:
$pdf=new PDF( PDF_INV_PORTRAIT_LANDSCAPE,'mm', 'A4' );
In addition, any other reference to a function defined in the PDF class will stop the PDF file from being generated
NOTE: This behaviour only occurs when pdfinvoice.php is put into a specific subfolder. In my main folder all works fine!
For full details please refer to: http://forums.oscommerce.com/topic/271214-contribution-pdf-customer-invoice/page-34#entry1713911
Kind regards,
Dennis

understanding the basic codeIgniter

<?php
if (!defined('BASEPATH', ''))
exit('no direct script access allowed');
class Hello extends CI_Controller {
public function index() {
echo "this is my index function";
}
public function one() {
$this->load->view('one');
}
}
?>
I am the new user of codeIgniter.First i download code igniter software,then place it to the xampp/htdocs.Then i create a simple codeIgniter code which are show in the above.I don't understand where this code is to be save.I save this code in the Codeignitor/appilication.but it give me the below answer:
no direct script access allowed
What are the fault.please help me anyone.
Have you read the User Guide of Codeigniter? This is your Controller code and it should be placed inside appilication/controllers folder by name hello.php. See this.
Before starting of any framework it would be good if you understanding directory structure, and what is the role of files.
Where should keep HTML, CSS, JS and logic and model part.
As you creating controller then it should save under
application/controllers/ControllerName.php
Here is the guideline for controller
Also refer tutorials
http://tutorialcodeigniter.com
http://codesamplez.com/development/codeigniter-basic-tutorial
Open config.php within CodeIgniter\system\application\config.
Change base site url http://localhost/foldername

Categories