我正在尝试使用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;
发布于 2017-12-27 05:56:44
wordpress已经向浏览器发送了一些数据,并且当任何数据被发送时,标头也会被发送。
我建议你使用缓冲区(ob_start,ob_end_flush,.)但是它是通用的,我不知道它在你的情况下是否适用,我并不是真的在使用wordpress结构。
https://stackoverflow.com/questions/47987011
复制相似问题