首页 文章

使用如此多的数据在Laravel中导出PDF

提问于
浏览
2

我有下一个代码

$html = '';
foreach ($posts as $post) {
        $html .= '<div class="table-scrollable">
                        <table id="posts" class="table table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th>Id</th>
                                    <th>Name</th>
                                    <th>Title</th>
                                </tr>
                            </thead>
                            <tbody id="body"><tr>
                                <td>'
                . $post->id . ' 
                                </td>
                                <td>' .
                $post->name .
                '</td>
                                <td>'
                . $post->title .
                '</td> 
                    </tr>
                   </tbody>
                </table>
            </div>';
}
return PDF::load($html, 'A4', 'portrait')->download('my_pdf');

问题是,当我想下载如此多的数据时,我得到下一个错误 The localhost page isn’t working localhost is currently unable to handle this request. 但是如果我尝试下载例如20条目,则工作正常 . 我能做什么?

laravel.log

[2016-11-24 13:15:17] production.ERROR:异常'ErrorException',在/Applications/XAMPP/xamppfiles/htdocs/dicom/app/controllers/IncasariController.php:58中显示消息'未定义变量:data_start'堆栈跟踪:0 /Applications/XAMPP/xamppfiles/htdocs/dicom/app/controllers/IncasariController.php(58):Illuminate \ Exception \ Handler-> handleError(8,'Undefined varia ...','/ Applications / X ...',58,数组)1 [内部函数]:IncasariController-> filtrareChitante()2 /Applications/XAMPP/xamppfiles/htdocs/dicom/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(231 ):call_user_func_array(Array,Array)3 /Applications/XAMPP/xamppfiles/htdocs/dicom/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(93):Illuminate \ Routing \ Controller-> callAction('filtrareChitant) ...',Array)4 /Applications/XAMPP/xamppfiles/htdocs/dicom/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(62):Illuminate \ Routing \ ControllerDispatcher-> call(Object(IncasariC) ontroller),Object(Illuminate \ Routing \ Route),'filtrareChitant ...')5 /Applications/XAMPP/xamppfiles/htdocs/dicom/vendor/laravel/framework/src/Illuminate/Routing/Router.php(962): Illuminate \ Routing \ ControllerDispatcher-> dispatch(Object(Illuminate \ Routing \ Route),Object(Illuminate \ Http \ Request),'IncasariControl ...','filtrareChitant ...')6 [内部功能]:照亮\路由\ Router-> Illuminate \ Routing ()7 /Applications/XAMPP/xamppfiles/htdocs/dicom/vendor/laravel/framework/src/Illuminate/Routing/Route.php(109):call_user_func_array(Object(Closure),数组)8 /Applications/XAMPP/xamppfiles/htdocs/dicom/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1028):Illuminate \ Routing \ Route-> run(Object(Illuminate \ Http \ Request) )9 /Applications/XAMPP/xamppfiles/htdocs/dicom/vendor/laravel/framework/src/Illuminate/Routing/Router.php(996):Illuminate \ Routing \ Router-> dispatchToRoute(Object(Illuminate \ Http \ Request)) 10 / Applications / XAMPP / xamppfiles / htdocs / dicom / vendor / laravel / f ramework / src / Illuminate / Foundation / Application.php(775):Illuminate \ Routing \ Router-> dispatch(Object(Illuminate \ Http \ Request))11 / Applications / XAMPP / xamppfiles / htdocs / dicom / vendor / laravel / framework /src/Illuminate/Foundation/Application.php(745):Illuminate \ Foundation \ Application-> dispatch(Object(Illuminate \ Http \ Request))12 / Applications / XAMPP / xamppfiles / htdocs / dicom / vendor / laravel / framework / src / Illuminate / Session / Middleware.php(72):Illuminate \ Foundation \ Application-> handle(Object(Illuminate \ Http \ Request),1,true)13 / Applications / XAMPP / xamppfiles / htdocs / dicom / vendor / laravel /framework/src/Illuminate/Cookie/Queue.php(47):Illuminate \ Session \ Middleware-> handle(Object(Illuminate \ Http \ Request),1,true)14 / Applications / XAMPP / xamppfiles / htdocs / dicom / vendor / laravel / framework / src / Illuminate / Cookie / Guard.php(51):Illuminate \ Cookie \ Queue-> handle(Object(Illuminate \ Http \ Request),1,true)15 / Applications / XAMPP / xamppfiles / htdocs /dicom/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23):我lluminate \ Cookie \ Guard-> handle(Object(Illuminate \ Http \ Request),1,true)16 /Applications/XAMPP/xamppfiles/htdocs/dicom/vendor/laravel/framework/src/Illuminate/Foundation/Application.php( 641):Stack \ StackedHttpKernel-> handle(Object(Illuminate \ Http \ Request))17 /Applications/XAMPP/xamppfiles/htdocs/dicom/public/index.php(49):Illuminate \ Foundation \ Application-> run() 18 [] []

在网页上是:

The localhost page isn’t working

localhost is currently unable to handle this request.

1 回答

  • 0

    您的代码假设每条记录都有ID,名称和 Headers .

    我会在调用之前检查这些是否实际设置

    像这样的东西 .

    foreach ($posts as $post) {
    
    $id= isset($post->id) ? $post->id : 'ID Not Set';
    $name= isset($post->name) ? $post->name : 'Name Not Set';
    $title= isset($post->title) ? $post->title : ' Title Not Set';
    
        $html .= '<div class="table-scrollable">
                        <table id="posts" class="table table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th>Id</th>
                                    <th>Name</th>
                                    <th>Title</th>
                                </tr>
                            </thead>
                            <tbody id="body">
                              <tr>
                                <td>'. $id .'</td>
                                <td>'. $name .'</td>
                                <td>'. $title .'</td> 
                              </tr>
                   </tbody>
                </table>
            </div>';
    }
    

    如果未设置它们,则可能导致代码失败,并且找不到对象错误 .

    您可以从storage / logs文件夹中检查日志

相关问题