How to output mysql table content to pdf using tcpdf?

I need to create pdf document which outputs all rows from mysql table, below are the attached code

<div class="container-fluid">
  <div class="add-result1">
	<table>
		<th>No.</th>
		<th>DrugName</th>
		<th>Description</th>
		<th>Quantity Entered</th>
		<th>Quantity Status</th>
		<th>Wholesale Price</th>
		<th>retail Price</th>
		<th>Total Wholesale Price</th>
		<th>Total retail Price</th>
		<?php view_dawa_pharmacist(); ?>
	</table>
	<div class="nafasi">

	<label>Total Amount Wholesale Price is <?php amounttotalwholesale(); ?> </label><br/>
	<label>Total Amount Retail Price is <?php amounttotalretail(); ?> </label>

<a href="pdf.php"><button id="pdf">Print PDF</button></a>
</div>
		</div>
			</div>

pdf.php will be the page containing the “code.” :slight_smile:

What seems to be wrong? Is there an error in the console?

@StevenHu actually it doesn’t work.

Can you show the code for pdf.php, where you take the data and build it into the PDF file?

@droopsnoot
This is my pdf.php

<?php
session_start();
?>
<?php include ('/../control/function.php'); ?>
<?php 

function fetch_data()
{
    //session_start();
    include 'connect.php';
    $drug=$_SESSION['id'];

    $output='';

    $select_drug="select * from dawa order by sid ASC";
    $run_drug=mysql_query($select_drug);

    while($row_data=mysql_fetch_array($run_drug))


    {
    $drug=$row_data['id'];
    $output .='

<div class="container-fluid">
  <div class="add-result1">
  <table>
    <th>No.</th>
    <th>DrugName</th>
    <th>Description</th>
    <th>Quantity Entered</th>
    <th>Quantity Status</th>
    <th>Wholesale Price</th>
    <th>retail Price</th>
    <th>Total Wholesale Price</th>
    <th>Total retail Price</th>
    <?php view_dawa_pharmacist(); ?>
  </table>
  <div class="nafasi">

  <label>Total Amount Wholesale Price is <?php amounttotalwholesale(); ?> </label><br/>
  <label>Total Amount Retail Price is <?php amounttotalretail(); ?> </label>

<a href="pdf.php"><button id="pdf">Print PDF</button></a>
</div>
    </div>
      </div>

    ';
    

    
    }
    return $output
}

if (isset($_POST["print_pdf"])) {

    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("Export html table data to pdf 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->SetDefaultMonospaceFont('helvetica');
    $obj_pdf->SetFooterMargin(PDF_FOOTER_MARGIN);
    $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '5', PDF_MARGIN_RIGHT);
    $obj_pdf->SetPrintHeader(false);
    $obj_pdf->SetPrintFooter(false);
    $obj_pdf->SetAutoPageBreak(TRUE, 10);
    $obj_pdf->SetFont('helvetica', '', 12);

    $content='';

    $content .='
<div class="container-fluid">
  <div class="add-result1">
  <table>
    <th>No.</th>
    <th>DrugName</th>
    <th>Description</th>
    <th>Quantity Entered</th>
    <th>Quantity Status</th>
    <th>Wholesale Price</th>
    <th>retail Price</th>
    <th>Total Wholesale Price</th>
    <th>Total retail Price</th>
    <?php view_dawa_pharmacist(); ?>
  </table>
  <div class="nafasi">

  <label>Total Amount Wholesale Price is <?php amounttotalwholesale(); ?> </label><br/>
  <label>Total Amount Retail Price is <?php amounttotalretail(); ?> </label>

<a href="pdf.php"><button id="pdf">Print PDF</button></a>
</div>
    </div>
      </div>


    ';

    $content .= fetch_data();

    $content .= '</table>';

    $obj_pdf->writeHTML($content);

    $obj_pdf->Output("view_data.pdf", "I");
}

 ?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
      <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <!-- BOOTSTRAP STYLES-->
    <link href="/newpharmacy/assets/css/bootstrap.css" rel="stylesheet" />
     <!-- FONTAWESOME STYLES-->
    <link href="/newpharmacy/assets/font-awesome/css/font-awesome.css" rel="stylesheet" />
     <!-- MORRIS CHART STYLES-->
    <link href="/newpharmacy/assets/js/morris/morris-0.4.3.min.css" rel="stylesheet" />
        <!-- CUSTOM STYLES-->
    <link href="/newpharmacy/assets/css/custom.css" rel="stylesheet" />
     <!-- GOOGLE FONTS-->
   <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css' />
</head>
<body>
        <!-- /. NAV SIDE  -->
        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2>Stock Status</h2>   
                    </div>
                </div>              
                 <!-- /. ROW  -->
                  <hr />
                  <div class="container-fluid">
  <div class="add-result">
  <table>
    <th>No.</th>
    <th>DrugName</th>
    <th>Quantity Entered</th>
    <th>Quantity Status</th>
    <th>Wholesale Price</th>
    <th>retail Price</th>
    <th>Total Wholesale Price</th>
    <th>Total retail Price</th>
    <?php view_dawa(); ?>
  </table>
  <div class="nafasi">

  <label>Total Amount Wholesale Price is <?php amounttotalwholesale(); ?> </label><br/>
  <label>Total Amount Retail Price is <?php amounttotalretail(); ?> </label><br/>
</div>
            </div>    
         </div>    <!-- /. ROW  -->
         </div>
   
</body>
</html>

OK, and what happens when you run the code?

I’m a bit confused about some of the code - you seem to append a closing table tag where one isn’t necessary, for example, because there doesn’t seem to be an open table.

@droopsnoot it does nothing, and you must know that am new at php may be you can help me forexample on how to open that table.

OK, I didn’t know that - I am never quite sure whether things I think are missing are actually being done elsewhere in an advanced way that I don’t know anything about. You’ve got called to functions in there that are not shown in the post, so there may be other things that you haven’t shown. For example:

if (isset($_POST["print_pdf"])) {

the line above will never return true because there’s no form submission in your code, no form field called print_pdf, so the code inside that if() clause won’t run. Unless the form is in some other code.

So, does the code actually display what you want in the table you display towards the bottom of the code, coming from your view_dawa() function? Is it correct to presume that you want to display the information there, then if the user presses a “pdf” button, create a PDF containing the same information?

Another note - if you’re learning PDF at the moment, stop using the old mysql functions like mysql_query() and switch to a more modern library such as PDO. The old mysql functions are no longer part of the language. If you see tutorials and sample code that uses them, then find a newer tutorial.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.