John Davidson

html - PHP table data convert to downloadable PDF --- dat doesn't pass to table?

0 comments
Message:


All data display in the table when selecting the parameter and post it from the form. But when data convert to pdf, data value doesn't pass to pdf table. But if I remove $Parameter value from


$sql = "SELECT 
id,
sensor,
location,
Parameter,
Value,
Reading_Time
FROM readings
WHERE Parameter='$Parameter'
ORDER BY id DESC LIMIT 10";

Here and set Parameter ='Temperature' or any other variable. Code works fine.
I can't find why data doesn't pass to pdf file?
Can anyone help me with this?



<html>

<head>
<title>SoftAOX | Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
</head>

<body>

<form id="s" method="post">
<br><label>Parameter</label><br>
<select name="parameter">
<option value="">None</option>
<option value="Rain">Rain</option>
<option value="Temperature">Temperature</option>
<option value="Humidity">Humidity</option>
<option value="Pressure">Pressure</option>

</select>
<input type="submit" name="Submit" value="Send">
</form>

<?php

$type="";
if(isset($_POST['parameter'])) {
//echo "selected parameter: ".$_POST['parameter'];
$type=$_POST['parameter']; }

function fetch_data() {
global $type;
$output = '';
$conn = mysqli_connect("localhost", "root", "", "example_esp_data");
$Parameter=$type;
$sql = "SELECT id, sensor, location, Parameter, Value, Reading_Time FROM readings WHERE Parameter='$Parameter' ORDER BY id DESC LIMIT 10";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result))
{
$t = $row["Reading_Time"]+19800;
$row_Time = date("j-m-Y H:i:s",$t);
$output .= '<tr>
<td>'.$row["sensor"].'</td>
<td>'.$row["location"].'</td>
<td>'.$row["Parameter"].'</td>
<td>'.$row["Value"].'</td>
<td>'.$row_Time .'</td>
</tr>
';
}

return $output;

}

if(isset($_POST["generate_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("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>Sensor</th>
<th>Location</th>
<th>Parameter</th>
<th>Value</th>
<th>Reading_Time</th>
</tr>
';
$content .= fetch_data();
$content .= '</table>';
$obj_pdf->writeHTML($content);
ob_end_clean();
$obj_pdf->Output('file.pdf', 'I');

}
?>



<br />
<div class="container">
<h4 align="center"> <?php echo $Parameter .""."Weather Data" ; ?></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="Download" />
</form>
</div>
<br />
<br />
<table class="table table-bordered">
<tr>
<th>Sensor</th>
<th>Location</th>
<th>Parameter</th>
<th>Value</th>
<th>Reading_Time</th>
</tr>
<?php
echo fetch_data();

?>
</table>
</div>
</div>
</body>

</html>






<?php ini_set("display_errors", 1);
ini_set("track_errors", 1);
ini_set("html_errors", 1);
error_reporting(E_ALL);

require './Database.php';
?>


My Answer:
To convert HTML table data to a downloadable PDF using PHP, you can use a library like TCPDF or FPDF. Here's an example using TCPDF:

1. First, you need to create an HTML table with the data you want to convert to PDF. For example:

html













Name Email
John Doe john.doe@example.com
Jane Smith jane.smith@example.com



2. Next, you can use TCPDF to convert this HTML table to a PDF file. Here's an example PHP code:

php
require_once('tcpdf/tcpdf.php');

$pdf = new TCPDF();
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();
$pdf->writeHTML('

Table Data

');
$pdf->writeHTML('
NameEmail
John Doejohn.doe@example.com
Jane Smithjane.smith@example.com
');

$pdf->Output('table_data.pdf', 'D');


3. Save this PHP code in a file (e.g., convert.php) and place it in the same directory as the TCPDF library.

4. When you run this PHP script in your browser, it will generate a PDF file with the table data and prompt the user to download it.

Note: Make sure to adjust the HTML table data and styling as needed for your specific use case. You can also customize the TCPDF settings to change the appearance of the PDF file.

Rate this post

4 of 5 based on 9005 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us