Jump to content
mahenda

show me a way to convert mysql table to pdf or text document when user click download button mysql procedural

Recommended Posts

create table mimi (mimiId int(11) not null, mimiBody varchar(255) );
<?php
//connecting to database
include_once ('conn.php');
$sql ="SELECT mimiId, mimiBody FROM mimi";

$result = mysqli_query($conn, $sql );

$mimi = mysqli_fetch_assoc($result);

$mimiId ='<span>No: '.$mimi['mimiId'].'</span>';

$mimiBody ='<p class="leading text-justify">'.$mimi['mimiBody'].'</p>';
?>
//what is next?

i want to download pdf or text document after clicking button or link how to do that

Share this post


Link to post
Share on other sites

As you put your data inside HTML tags I assue that by text file you mean an html text file. (Neither plain text nor PDF use html tags.

The easier option is the text one. Add this code

$mycontent = <<<TEXT
<!DOCTYPE=html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample output</title>
</head>
<body>
    $mimiId
    $mimiBody
</body>
</html>
TEXT;

header('Content-Type: text/html');
header('Content-Disposition: attachment; filename="mytest.html" ');
header('Pragma: no-cache');
header('Expires: 0');
echo $mycontent;

if you want pdf, that is more complicated. I suggest you visit the FPDF site. Lots of example there.

Share this post


Link to post
Share on other sites
7 minutes ago, Barand said:

As you put your data inside HTML tags I assue that by text file you mean an html text file. (Neither plain text nor PDF use html tags.

The easier option is the text one. Add this code


$mycontent = <<<TEXT
<!DOCTYPE=html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample output</title>
</head>
<body>
    $mimiId
    $mimiBody
</body>
</html>
TEXT;

header('Content-Type: text/html');
header('Content-Disposition: attachment; filename="mytest.html" ');
header('Pragma: no-cache');
header('Expires: 0');
echo $mycontent;

if you want pdf, that is more complicated. I suggest you visit the FPDF site. Lots of example there.

thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.