【问题标题】:DOMPDF not workingDOMPDF 不工作
【发布时间】:2020-05-18 19:04:15
【问题描述】:

我正在尝试从textarea 中获取一些 html 并将其转换为 pdf。我从https://github.com/dompdf/dompdf 下载了 DOMPDF 并编写了下面的代码。当我单击提交时,我收到此错误:“内部服务器错误”。 (我的虚拟主机没有告诉我它是哪一行) (这个文件的名字是test2.php)

<?php
if (isset($_POST['submit'])) {
$content = $_POST['content'];
if (empty($content)){
    $error = 'write something';
}
else {
    include_once( 'dompdf/dompdf_config.inc.php' );
    $dompdf = new DOMPDF();
    $dompdf->load_html($content);
    $dompdf->render();
    $dompdf->stream('example.pdf');
}
}


?>
<!DOCTYPE html>
<head>

</head>
<body>
<?php
if(isset($error)){
echo $error;
}
?>
<form method="post" action="test2.php">
<textarea name="content" id="content">hello world</textarea><br>
<input type="submit" name="submit" value='submit'>
</form>
</body>
</html>

【问题讨论】:

  • 你下载了dompdf,你也下载了php-font-lib吗?这个库必须使用最新的代码安装,但是从 github does not include it 下载 ZIP。我们计划更新我们的“夜间”下载,目前仍在从旧的 SVN 存储库中提取。
  • @BrianS 谢谢,但我应该如何将该库插入到我的代码中?我不太擅长这些东西
  • 您可以从 php-font-lib 项目页面下载 ZIP(与使用 dompdf 相同)。然后将文件解压到 dompdf/lib/php-font-lib 的 dompdf 安装中。如果您仍然遇到问题,您可以download the last released version from Google Code
  • @BrianS 成功了!非常感谢!

标签: html pdf dompdf


【解决方案1】:

我为解决这个问题苦苦挣扎了一个多月。最后我解决了。解决方法如下。

<?php
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
?>
<html>
  <head></head>
  <body>
    <h1>Sucess</h1>
  </body>
</html>
<?php

$html = ob_get_clean();
$dompdf = new DOMPDF();
$dompdf->setPaper('A4', 'portrait');
//$dompdf->setPaper('A4', 'landscape');
$dompdf->load_html($html);
$dompdf->render();
//For view
$dompdf->stream("",array("Attachment" => false));
// for download
//$dompdf->stream("sample.pdf");

?>

【讨论】:

    【解决方案2】:

    在将 DOMPDF 用于项目时,我在客户端的服务器上遇到了类似的问题。

    您的 PHP 安装可能没有正确配置错误报告级别。

    在脚本的顶部放置以下内容; error_reporting(E_ALL);

    例子:

    error_reporting(E_ALL);
    if (isset($_POST['submit'])) {
    $content = $_POST['content'];
    if (empty($content)){
        $error = 'write something';
    }
    else {
        include_once( 'dompdf/dompdf_config.inc.php' );
        $dompdf = new DOMPDF();
        $dompdf->load_html($content);
        $dompdf->render();
        $dompdf->stream('example.pdf');
    }
    }
    

    您现在应该会看到有关收到的错误类型的更详细消息。

    您传递给 $dompdf-&gt;load_html($content); 方法的 HTML 标记可能存在问题,或者您可能遇到与内存相关的问题(超出内存限额)。

    通常这些错误会自行报告,但同样取决于您的设置,报告可能会受到限制。

    【讨论】:

    • 我把错误信息放在了顶部,但我的虚拟主机仍然收到同样的错误信息“内部服务器错误”
    猜你喜欢
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    • 2021-04-11
    • 2017-09-27
    • 2018-11-01
    相关资源
    最近更新 更多