Membuat pdf dari database - URL web dan Tutorial - Publisher - Royz Rambo
Ilham Penta
Site menu
Section categories
My articles [6]
Java [14]
Java Programming
Flash [6]
Flash
URL web dan Tutorial [29]
Petualangan [1]
Our poll
Rate my site
Total of answers: 19
Statistics

Total online: 1
Guests: 1
Users: 0
Login form
Main » Articles » URL web dan Tutorial

Membuat pdf dari database
Script code untuk membuat pdf dengan data dari database, dan library fpdf

block   
  1. //koneksi ke database
  2. $host = "localhost";
  3. $user = "root";
  4. $pass = "";
  5. $dbnm = "dbmahasiswa";
  6.  
  7. $conn = mysql_connect($host, $user, $pass);
  8. if ($conn) {
  9. $open = mysql_select_db($dbnm);
  10. if (!$open) {
  11. die ("Database tidak dapat dibuka karena ".mysql_error());
  12. }
  13. } else {
  14. die ("Server MySQL tidak terhubung karena ".mysql_error());
  15. }
  16. //akhir koneksi
  17.  
  18. #ambil data di tabel dan masukkan ke array
  19. $query = "SELECT * FROM mhs ORDER BY nama";
  20. $sql = mysql_query ($query);
  21. $data = array();
  22. while ($row = mysql_fetch_assoc($sql)) {
  23. array_push($data, $row);
  24. }
  25.  
  26. #setting judul laporan dan header tabel
  27. $judul = "LAPORAN DATA MAHASISWA";
  28. $header = array(
  29. array("label"=>"NIM", "length"=>30, "align"=>"L"),
  30. array("label"=>"NAMA", "length"=>50, "align"=>"L"),
  31. array("label"=>"ALAMAT", "length"=>80, "align"=>"L"),
  32. array("label"=>"TGL LAHIR", "length"=>30, "align"=>"L")
  33. );
  34.  
  35. #sertakan library FPDF dan bentuk objek
  36. require_once ("fpdf16/fpdf.php");
  37. $pdf = new FPDF();
  38. $pdf->AddPage();
  39.  
  40. #tampilkan judul laporan
  41. $pdf->SetFont('Arial','B','16');
  42. $pdf->Cell(0,20, $judul, '0', 1, 'C');
  43.  
  44. #buat header tabel
  45. $pdf->SetFont('Arial','','10');
  46. $pdf->SetFillColor(255,0,0);
  47. $pdf->SetTextColor(255);
  48. $pdf->SetDrawColor(128,0,0);
  49. foreach ($header as $kolom) {
  50. $pdf->Cell($kolom['length'], 5, $kolom['label'], 1, '0', $kolom['align'], true);
  51. }
  52. $pdf->Ln();
  53.  
  54. #tampilkan data tabelnya
  55. $pdf->SetFillColor(224,235,255);
  56. $pdf->SetTextColor(0);
  57. $pdf->SetFont('');
  58. $fill=false;
  59. foreach ($data as $baris) {
  60. $i = 0;
  61. foreach ($baris as $cell) {
  62. $pdf->Cell($header[$i]['length'], 5, $cell, 1, '0', $kolom['align'], $fill);
  63. $i++;
  64. }
  65. $fill = !$fill;
  66. $pdf->Ln();
  67. }
  68.  
  69. #output file PDF
  70. $pdf->Output();
  71. ?> Enter code here



Source: http://achmatim.net/2009/11/29/step-by-step-php-membuat-laporan-pdf-dengan-fpdf/
Category: URL web dan Tutorial | Added by: penta (2011-10-11)
Views: 6579 | Tags: PHP, mysql, PDF | Rating: 0.0/0
Total comments: 0
Name *:
Email *:
Code *:
Search
Site friends
  • Create a free website
  • Copyright Penta.Inc © Powered By: Senjapala