【问题标题】:loading dynamic html into dompdf using php使用 php 将动态 html 加载到 dompdf
【发布时间】:2014-06-01 07:46:38
【问题描述】:

您好,我正在编写一个脚本,该脚本将使用file_get_contents 动态加载 html,并将生成并保存在服务器上的 pdf,但不知何故,我的 php 文件给了我 500 内部服务器错误 所以有没有其他方法可以将 html 加载到 dompdf 这是我的代码

<?php
error_reporting(E_ALL);

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');
require_once("dompdf_config.inc.php");

$mvouch = $_REQUEST["vouchno"];
$dompdf = new DOMPDF();
$html =file_get_contents("http://www.myurl.com/project/file_to_be_loaded.php?vouchno=$mvouch");
$dompdf->load_html($html);
$dompdf->render();
$output = $dompdf->output();
file_put_contents("/files/report.pdf", $output);
echo "<h1>DONE!!!</h1>";
?>

【问题讨论】:

  • 您可以访问 wkhtmltopdf 吗?我发现它是从 HTML 创建 PDF 的更好的库和工具。如果要解决 500 错误,则需要查看 php error_log 文件(linux:/var/log/apache2/error_log 或 /var/log/nginx/error_log)以查看处理器抛出的内容消息。
  • 另外,include & (include ('includes...');
  • 它告诉我这个错误Fatal error: Class 'DOMDocument' not found in /home/cepheisys/public_html/sbsweb/include/dompdf.cls.php on line 629
  • 这意味着你没有安装 libxml 和 XML php 库。在手机上所以无法链接,但谷歌'php install xml'
  • 正如贾斯汀所说,您缺少 DOMDocument,这是一个核心 PHP 扩展。它默认启用,但一些托管服务提供商明确禁用它。见php.net/manual/en/dom.setup.php

标签: php html mysql dynamic-data dompdf


【解决方案1】:

应该这样做!

<?php
error_reporting(E_ALL);

require('includes/check_auth_user.php');
require('includes/dbconnect.php');
include ('includes/function.php');
require_once("dompdf_config.inc.php");

$dompdf = new DOMPDF();

require_once('file_to_be_loaded.php?vouchno='.$mvouch');

$dompdf->load_html($html);
$dompdf->render();
$output = $dompdf->output();
file_put_contents('/files/report.pdf', $output);
echo '<h1>DONE!!!</h1>';
?>

也许您还想在报告文件中使用动态文件名并按年/月对文件夹进行排序

$month  = date('Ym');
$date= Date("Y-m-d");
$time = Date("His");
$filename = 'files/'.$month.'/report'.$date.'_'.$time.'.pdf';
        if (!is_dir("files/".$month)) {
          // dir doesn't exist, make it
          mkdir("files/".$month);
}
file_put_contents($filename, $output);

file_to_be_loaded.php - 应该是这样的

$html = '
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <html lang="sv">
</head>
<body>';
$html .='<h2>Report</h2>';
$html .='
</body>
</html>';

【讨论】:

    【解决方案2】:

    如果你使用centos作为服务器,这是Fatal error: Class 'DOMDocument' not found in /var/www/html/include/dompdf.cls.php on line 177的解决方案:

    要安装 PHP-XML,请运行以下命令:yum install php-xml

    要安装 DOM/XML,运行以下命令:yum whatprovides php-dom

    要重新启动 Apache,请运行以下命令:service httpd restart

    【讨论】:

      猜你喜欢
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      • 1970-01-01
      相关资源
      最近更新 更多