Xuất dữ liệu từ PHP sang PDF

Trong lập trình web, không những phải hoc php  mà còn phải học những công cụ hỗ trợ cho lập trình, mã nguồn mở,… còn cả cơ sở dữ liệu của trang web đấy sẽ gồm những gì, hoạt động như thế nào.  Trong bài này tôi sẽ hướng dẫn các bạn xuất dữ liệu từ Mysql ra PDF có định dạng dữ liệu cực đẹp.

Trước tiên bạn phải tạo 1 project. Sau đó bạn dowload thư viện này về cho vào trong project của bạn tại đây

Sau đó bạn tạo một file là exportpdf.php sau đó chúng ta sẽ bắt tay vào xây dựng như sau:

[codesyntax lang=”php”]

require('fpdf.php'); //include thư viện

$d=date('d_m_Y');

class PDF extends FPDF

{

function Header()

{

     $name="Export PDF";

   $this->SetFont('Arial','B',15);

   //Move to the right

   $this->Cell(80);

   //Title

      $this->SetFont('Arial','B',9);

   //Line break

   $this->Ln(20);

}

//Page footer

function Footer()

{

}

//Load data

function LoadData($file)

{

      //Read file lines

      $lines=file($file);

      $data=array();

      foreach($lines as $line)

            $data[]=explode(';',chop($line));

      return $data;

}



//tao bang

function BasicTable($header,$data)

{

$this->SetFillColor(0,255,0);

$this->SetDrawColor(128,0,0);

$w=array(30,15,20,10,10,10,10,10,15,15,15,15,15);

      //Header

      for($i=0;$i<count($header);$i++)

            $this->Cell($w[$i],7,$header[$i],1,0,'C',true);

      $this->Ln();

      //Data

      foreach ($data as $eachResult)

      { //width

            $this->Cell(30,6,$eachResult["bookstall_id"],1);

            $this->Cell(15,6,$eachResult["name"],1);

            $this->Cell(20,6,$eachResult["location"],1);

            $this->Cell(10,6,$eachResult["address"],1);

            $this->Cell(10,6,$eachResult["telephone"],1);

            $this->Ln();



      }

}

//Better table

}
[/codesyntax]

Đã xong phần thủ tục ban đầu. Bây giờ đến phần lấy dữ liệu của chúng ta ra

[codesyntax lang=”php”]

$pdf=new PDF();

$header=array('bookstall_id','name','location','address','telephone');

// lay du lieu trong bang.

$objConnect = mysql_connect("localhost","database_username","database_password") or die("Error:Please check your database username & password");

$objDB = mysql_select_db("database_name");

$strSQL = "SELECT bookstall_id,     name, location,   address,    telephone FROM bookstall";

$objQuery = mysql_query($strSQL);

$resultData = array();

for ($i=0;$i<mysql_num_rows($objQuery);$i++) {

      $result = mysql_fetch_array($objQuery);

      array_push($resultData,$result);

}

//************************//
[/codesyntax]


Ở đây tôi lấy dữ liệu từ bảng bookstall, các bạn nhớ cấu hình lại kết nối cho phù hợp với database của bạn nhé. Bây giờ bắt đầu ghi dữ liệu ra PDF :

[codesyntax lang=”php”]

function forme()

{

$d=date('d_m_Y');

echo "PDF generated successfully. To download document click on the link >> <a href=".$d.".pdf>DOWNLOAD</a>";

}

$pdf->SetFont('Arial','',6);

//*** Table 1 ***//

$pdf->AddPage();

$pdf->Ln(35);

$pdf->BasicTable($header,$resultData);

forme();

$pdf->Output("$d.pdf","F");



?>
[/codesyntax]

Bạn ra trình duyệt kiểm tra kết quả thôi.

Đây là source code đầy đủ để các bạn tham khảo!


Xem thêm:
>> Địa chỉ in tem decal oto giá rẻ
>> Địa chỉ in bạt khổ lớn



http://bsnguyenphuonghong.com/giam-ngay-50-phi-cat-bao-quy-dau-va-kiem-tra-nam-phu-khoa-thang-8/

Comments

Popular posts from this blog

Tạo mã xác nhận cho form đăng ký thành viên

Các thuộc tính của font trong CSS

Đếm số người đang online trên Website bằng Whos.amung.us - Blog của bạn