1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Create Link to generate PDF File

Discussion in 'PHP' started by newphpcoder, Aug 25, 2015.

  1. #1
    Hi,

    Good day!

    I have a query to display data from my database.
    Now I display it in web page, but I need also to create a link to generate pdf file same as the web page display
    Here is my code:
    
     $query3 = "SELECT employee_no, arabic_name, fullname, nationality, join_date, passport_no, passport_end_date, res_no, res_end_date, company_work, company_sponsor
      FROM tbl_employee_informations WHERE res_end_date = '$date_visa' ORDER BY res_end_date";
       
    $res = $conn->query($query3);
      $cnt = $res->num_rows;
       
      <p>  
    echo"<a href=\"expiring_visa_pdf.php\" target=\"_Blank\" style='float:left;margin:auto auto 10px auto;font-weight:bold;text-decoration:none; color:#8d735a;'>Generate PDF<a/>";
      echo "<p style='float:left;margin:auto auto auto 350px;font-weight:bold; color:#8d735a;'>Total: $cnt </p>";
     
      echo "<div id='report_one'>";
      echo "<table style='border-radius:3px; border: 1px solid #a6a6a4;'>";
      echo "<tr style='text-align:center;font-weight:bold; background-color:#FFF;'>";
      echo "<th class='report_table'>EMPLOYEE NO.</th>";
      echo "<th class='report_table'>ARABIC NAME</th>";
      echo "<th class='report_table'>ENGLISH NAME</th>";
      echo "<th class='report_table'>NATIONALITY</th>";
      //echo "<th class='report_table'>JOIN DATE</td>";   
      echo "<th class='report_table'>PASSPORT NO.</th>";
      //echo "<th class='report_table'>PASSPORT END DATE</th>";
      echo "<th class='report_table'>RESIDENCY ID</th>";
      echo "<th class='report_table'>RESIDENCY END DATE</th>";
      echo "<th class='report_table'>COMPANY WORK</th>";
      echo "<th class='report_table'>COMPANY SPONSOR</th>";
      echo "</tr>";
    while($row = $res->fetch_assoc())
    {
      $employee_no = $row['employee_no'];
      $fullname = $row['fullname'];
      $fullname = utf8_encode($fullname);
      $arabic_name = $row['arabic_name'];
      $nationality = $row['nationality'];
      $join_date = $row['join_date'];
      $join_date = date('d-m-Y',strtotime($join_date));
      $passport_no = $row['passport_no'];
      $passport_end_date = $row['passport_end_date'];
      $passport_end_date = date('d-m-Y',strtotime($passport_end_date));
      $res_no = $row['res_no'];
      $res_end_date = $row['res_end_date'];
      $res_end_date = date('d-m-Y',strtotime($res_end_date));
      $company_work = $row['company_work'];
      $company_sponsor = $row['company_sponsor'];
       
      echo "<tr>";
      echo "<td class='report_table'>$employee_no</td>";
      echo "<td class='report_table' dir='RTL'>$arabic_name</td>";
      echo "<td class='report_table'>$fullname</td>";   
      echo "<td class='report_table'>$nationality</td>";   
      //echo "<td class='report_table'>$join_date</td>";
      echo "<td class='report_table'>$passport_no</td>";   
      //echo "<td class='report_table'>$passport_end_date</td>";
      echo "<td class='report_table'>$res_no</td>";
      echo "<td class='report_table'>$res_end_date</td>";
      echo "<td class='report_table'>$company_work</td>";
      echo "<td class='report_table'>$company_sponsor</td>";
      echo "</tr>";
    }
    
    echo "</table>";
    echo "</div>";
    
    Code (markup):
    Now, I need to convert this table form html to pdf once I click the Generate PDF.

    I don't know how to do that.

    I started by this:

    
     <?php
    require('fpdf/fpdf.php');
    
    class PDF extends FPDF
    {
    // Page header
    function Header()
    {
      // Logo
      $this->Image('images/logo.png',10,6,30);
      // Arial bold 15
      $this->SetFont('Arial','B',15);
      // Move to the right
      $this->Cell(80);
      // Title
      $this->Cell(30,10,'Expiring Visa');
      // Line break
      $this->Ln(20);
    }
    
    // Page footer
    function Footer()
    {
      // Position at 1.5 cm from bottom
      $this->SetY(-15);
      // Arial italic 8
      $this->SetFont('Arial','I',8);
      // Page number
      $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    }
    }
    
    
    
    // Instanciation of inherited class
    $pdf = new PDF();
    $pdf->AliasNbPages();
    $pdf->AddPage();
    $pdf->SetFont('Times','',12);
    //for($i=1;$i<=40;$i++)
      //  $pdf->Cell(0,10,'Printing line number '.$i,0,1);
    //$pdf->Cell();
    $pdf->Output();
    
    
    ?>
    
    Code (markup):
    but in this code I can only show the logo and title

    I hope someone can help me on this matter.

    Thank you.
     
    newphpcoder, Aug 25, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Dunno FPDF, but that output code you have there is... horrible. Seriously horrible.
    Rewritten:
    
    $get_employee_info = $conn->query("SELECT employee_no, arabic_name, fullname, nationality, join_date, passport_no, passport_end_date, res_no, res_end_date, company_work, company_sponsor FROM tbl_employee_informations WHERE res_end_date = '$date_visa' ORDER BY res_end_date");
    
    $count = $get_employee_info->numRows();
    
    echo '<a href="expiring_visa_pdf.php" class="pdf_link">Generate PDF<a/>
      <p>Total:'.$count.'</p>
    
      <div id="report_one">
      <table class="report_table">
      <thead>
      <tr>
      <th>EMPLOYEE NO.</th>
      <th>ARABIC NAME</th>
      <th>ENGLISH NAME</th>
      <th>NATIONALITY</th>
      <th>JOIN DATE</th>
      <th>PASSPORT NO.</th>
      <th>PASSPORT END DATE</th>
      <th>RESIDENCY ID</th>
      <th>RESIDENCY END DATE</th>
      <th>COMPANY WORK</th>
      <th>COMPANY SPONSOR</th>
      </tr>';
    
      while($row = $get_employee_info->fetch_assoc()) {
      echo '<tr>
      <td>'.$row['employee_no'].'</td>
      <td dir="RTL">'.$row['arabic_name'].'</td>
      <td>'.$row['fullname'].'</td>
      <td>'.$row['nationality'].'</td>
      <td>'.$row['join_date'].'</td>
      <td>'.$row['passport_no'].'</td>
      <td>'.$row['passport_end_date'].'</td>
      <td>'.$row['res_no'].'</td>
      <td>'.$row['res_end_date'].'</td>
      <td>'.$row['company_work'].'</td>
      <td>'.$row['company_sponsor'].'</td>
      </tr>';
    }
    
    echo '</table>
    </div>';
    
    PHP:
    You don't use target-attributes, inline-style, there's no need for a class on every th and td (just add a class if you need to one of the parent containers, in this case the table, and style via CSS. No need for multiple echo's, and if you just encapsulate and concoct, no need for escaping " inside echo.
     
    PoPSiCLe, Aug 25, 2015 IP
  3. newphpcoder

    newphpcoder Greenhorn

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    I tried this one, but I got problem in fetch data to database.
    I don't know why the data did not display

    
    <?php
    $today=date('Y-m-d');
    $hostname = "localhost";
    $database = "hr_big";
    $username = "root";
    $password = "vertrigo";
    $conn = mysql_connect($hostname, $username, $password) or die(mysql_error());
    mysql_select_db($database, $conn);
    date_default_timezone_set('UTC');
    require('fpdf/fpdf.php');
    class PDF_result extends FPDF {
       function __construct ($orientation = 'P', $unit = 'pt', $format = 'Letter', $margin = 40) {
         $this->FPDF($orientation, $unit, $format);
         $this->SetTopMargin($margin);
         $this->SetLeftMargin($margin);
         $this->SetRightMargin($margin);
         
         $this->SetAutoPageBreak(true, $margin);
       }
       
       function Header () {
        $this->Image('images/logo.png',100,15,200);
       //   $this->SetFont('Arial', 'B', 20);
       //   $this->SetFillColor(36, 96, 84);
       //   $this->SetTextColor(225);
       //   $this->Cell(0, 30, "YouHack MCQ Results", 0, 1, 'C', true);
       }
       
     function Footer()
    {
      //Position at 1.5 cm from bottom
      $this->SetY(-15);
      //Arial italic 8
      $this->SetFont('Arial','I',8);
      //Page number
      $this->Cell(0,10,'Generated at karismaa Attendance',0,0,'C');
    }
    //function Generate_Table($i, $employee_name, $project_name, $emp_designation, $status, $empdate) {
       function Generate_Table($employee_no, $fullname, $nationality, $join_date, $passport_no, $passport_end_date) {
       $this->SetFont('Arial', 'B', 12);
       $this->SetTextColor(0);
    //   $this->SetFillColor(94, 188, z);
    $this->SetFillColor(94, 188, 225);
       $this->SetLineWidth(1);
       $this->Cell(30, 25, "Employee No.", 'LTR', 0, 'C', true);
       $this->Cell(120, 25, "English Name", 'LTR', 0, 'C', true);
       $this->Cell(100, 25, "Nationality", 'LTR', 0, 'C', true);
       $this->Cell(100, 25, "Join Date", 'LTR', 0, 'C', true);
       $this->Cell(100, 25, "Passport No.", 'LTR', 0, 'C', true);
       $this->Cell(100, 25, "Passport End Date", 'LTR', 0, 'C', true);
       
       $this->SetFont('Arial', '');
       $this->SetFillColor(238);
       $this->SetLineWidth(0.2);
       $fill = false;
    }
       function qry($qry)
    {
       $this->$qry=mysql_query("select * from tbl_employee_informations where res_end_date BETWEEN '2015-01-17' AND '2015-03-01'",$this->$conn);
       if($this->$qry!=false)
    {
       //$i=1;
       while($this->$res=mysql_fetch_array($qry))
       {
         $myArr=array($res['employee_no'],$res['fullname'],$res['nationality'],$res['join_date'],$res['passport_no'],$res['passport_end_date']);
       //   $this->$i++;
       }
    }
    
    }
       
       //for ($i = 0; $i < count($subjects); $i++) {
       //   $this->Cell(427, 20, $subjects[$i], 1, 0, 'L', $fill);
       //   $this->Cell(100, 20,  $marks[$i], 1, 1, 'R', $fill);
       //   $fill = !$fill;
       //}
       //$this->SetX(367);
       //$this->Cell(100, 20, "Total", 1);
    //   $this->Cell(100, 20,  array_sum($marks), 1, 1, 'R');
    }
    $pdf = new PDF_result();
    $pdf->AddPage();
    $pdf->SetFont('Arial', 'B', 12);
    $pdf->SetY(100);
    $pdf->Cell(100, 13, "");
    $pdf->SetFont('Arial', 'B');
    $pdf->Cell(250, 13, "Expiring Visa");
    $pdf->SetFont('Arial', 'B');
    $pdf->Cell(50, 13, "Date:");
    $pdf->SetFont('Arial','');
    $pdf->Cell(100, 13, date('F j, Y'), 0, 1);
    $pdf->SetFont('Arial', 'I');
    $pdf->SetX(140);
    //$pdf->Cell(200, 15, $_POST['e-mail'], 0, 2);
    //$pdf->Cell(200, 15, $_POST['Address'] . ',' . $_POST['City'] , 0, 2);
    //$pdf->Cell(200, 15, $_POST['Country'], 0, 2);
    $pdf->Ln(100);
    $pdf->Generate_Table($employee_no, $fullname, $nationality, $join_date, $passport_no, $passport_end_date );
    $pdf->Ln(50);
    $message = "For More Information Contact us at : ";
    $pdf->MultiCell(0, 15, $message);
    $pdf->SetFont('Arial', 'U', 12);
    $pdf->SetTextColor(1, 162, 232);
    $pdf->Write(13, "support@domain.com", "support@domain.com");
    $pdf->Output('result-.$today.pdf', 'F');
    ?>
    
    Code (markup):
     
    newphpcoder, Aug 25, 2015 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    It was untested, and may contain some errors. You could try it just keeping the current db connection and naming, just alter the echo and html.
    As for the fpdf bit - i can't see anywhere in the code that you're actually outputting the info from the db / putting what is in the table into the pdf?
     
    PoPSiCLe, Aug 26, 2015 IP