I'm trying to use dompdf on my plugin to create a save as pdf functionality,but I'm still getting
Unable to stream pdf: headers already sent
.
I've done some research and the solutions won't work. I've also use exit()
as one of the answers suggest but it doesn't work.
Here is my current code
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;
your wordpress already sent some data to the browser, and when any data is sent, the headers are sent as well.
I would suggest you to use buffers (ob_start, ob_end_flush, ...) but it's on a general purpose, I do not know if it is appliable in your case, i'm not really working with wordpress structure.