简体   繁体   中英

How to export mySQL data to PDF in Codeigniter?

I'm new to Codeigniter and I need to export the mySQL data into a PDF file.

How can I do that?

Download library from here

Library explanation

After download extract folder You have you will find two files namely class.ezpdf.php/cezpdf.php and class.pdf.php. Now put these two .php files inside application/libraries. To make these work within CI you will have to a modification in the cezpdf.php/class.ezpdf.php. The modification is to be done in the include statement :

include_once(APPPATH . 'libraries/class.pdf.php');

Now go to your controller folder and there make a new file name generate.php and pdf_helper.php.

pdf_helper.php :

<?php

function prep_pdf($orientation = 'portrait')
{
    $CI = & get_instance();

    $CI->cezpdf->selectFont(base_url() . '/fonts');    

    $all = $CI->cezpdf->openObject();
    $CI->cezpdf->saveState();
    $CI->cezpdf->setStrokeColor(0,0,0,1);
    if($orientation == 'portrait') {
        $CI->cezpdf->ezSetMargins(50,70,50,50);
        $CI->cezpdf->ezStartPageNumbers(500,28,8,'','{PAGENUM}',1);
        $CI->cezpdf->line(20,40,578,40);
        $CI->cezpdf->addText(50,32,8,'Printed on ' . date('m/d/Y h:i:s a'));
        $CI->cezpdf->addText(50,22,8,'PDF Tutorial');
    }
    else {
        $CI->cezpdf->ezStartPageNumbers(750,28,8,'','{PAGENUM}',1);
        $CI->cezpdf->line(20,40,800,40);
        $CI->cezpdf->addText(50,32,8,'Printed on ' . date('m/d/Y h:i:s a'));
        $CI->cezpdf->addText(50,22,8,'PDF Tutorial');
    }
    $CI->cezpdf->restoreState();
    $CI->cezpdf->closeObject();
    $CI->cezpdf->addObject($all,'all');
}

?>

generate.php :

<?php

class Generate extends CI_Controller
{

    function Generate()
    {
        parent::__construct();
        $this->load->database();
        $this->load->helper('url');
    }


    function create()
    {

                $this->load->library('cezpdf');

        $this->cezpdf->ezText('PDF REPORT OF LOGIN TABLE', 12, array('justification' => 'center'));
        $this->cezpdf->ezSetDy(-10);
                $i=1;
                $content="";

                $fname="";
                $query = $this->db->query('SELECT * FROM table_name');
                $num = $query->num_fields();
                $farr=array();

                while($i <= $num){
                    $test = $i;
                    $value = $this->input->post($test);

                    if($value != ''){
                            $fname= $fname." ".$value;
                            array_push($farr, $value);

                        }
                     $i++;
                }

                $fname = trim($fname);

                $fname=str_replace(' ', ',', $fname);
                $this->db->select($fname);
                $query = $this->db->get('table_name');
                $result = $query->result();

                foreach ($farr as $j)
                {

                    $content= strtoupper($j)."\n\n";
                    foreach($result as $res){
                       $content = $content.$res->$j."\n";
                    }

                      $this->cezpdf->ezText($content, 10);

                       $this->cezpdf->ezStream();
                 }

    }

In the above, first thing we do is load the R&OS library for use. Next we use the ezText() function to create a title for our document. This function takes the text it will display as the first argument, the size of that text and an optional array of additional configuration options.

After the whites pace we put the rest of the content for the document in a variable called $content and add it to our document using the ezText() function again. Finally, we create our document using the ezStream() function which actually creates the document and sends it to the users which prompts them to view/download the generated PDF document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM