2011-03-18 45 views
1

我是FPDF的新手。如何使用PHP MySQL创建PDF文件?

但我想下面。如何使用FPDF从HTML标签创建PDF?下面

<?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(); 
?> 

是我的PHP程序

<?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); 
?> 
+2

[How to create a PDF file with PHP?](http://stackoverflow.com/questions/4666171/how-to-create-a-pdf-file-with-php) – 2011-03-18 15:12:51

+0

因为你是对HTML感到舒服,您可能需要考虑[HTML to PDF转换器列表](http://stackoverflow.com/q/3178448/264628)。它不仅涵盖了基于PHP的解决方案,还列出了各种选项。 – BrianS 2011-03-18 18:05:59

回答

0

PHP代码的HTML转换为PDF工作正常,在我结束。 下面的代码转换网页和生成的PDF发送到浏览器:

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; 

您还可以将原始的HTML代码,只需使用convertHtml()方法,而不是convertURI():

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

的API还允许你转换本地HTML文件:

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

去这里下载API Visit