获取TCPDF:某些数据已经输出,无法在Laravel 4中发送PDF文件

时间:2015-12-17 10:49:21

标签: php laravel html2pdf laravel-4.2

我正在使用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');
        }
    }

1 个答案:

答案 0 :(得分:1)

在流式传输pdf内容之前,可能已经将某些内容写入输出缓冲区

尝试在方法调用之前使用ob_end_clean清除outpput缓冲区:

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