php - 使用 TCPDF 获取 'Headers already sent'

标签 php html pdf tcpdf

编辑 2:问题没有解决;我通过切换到使用 ezpdf 而不是 TCPDF 的旧版本文件找到了解决方法,并对其进行了更改以获得所需的打印输出(我不知道为什么整个网站中只有一个页面更改为 TCPDF,但显然它发生了)。我仍然想知道是否有解决此问题的方法,因为 TCPDF 的代码在运行 php 5.2 的服务器上运行正常,但在新服务器上运行不正常。但是,它不再是主要优先事项。

提交 PDF 请求后,我什么也没得到;只是一个空白页。这是从另一台服务器获取的所有代码(我们正在将 php 版本更新到 5.3)并且代码在第一台服务器上运行良好。

Warning: Cannot modify header information - headers already sent (output started at filepath in path/tcpdf.php) TCPDF ERROR: Some data has already been output to browser, can't send PDF file

经过一些调试,我注意到问题是“headers already sent error”, 我在更新此站点时遇到过几次。大多数这些错误都可以使用 meta http-equiv 修复,因为它们大多只是页面刷新。但是,TCPDF 使用 header 制作 PDF 文件并强制下载。

如何在不需要 header 的情况下使案例“D”正常工作,如果无法完成,是否可以将案例“I”改为在没有 header 的情况下工作?

我尝试在 $pdf->output() 之前使用 ob_clean()。我还尝试删除 require_once('./common/tcpdf/config/lang/eng.php')。这两个都对其他人有固定的问题,我已经广泛研究了这个话题。我不认为它在这种情况下会起作用,因为我知道该网站此时已经开始输出。

我想要的是在下面的代码中使用 header (meta http-equiv?)的替代方法,以防出现“D”(首选)或“I”(可行),这将给我相同的结果(pdf下载或在新选项卡或窗口中打开 pdf)。当然,除非有另一种修复方法。如果没有,我将不得不在不使用 TCPDF 的情况下重新编写页面。

编辑:在原始服务器上,“生成 pdf”会导致页面重新加载,然后在长时间等待(查询数据库时)后,它会强制用户下载。在新服务器上,页面被重新加载,等待查询的时间很长,然后菜单实际上中断了(注销被删除,另一个按钮被移到菜单栏其余部分的下方)并且没有其他任何事情发生。

取自 tcpdf.php 的代码:

case 'I': {
                // Send PDF to the standard output
                echo "<script>console.log('some log');</script>";
                if (ob_get_contents()) {
                    $this->Error('Some data has already been output, can\'t send PDF file');
                }
                if (php_sapi_name() != 'cli') {
                    //We send to a browser
                    header('Content-Type: application/pdf');
                    if (headers_sent()) {
                        $this->Error('Some data has already been output to browser, can\'t send PDF file');
                    }
                    header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
                    header('Pragma: public');
                    header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
                    header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');  
                    header('Content-Length: '.$this->bufferlen);
                    header('Content-Disposition: inline; filename="'.basename($name).'";');
                }
                echo $this->getBuffer();
                break;
            }
                case 'D': {
                    // Download PDF as file
                    if (ob_get_contents()) {
                        $this->Error('Some data has already been output, can\'t send PDF file');
                    }
                    header('Content-Description: File Transfer');
                    if (headers_sent()) {

                        $this->Error('Some data has already been output to browser, can\'t send PDF file');
                    }
                    header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
                    header('Pragma: public');
                    header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
                    header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
                    // force download dialog
                    header('Content-Type: application/force-download');
                    header('Content-Type: application/octet-stream', false);
                    header('Content-Type: application/download', false);
                    header('Content-Type: application/pdf', false);
                    // use the Content-Disposition header to supply a recommended filename
                    header('Content-Disposition: attachment; filename="'.basename($name).'";');
                    header('Content-Transfer-Encoding: binary');
                    header('Content-Length: '.$this->bufferlen);
                    echo $this->getBuffer();
                    break;
                }

最佳答案

我在网上看到很多关于使用 ob_start() 的引用,在文件末尾使用 ob_end_flush()。 也许这在某些情况下有效,尽管我真的不明白它会如何。不反对 - 只是说我不明白,而且我没有找到解决问题的办法。

但是,我发现在文件顶部使用 ob_start() 并在输出之前使用 ob_end_clean() 应该可以解决问题。

<?PHP
    ob_start();
    // All other content
    ob_end_clean();
    $pdf->Output('MyPDF_File.pdf', 'D');
?>  

关于php - 使用 TCPDF 获取 'Headers already sent',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24068529/

相关文章:

Android打开失败: EACCES (Permission denied) in implementing PDF Reader library

c++ - 使用 PoDoFo 访问/差异数组

php - in_array 检查非假值

php - 如何更改Server字符集和Db字符集?

javascript - 基于范围 slider 值从底部淡入的背景

html - 重复的工具提示修复?

php - 在 PHP 字符串中查找 youtube 链接并将其转换为嵌入代码?

php - 为什么我的 PDO 对象返回对象(PDO)[2]

html - Firefox 中不显示绝对居中的图像

python-3.x - 使用 python 提取 pdf 表格中包含的文本的最佳方法是什么?