【问题标题】:Converting Dynamic PHP output to PDF [closed]将动态 PHP 输出转换为 PDF [关闭]
【发布时间】:2015-07-24 19:06:24
【问题描述】:

我想将我的动态 PHP 输出转换为 PDF,我一直在寻找答案,但找不到有用的答案。我使用过 mPDF,但它对我没有多大用处。

【问题讨论】:

  • 您可以将 php 的输出缓冲能力与 PDF 输出库结合起来

标签: php pdf-generation


【解决方案1】:

使用dompdf,可以从here下载。

将您的输出存储在一些变量中并使用以下代码

用法

require_once 'dompdf/dompdf_config.inc.php';
$html = "<html><body>". $dynamic_output_variable ."
</body></html>";
$pdf = new DOMPDF();
$pdf->load_html($html);
$pdf->render();
$output = $pdf->output();
file_put_contents('output folder/file name.pdf', $output);
$pdf->stream('file name.pdf', array('Attachment' => 0));

【讨论】:

  • 非常感谢您的帮助:)
【解决方案2】:

您可以使用 DomPdf 库。您可以使用 URL 下载该库的最新版本:https://code.google.com/p/dompdf/downloads/detail?name=dompdf_0-6-0_beta3.tar.gz&can=2&q=

这个最新版本支持多种字体。

使用以下代码包含库:

require_once("/dompdf/dompdf_config.inc.php");

在浏览器中查看pdf输出:

$html = "Your dynamic php output";
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$preview = $dompdf->stream("dompdf.pdf", array('Attachment' => 0));
echo $preview;die;

使用以下代码将 PDF 作为字符串获取:

$pdf = $dompdf->output();

您可以使用以下代码使其可下载:

$filepath//full path of your temporary/public location;
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
file_put_contents($filepath, $pdf); // save the pdf file on server
header("Content-type: application/pdf");
header("Content-Disposition: 'attachment'; filename='dompdf.pdf'");
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
readfile($filepath);
exit();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-16
    • 2013-08-09
    • 2013-12-26
    • 2012-02-05
    • 2015-08-02
    • 2019-10-26
    • 2010-10-31
    • 2012-11-29
    相关资源
    最近更新 更多