无法流式传输 pdf:headers 使用 wordpress 时已发送

Unable to stream pdf: headers already sent when using wordpress

我正在尝试在我的插件上使用 dompdf 来创建另存为 pdf 功能,但我仍然 Unable to stream pdf: headers already sent.

我做了一些研究,但解决方案不起作用。我也使用 exit() 作为建议的答案之一,但它不起作用。

这是我当前的代码

include(WPtest_PLUGIN_PATH.'/admin/includes/dompdf/lib/html5lib/Parser.php');
include(WPtest_PLUGIN_PATH.'/admin/includes/dompdf/lib/php-font-lib/src/FontLib/Autoloader.php');
include(WPtest_PLUGIN_PATH.'/admin/includes/dompdf/lib/php-svg-lib/src/autoload.php');
include(WPtest_PLUGIN_PATH.'/admin/includes/dompdf/src/Autoloader.php');
Dompdf\Autoloader::register();

use Dompdf\Dompdf;

class WPtest_Save_PDF{

    //use Dompdf\Dompdf;
    function __construct(){
        add_shortcode( 'save_me',array($this,'print_callback'));
    }       


    function print_callback(){

        if(isset($_GET['print']))
        {


            $dompdf = new Dompdf();

            $dompdf->loadHtml("test");

            // (Optional) Setup the paper size and orientation
            $dompdf->setPaper('A4', 'landscape');

            // Render the HTML as PDF
            $dompdf->render();

            // Output the generated PDF to Browser
            $dompdf->stream();

            exit();
        }

    }
}

new WPtest_Save_PDF;

您的 wordpress 已经向浏览器发送了一些数据,并且在发送任何数据时,也会发送 headers。

我建议你使用缓冲区 (ob_start, ob_end_flush, ...) 但它是通用的,我不知道它是否适用于你的情况,我'我并没有真正使用 wordpress 结构。