Report genearation in pdf form

I'm trying to generate report in pdf form from dynamic html table.When I try to do that from Query 2 in below code I'm able to generate the pdf but from Query 1 I'm only able to generate html table It's not sending dynamic data from html table to pdf. I'm selcting my course,semester,subjects from other page from <form action="pdf.php" method="post">.Any help would be greatly appreciated.

 <?php  
 function fetch_data()  
 {

  $courses = isset($_POST['courses']) ? $_POST['courses'] : '';
  $semesters = isset($_POST['semesters']) ? $_POST['semesters'] : '';
  $subjects = isset($_POST['subjects']) ? $_POST['subjects'] : '';
  $output = '';  
  $conn = mysqli_connect("localhost", "root", "", "db_sars");  
  //Query 1
 $sql = "SELECT att.student_no, CONCAT(student.firstname,' ' ,student.lastname) AS Name, 
                                                     att.subjects,  COUNT(CASE WHEN att.attendance_status='P' THEN 'P' END) / COUNT(subjects) *100 AS  
                                                     Attendance FROM att INNER JOIN student ON att.student_no = student.student_no  
                                                     AND att.courses='$courses' AND att.semesters='$semesters' AND att.subjects='$subjects'
                                                     GROUP BY subjects,student_no"; 

//Query 2
/*$sql="SELECT att.student_no, CONCAT(student.firstname,' ' ,student.lastname) AS Name, 
                                                     att.subjects,  COUNT(CASE WHEN att.attendance_status='P' THEN 'P' END) / COUNT(subjects) *100 AS  
                                                     Attendance FROM att INNER JOIN student ON att.student_no = student.student_no GROUP BY subjects,student_no ";*/

$result = mysqli_query($conn, $sql);  
  while($row = mysqli_fetch_array($result))  
  {       
  $output .= '<tr>  
                      <td>'.$row["student_no"].'</td>  
                      <td>'.$row["Name"].'</td>  
                      <td>'.$row["subjects"].'</td>  
                      <td>'.$row["Attendance"].'</td>  
                 </tr>  
                      ';  
  }  
  return $output; 
} 

if(isset($_POST["generate_pdf"]))  
{ 
$output=$_POST['result'];
  require_once('tcpdf/tcpdf.php');  
  $obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);  
  $obj_pdf->SetCreator(PDF_CREATOR);  
  $obj_pdf->SetTitle("Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP");  
  $obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);  
  $obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));  
  $obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));  
  $obj_pdf->SetDefaultMonospacedFont('helvetica');  
  $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);  
  $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '10', PDF_MARGIN_RIGHT);  
  $obj_pdf->setPrintHeader(false);  
  $obj_pdf->setPrintFooter(false);  
  $obj_pdf->SetAutoPageBreak(TRUE, 10);  
  $obj_pdf->SetFont('helvetica', '', 11);  
  $obj_pdf->AddPage();  
  $content = '';  
  $content .= '  
  <h4 align="center">Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP</h4><br /> 
  <table border="1" cellspacing="0" cellpadding="3">  
       <tr>  
            <th width="15%">Student Id</th>  
            <th width="30%">Name</th>  
            <th width="30%">Subjects</th>  
            <th width="25%">Attendance</th>  
       </tr>  
  ';  
  $content .= fetch_data();  
  $content .= '</table>';  
  $obj_pdf->writeHTML($content);  
  $obj_pdf->Output('file.pdf', 'I');  
 } 
?>
<!DOCTYPE html>  


<html>  
  <head>  
       <title>Total Attendance</title>  
       <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />            
  </head>  
  <body>  
       <br />
       <div class="container">  
            <h4 align="center"> Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP</h4><br />  
            <div class="table-responsive">  
                <div class="col-md-12" align="right">
                 <form method="post">  
                      <input type="submit" name="generate_pdf" class="btn btn-success" value="Generate PDF" />  
                 </form>  
                 </div>
                 <br/>
                 <br/>
                 <table class="table table-bordered">  
                      <tr>  
                           <th width="15%">Student Id</th>  
                           <th width="30%">Name</th>  
                           <th width="30%">Subjects</th>  
                           <th width="25%">Attendance</th>  
                      </tr>  
                 <?php  
                 echo fetch_data();  
                 ?>  
                 </table>  
            </div>  
       </div>  
  </body>  
</html>