【问题标题】:Throwing an exception named: "Unable to stream pdf: headers already sent" after all whitespaces removed在删除所有空格后引发名为“无法流式传输 pdf:标头已发送”的异常
【发布时间】:2017-05-26 15:18:16
【问题描述】:

我正在尝试通过将整个 HTML 复制到 textarea 中来将 HTML 转换为 PDF。我已经尝试了question 中显示的所有示例,但没有解决问题。为什么会这样?这是我的主要 php 文件:

<?php
use Dompdf\Dompdf;
require 'vendor/autoload.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    <title>PDF Converter</title>
</head>
<body>
    <div style="text-align: center;">
        <form method="POST">
            <div class="form-group">
                <label for="convert">Paste Your HTML Code here ; )
                    <hr>
                    <textarea name="htmlCode" class="form-control" rows="10" placeholder="<body>Example..</body>">asd</textarea>
                </label>
            </div>
            <input class="btn btn-primary" type="submit" value="Convert To PDF" name="convert">
        </form>
        <?php
            if (isset($_POST['convert'])) {
                $dompfd = new Dompdf();
                $html = htmlspecialchars($_POST['htmlCode']);
                $dompfd->loadHtml($html);
                $dompfd->setPaper('A4', 'landscape');
                $dompfd->render();
                $dompfd->stream();
            }
        ?>
    </div>
</body>
</html>

【问题讨论】:

    标签: php html exception


    【解决方案1】:

    您输出 HTML,因此标题发送说它是 text/html。

    将你的 PHP 移动到所有 HTML 之上的脚本顶部,它应该可以工作了!

    退出();在调用 stream() 方法之后。

    【讨论】:

      猜你喜欢
      • 2013-10-31
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      • 2016-11-21
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 2016-06-17
      相关资源
      最近更新 更多