title: How to Export Html Table to CSV and PDF with formatting using Php
body: <p>I created Html Table, in which there is data from mysql tables, and now  i want to export the data into CSV and PDF file, how? <br>
  I want to, when i click on button (separate for both CSV and PDF), it automatically download in CSV and PDF file with correct Formatting.<br>
  I try some of script i found on internet, but it is not working, some have formatting issue and save have downloading. <br>
  In Advance Thanks.<br></p>
  
  <pre><code>&lt;table id="example2" class="table table-bordered table-hover"&gt;
                  &lt;thead&gt;
                  &lt;tr&gt;
          &lt;th&gt;District&lt;/th&gt;
          &lt;th&gt;Union Council&lt;/th&gt;
          &lt;th&gt;Village&lt;/th&gt;
          &lt;th&gt;Recreational&lt;/th&gt;
          &lt;th&gt;Awareness Raising&lt;/th&gt;
          &lt;th&gt;Training&lt;/th&gt;
          &lt;th&gt;Social Mobilization&lt;/th&gt;
          &lt;th&gt;Total Activities&lt;/th&gt;
  
                  &lt;/tr&gt;
                  &lt;/thead&gt;
                    &lt;tbody&gt;
        &lt;tr&gt;
        &lt;td style="color:red;"&gt;&lt;b&gt;&lt;?php echo getTotalDistrictUnique();?&gt;&lt;/b&gt;&lt;/td&gt;
        &lt;td style="color:red;"&gt;&lt;b&gt;&lt;?php echo getTotalUnionCouncilUnique();?&gt;&lt;/b&gt;&lt;/td&gt;
        &lt;td style="color:red;"&gt;&lt;b&gt;&lt;?php echo getTotalVillages(); ?&gt;&lt;/b&gt;&lt;/td&gt;
        &lt;td style="color:red;"&gt;&lt;b&gt;&lt;?php echo getTotalRecreational();?&gt;&lt;/b&gt;&lt;/td&gt;
        &lt;td style="color:red;"&gt;&lt;b&gt;&lt;?php echo getTotalAwareness();?&gt;&lt;/b&gt;&lt;/td&gt;
        &lt;td style="color:red;"&gt;&lt;b&gt;&lt;?php echo getTotalTraining();?&gt;&lt;/b&gt;&lt;/td&gt;
        &lt;td style="color:red;"&gt;&lt;b&gt;&lt;?php echo getTotalSocial();?&gt;&lt;/b&gt;&lt;/td&gt;
        &lt;td style="color:red;"&gt;&lt;b&gt;&lt;?php echo getTotalRecreational() + getTotalAwareness() + getTotalTraining() + getTotalSocial(); ?&gt;&lt;/td&gt;
  
        &lt;/tr&gt;
  
        &lt;?php
  include("connection.php");
  $query ="select * from general";
  $run =mysqli_query($con,$query);
  while ($row=mysqli_fetch_array($run)) {
      $id=$row['id'];
      $createdate=$row[1];
      $createday=$row[2];
      $partnername=$row[3];
      $district=$row[4];
      $unioncouncil=$row[5];
      $village=$row[6];
      $vannumber=$row[7];
      $facilitator=$row[8];
      $beneficiarytype=$row[9];
      $rmultimedia=$row[10];
      $rgame=$row[11];
      $rsprort=$row[12];
      $rart=$row[13];
      $rgroupreading=$row[14];
      $rother=$row[15];
      $alandminer=$row[16];
      $apersonalsafety=$row[17];
      $abirthregister=$row[18];
      $aother=$row[19];
      $tstickstone=$row[20];
      $tohthers=$row[21];
      $sbirthregister=$row[22];
      $sother=$row[23];
      $formnumber=$row[24];
      $submitdatatime=$row[25];
  ?&gt;
  
        &lt;tr&gt;
        &lt;td&gt;&lt;?php echo $district?&gt;&lt;/td&gt;
        &lt;td&gt;&lt;?php echo $unioncouncil?&gt;&lt;/td&gt;
        &lt;td&gt;&lt;?php echo $village?&gt;&lt;/td&gt;
        &lt;td&gt;
        &lt;?php 
        if($rmultimedia=='true')
         {
          $rmultimedia_value = 1;
          }
          else{
              $rmultimedia_value = 0;
          }
  
          if($rgame=='true')
         {
          $rgame_value = 1;
          }
          else{
              $rgame_value = 0;
          }
  
          if($rsprort=='true')
         {
          $rsprort_value = 1;
          }
          else{
              $rsprort_value = 0;
          }
  
          if($rart=='true')
         {
          $rart_value = 1;
          }
          else{
              $rart_value = 0;
          }
  
          if($rgroupreading=='true')
         {
          $rgroupreading_value = 1;
          }
          else{
              $rgroupreading_value = 0;
          }
  
          if($rother=='true')
         {
          $rother_value = 1;
          }
          else{
              $rother_value = 0;
          }
  
        $recreational_sum = $rmultimedia_value + $rgame_value + $rsprort_value + $rart_value + $rgroupreading_value + $rother_value;
  
        echo $recreational_sum;?&gt;&lt;/td&gt;
  
        &lt;td&gt;
            &lt;?php 
        if($alandminer=='true')
         {
          $alandminer_value = 1;
          }
          else{
              $alandminer_value = 0;
          }
  
          if($apersonalsafety=='true')
         {
          $apersonalsafety_value = 1;
          }
          else{
              $apersonalsafety_value = 0;
          }
  
          if($abirthregister=='true')
         {
          $abirthregister_value = 1;
          }
          else{
              $abirthregister_value = 0;
          }
  
          if($aother=='true')
         {
          $aother_value = 1;
          }
          else{
              $aother_value = 0;
          }
  
        $awareness_raising_sum = $alandminer_value + $apersonalsafety_value + $abirthregister_value + $aother_value;
  
        echo $awareness_raising_sum;?&gt;
        &lt;/td&gt;
  
        &lt;td&gt;
                &lt;?php 
        if($tstickstone=='true')
         {
          $tstickstone_value = 1;
          }
          else{
              $tstickstone_value = 0;
          }
  
          if($tohthers=='true')
         {
          $tohthers_value = 1;
          }
          else{
              $tohthers_value = 0;
          }
  
        $training_sum = $tstickstone_value + $tohthers_value;
  
        echo $training_sum;?&gt;
        &lt;/td&gt;
  
        &lt;td&gt;
          &lt;?php 
        if($sbirthregister=='true')
         {
          $sbirthregister_value = 1;
          }
          else{
              $sbirthregister_value = 0;
          }
  
          if($sother=='true')
         {
          $sother_value = 1;
          }
          else{
              $sother_value = 0;
          }
  
        $social_mobilization_sum = $sbirthregister_value + $sother_value;
  
        echo $social_mobilization_sum;?&gt;
        &lt;/td&gt;
  
        &lt;td style="color:red;"&gt;&lt;?php echo $recreational_sum + $awareness_raising_sum + $training_sum + $social_mobilization_sum;?&gt;&lt;/td&gt;
  
        &lt;/tr&gt;
  &lt;?php } ?&gt;
      &lt;/tbody&gt;
                &lt;/table&gt;
  </code></pre>