Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to export data in PDF file from database

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 345
    Comment on it

    Data fetch from database according to the user's entry in the database table in the pdf format, benefit of using this format for saving our data in compact and secure form, this will help to user to store the data in the file. Here is simple line of code that will help to to code for saving it in the pdf format.
    In the usercontroller we have to import some files name like vendor and fpdf,as users can see them in controller. Then in the ctp file we have to use for each function for fetching the values in string form.


    User Controller:

    App::uses('AppController', 'Controller');
    App::import('Vendor', 'Fpdf', array('file' => 'fpdf/fpdf.php'));
        public $uses=array();
    
          public function pdf() 
          {
    
        $this->layout = 'pdf'; //this will use the pdf.ctp layout
        $this->set('fpdf', new FPDF('P','mm','A4'));
        $this->set('data', '' );
        $this->set('users', $this->User->find('all'));
    
          }

    pdf.ctp

    <?php
    
     foreach ($users as $key=>$value)
    {
    	$post= $users[$key]['User'];
    	
        $ecex= implode(",", $post);
        $data= $data ."\n". $ecex;
    	
      
    }
    	 
        $fpdf->AddPage();
        $fpdf->SetFont('helvetica', '',10);
        $fpdf->SetTextColor(255, 4, 4);
        $fpdf->Cell(20,0,$data);
        $fpdf->Output();
        echo $this->Html->link(__('PDF'), array('action' => 'pdf', 'ext' => 'pdf', $user['Users']['id']));
    
    ?>


    layout:

    <?php
        header('Content-Disposition: attachment; filename="Pdf Format.pdf"');
        echo $content_for_layout;
    ?>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: