【问题标题】:How to create PDF files using PHP MySQL?如何使用 PHP MySQL 创建 PDF 文件?
【发布时间】:2011-07-18 06:10:36
【问题描述】:

我是 FPDF 新手。

但我正在尝试如下。如何使用 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);
?>

【问题讨论】:

标签: php html fpdf html2pdf


【解决方案1】:

我认为你应该考虑使用HTML2FPDF

this blog post底部有用法示例。

检查其他 SO 问题:https://stackoverflow.com/questions/910243/how-to-convert-an-html-to-pdf-in-php-using-fpdf-lib-1-6

【讨论】:

    【解决方案2】:

    将 HTML 转换为 PDF 的 PHP 代码对我来说可以正常工作。 以下代码转换网页并将生成的 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");
    

    去这里下载APIVisit

    【讨论】:

      猜你喜欢
      • 2011-08-27
      • 2010-11-15
      • 2011-06-07
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      • 1970-01-01
      相关资源
      最近更新 更多