Getting TCPDF:Some data has already been output, can't send PDF file in Laravel 4

- 1 answer

Ad

I am using HTML2pDf convertor to generate pdf my code looks like this

    require_once(app_path().'/libs/html2pdf/html2pdf.class.php') ;
    $html2pdf = new HTML2PDF('P','A4','en',true,'UTF-8',array(0, 0, 0, 0));
    $html2pdf->pdf->SetDisplayMode('fullpage');
    $html2pdf->WriteHTML($html22);
    $html2pdf->Output($username.'_questionnaire.pdf');

I am getting following error

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

The function where I have this script looks like this

public function postExporttopdf(){
        $validator = Validator::make(Input::all(),array('delid'=>'required'));
        $event = Session::get('tempevenname');
        if($validator->passes()){

            $uid1 = Input::get('delid');
            $username = User::find(Input::get('delid'))->name;

            $page1data = Question::where('event','=',$event)->where('page','=',1)->orderBy('order')->with('choices')
            ->with(array('answers'=>function($query){
                    $query->where('user_id','=',Input::get('delid'));
            }))->get();

            $page2data = Question::where('event','=',$event)->where('page','=',2)->orderBy('order')->with('choices')
            ->with(array('answers'=>function($query){
                    $query->where('user_id','=',Input::get('delid'));
            }))->get();

            $page3data = Question::where('event','=',$event)->where('page','=',3)->orderBy('order')->with('choices')
            ->with(array('answers'=>function($query){
                    $query->where('user_id','=',Input::get('delid'));
            }))->get();

            $page4data = Question::where('event','=',$event)->where('page','=',4)->orderBy('order')->with('choices')
            ->with(array('answers'=>function($query){
                    $query->where('user_id','=',Input::get('delid'));
            }))->get();

                $html22 = View::make('reviewsendpdf')->with(array(

                                    'page1data'=>$page1data,

                                    'page2data'=>$page2data,

                                    'page3data'=>$page3data,

                                    'page4data'=>$page4data

                                    ));



            require_once(app_path().'/libs/html2pdf/html2pdf.class.php') ;
            $html2pdf = new HTML2PDF('P','A4','en',true,'UTF-8',array(0, 0, 0, 0));
            $html2pdf->pdf->SetDisplayMode('fullpage');
            $html2pdf->WriteHTML($html22);
            $html2pdf->Output($username.'_questionnaire.pdf');

        } else {
            return Redirect::to('admin/exporttopdf')->with('message','Select a user and event');
        }
    }
Ad

Answer

Ad

Probably something has already been written to the output buffer prior to streaming the pdf content.

Try to use ob_end_clean() to clean the output buffer just before the method invocation:

require_once(app_path().'/libs/html2pdf/html2pdf.class.php') ;
$html2pdf = new HTML2PDF('P','A4','en',true,'UTF-8',array(0, 0, 0, 0));
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->WriteHTML($html22);

ob_end_clean();

$html2pdf->Output($username.'_questionnaire.pdf');
Ad
source: stackoverflow.com
Ad