获取TCPDF:一些数据已经输出,可以';t在Laravel 4中发送PDF文件


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

我使用HTML2pDf转换器生成pdf,我的代码看起来像这个

    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');

我得到以下错误

TCPDF错误:一些数据已经输出,无法发送PDF文件

我有这个脚本的函数看起来像这个

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');
        }
    }

在流式传输pdf内容之前,可能已经向输出缓冲区写入了一些内容。

尝试在方法调用之前使用ob_end_clean()清理输出缓冲区:

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');