1

I am new to FPDF.

But i am trying like below. How can I create a PDF from HTML tags using FPDF?

<?php
    require('fpdf.php');
    include (connection.php)
    $result = mysql_query("SELECT * FROM emp");

    <!--- I don't know how to add the html tag here -->

    $pdf=new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Output();
 ?>

below is my PHP program

<?php
 include (connection.php)

$result = mysql_query("SELECT * FROM emp");
?>
<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<?php
while($row = mysql_fetch_array($result))
  {
?>
  <tr>
  <td> <?php print $row['FirstName']; ?></td>
  <td> <?php print $row['LastName']; ?></td>
  </tr>
<?php
  }
?>
</table>
<?php
mysql_close($con);
?>
Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
abi
  • 19
  • 1
  • 2
  • 2
    possible duplicate of [How to create a PDF file with PHP?](http://stackoverflow.com/questions/4666171/how-to-create-a-pdf-file-with-php) – Mark Baker Mar 18 '11 at 15:12
  • Since you're comfortable with HTML you might want to consider the [List of HTML to PDF converters](http://stackoverflow.com/q/3178448/264628). It covers more than just PHP-based solutions, but it lists a variety of options. – BrianS Mar 18 '11 at 18:05

2 Answers2

2

I think you should consider to use HTML2FPDF

Usage example available at the bottom of this blog post.

Check as well this other SO question: https://stackoverflow.com/questions/910243/how-to-convert-an-html-to-pdf-in-php-using-fpdf-lib-1-6

Community
  • 1
  • 1
Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
0

PHP code to convert HTML to PDF works fine on my end. The following code converts a web page and sends the generated PDF to the browser:

require 'pdfcrowd.php';

// create an API client instance
$client = new Pdfcrowd("username", "apikey");

// convert a web page and store the generated PDF into a variable
$pdf = $client->convertURI('http://www.google.com/');

// set HTTP response headers
header("Content-Type: application/pdf");
header("Cache-Control: max-age=0");
header("Accept-Ranges: none");
header("Content-Disposition: attachment; filename=\"google_com.pdf\"");

// send the generated PDF 
echo $pdf;

You can also convert raw HTML code, just use the convertHtml() method instead of convertURI():

$pdf = $client->convertHtml("<body>My HTML Layout</body>");

The API lets you also convert a local HTML file:

$pdf = $client->convertFile("/path/to/MyLayout.html");

go here for download API Visit