Reputation: 23
Im currently developing a system that can load csv file to mysql db, generate qr code using the saved data in mysql and display all the saved qr codes in a tabular format in my web page. I am able to do all the above, but I would like to know how I would go about converting that tabular fomatted data to a pdf file by the click of a button. This is my code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
include("qrlib.php");
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//if(isset($_POST['submit'])){
$sql = "SELECT * FROM user";
$result = $conn->query($sql);
echo "<form><table border='1'>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//echo "name: " . $row["name"]. " - email: " . $row["email"]. " " . $row["phone"]. "<br>" ;
//set it to writable location, a place for temp generated PNG files
//$PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;
$PNG_TEMP_DIR = "qrcodes";
//html PNG location prefix
$PNG_WEB_DIR = 'temp/';
//include "pdf.php";
//ofcourse we need rights to create temp dir
if (!file_exists($PNG_TEMP_DIR))
mkdir($PNG_TEMP_DIR);
$filename = $PNG_TEMP_DIR.$row["name"].'.png';
//processing form input
//remember to sanitize user input in real-life solution !!!
$errorCorrectionLevel = 'L';
$matrixPointSize = 4;
// user data
$filename = $PNG_TEMP_DIR.$row["name"].'.png';
echo $filename;
QRcode::png($row["name"], $filename, $errorCorrectionLevel, $matrixPointSize, 2);
echo "<tr><td>
<img src='$filename'></td></tr>";
}
} else {
echo "0 results";
}
echo "
</table>
</form>";
//}
$conn->close();
?>
Upvotes: 1
Views: 5747
Reputation: 167
You can use the php library: tcpdf It can generate QR codes and then put them in PDF file in your desired format.
This is the link of an example using it: https://tcpdf.org/examples/example_050/
Upvotes: 1