WordPress plugin HTML2PDF headers already sent

I’m currently trying to use HTML2PDF (PHP Helper) to generate a HTML PDF with a Plugin. The Plugin is just a simple shortcode, to activate it. However, When i the function is activated (shortcode detected) I get an headers already sent message. I do understand that this means PHP has already set the header information.

But how can i fix this? so the plugin will generate an PDF.

Read More

Plugin code

require_once('html2pdf/html2pdf.class.php');

add_shortcode('generate_pdf', 'pdf_saving');

function pdf_saving()
{

    $content_html = "<h1>Hello</h1>";
    $html2pdf = new HTML2PDF('P', 'A4', 'en');
    $html2pdf->pdf->SetDisplayMode('fullpage');
    $html2pdf->writeHTML($content_html);
    // ob_clean(); I've also tried this.
    $html2pdf->Output('file.pdf');

}

Error message,

TCPDF ERROR: Some data has already been output, can’t send PDF file

Related posts

1 comment

  1. You try below code

    function pdf_saving(){
    try {
    ob_start(); 
    
    
    
                include(plugin_dir_path( __FILE__ ) .'html2pdf/html2pdf.class.php');
                $html2pdf = new HTML2PDF('P','A4','en');
    
                $html2pdf->WriteHTML("Test Content");
    
                $html2pdf->output('filename.pdf', 'D');
    
    }catch (Html2PdfException $e) {
                $html2pdf->clean();
                $formatter = new ExceptionFormatter($e);
    
    }
    }
    

Comments are closed.