如何使用 PHP MySQL 创建 PDF 文件?

发布于 2024-10-24 08:16:09 字数 900 浏览 0 评论 0原文

我是 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);
?>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清引 2024-10-31 08:16:09

我认为您应该考虑使用 HTML2FPDF

底部提供的使用示例 此博文

还要检查另一个问题:https://stackoverflow.com/questions/910243/how-to-convert-an-html-to-pdf-in-php-using-fpdf-lib-1-6

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

感受沵的脚步 2024-10-31 08:16:09

将 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");

转到此处下载 API 访问

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文