shruti A
asked on
generate PDF for retrieved using php
<?php include("db.php");?>
<!DOCTYPE html>
<html>
<head>
</HEAD>
<BODY>
<div id="product-grid">
<div class="txt-heading">Products <a href="docs.php" id="btnEmpty">Back</a></div>
<?php
$doc_array=array();
$doc_array="SELECT * FROM docs ";
$agg_array="SELECT * FROM Ministore_aggrement ";
$store_array="SELECT * FROM Office_images ";
$result = mysqli_query($con,$doc_array);
$result1 = mysqli_query($con,$agg_array);
$result2 = mysqli_query($con,$store_array);
?>
<table border="2">
<tr>
<th>AADHAR CARD</th>
<th>PAN CARD</th>
<th>CANCELLED CHEQUE</th>
<th>PASSBOOK</th>
<th>GST</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><img src="<?php echo $row["adhar"]; ?>" width="50%" height="30%"></td>
<td><img src="<?php echo $row["pan"]; ?>" width="50%" height="30%"></td>
<td><img src="<?php echo $row["canceled"];?>" width="50%" height="30%"></td>
<td><img src="<?php echo $row["passbook"]; ?>" width="50%" height="30%"></td>
<td><img src="<?php echo $row["gst"]; ?>" width="50%" height="30%"></td>
</tr>
<?php
}
?>
</table>
<table border="2">
<tr>
<th>Date</th>
<th>Proprietory</th>
<th>Firm</th>
<th>Place</th>
<th>Signatory</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result1))
{
echo'<tr>';
echo'<td>'.$row["date"].'</td>';
echo'<td>'.$row["Proprietor_name"].'</td>';
echo'<td>'.$row["Firm_name"].'</td>';
echo'<td>'.$row["Place"].'</td>';
echo'<td>'.$row["Signatory_name"].'</td>';
echo'</tr>';
echo '</table>';
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------
?>
<table border="2">
<tr>
<th>Image1</th>
<th>Image2</th>
<th>Image3</th>
<th>Image4</th>
<th>Image5</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result2))
{
?>
<tr>
<td><img src="<?php echo $row["img1"]; ?>" width="50%" height="30%"></td>
<td><img src="<?php echo $row["img2"]; ?>" width="50%" height="30%"></td>
<td><img src="<?php echo $row["img3"];?>" width="50%" height="30%"></td>
<td><img src="<?php echo $row["img4"]; ?>" width="50%" height="30%"></td>
<td><img src="<?php echo $row["img5"]; ?>" width="50%" height="30%"></td>
</tr>
<?php
}
?>
</table>
<?php
$sql = "SELECT Firm_name FROM Ministore_aggrement";
$result = mysqli_query($con,$sql);
$row= mysqli_fetch_array($result);
$Firm_name=trim($row['Firm_name']);
$apikey = '436d5c9d-5f31-4408-a4ff-a7f62c1544e8';
$value = 'http://www.google.com'; // a url starting with http or an HTML string. see example #5 if you have a long HTML string
$target_dir2 = "doc/pdfs/".$Firm_name.".pdf";
$result = file_get_contents("http://api.html2pdfrocket.com/pdf?apikey=" . urlencode($apikey) . "&value=" . urlencode($value));
file_put_contents($target_dir2,$result);
?>
</div>
</BODY>
</HTML>
here i'm retrieving data from tables and displaying data on screen so I want to generate pdf for that data so i have used API but its generating blank pdf so please me help to generate pdf with retrived data.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I am getting a PDF file with Google.com home page when using this code:
Are you sure that the $target_dir2 variable and the $Firm_name variables are correct? file_put_contents() fails if you try to put a file in a directory that doesn't exist. This creates the directory if it doesn't exist and puts the file in it.
Are you trying to print the current page?
<?php
$apikey = '436d5c9d-5f31-4408-a4ff-a7f62c1544e8';
$value = 'http://www.google.com'; // a url starting with http or an HTML string. see example #5 if you have a long HTML string
$target_dir2 = __DIR__."/eddie.pdf";
$result = file_get_contents("http://api.html2pdfrocket.com/pdf?apikey=" . urlencode($apikey) . "&value=" . urlencode($value));
file_put_contents($target_dir2,$result);
Are you sure that the $target_dir2 variable and the $Firm_name variables are correct? file_put_contents() fails if you try to put a file in a directory that doesn't exist. This creates the directory if it doesn't exist and puts the file in it.
function file_force_contents($dir, $contents){
$parts = explode('/', $dir);
$file = array_pop($parts);
$dir = '';
foreach($parts as $part) {
if(!is_dir($dir .= "/$part")) {
mkdir($dir);
}
}
file_put_contents("$dir/$file", $contents);
}
// Here's how to change your code to use this function:
$value = "http://google.com";
$target_dir2 = "doc/pdfs/".$Firm_name.".pdf";
$result = file_get_contents("http://api.html2pdfrocket.com/pdf?apikey=" . urlencode($apikey) . "&value=" . urlencode($value));
file_force_contents($target_dir2,$result);
Are you trying to print the current page?
ASKER
yes i'm trying to print current page
ASKER
pdf is generating with 0 bytes its not taking data so what should i do
I'll look at it later today
do you think you could post some dummy data, or at least the structures of your tables (docs, Ministore_aggrement, & Office_images)?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No replies from OP.
ASKER