简体   繁体   中英

Export PDF in Laravel with so much data

I have the next code

$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 problem is when I want to download so much data i'm getting the next error The localhost page isn't working localhost is currently unable to handle this request. But if I try to download for example 20 entry, works fine. What can I do?

laravel.log

[2016-11-24 13:15:17] production.ERROR: exception 'ErrorException' with message 'Undefined variable: data_start' in /Applications/XAMPP/xamppfiles/htdocs/dicom/app/controllers/IncasariController.php:58 Stack trace:

0 /Applications/XAMPP/xamppfiles/htdocs/dicom/app/controllers/IncasariController.php(58):

Illuminate\\Exception\\Handler->handleError(8, 'Undefined varia...', '/Applications/X...', 58, Array)

1 [internal function]: 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(IncasariController), 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 [internal function]: Illuminate\\Routing\\Router->Illuminate\\Routing{closure}()

7 /Applications/XAMPP/xamppfiles/htdocs/dicom/vendor/laravel/framework/src/Illuminate/Routing/Route.php(109):

call_user_func_array(Object(Closure), Array)

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/framework/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):

Illuminate\\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 {main} [] []

And on webpage is:

The localhost page isn’t working

localhost is currently unable to handle this request.

Your code is assuming that every record has an ID, name and a title.

I would check to see if these are actually set before calling them

Something like this.

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

If they are not set they could cause your code to fail with and Object not found error.

You can check your logs from storage/logs folder

Well, you try to export large datasets into PDF. My advice :

  1. Try to make custom CSS to reduce pages load ( you can copy paste your classes to your custom CSS )
  2. Change max execution time on php.ini on your XAMPP (1 minute, 5 minutes, 10 minutes, etc)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM