PhpOffice\PhpSpreadsheet\Reader\Exception Failed to load .... as a DOM document
Asked Answered
H

6

6

When I try to export datas as excel file on the specific request I got below error

PhpOffice\PhpSpreadsheet\Reader\Exception Failed to load path_to_project\storage\framework\laravel-excel\laravel-excel-pz23rYwVENIOZUw7vnvhKaNjUkkNLNT8.html as a DOM Document

I search alot but couldnot find any answer, the problem comes only when semester = First Sem but when semester is second, third etc this error doesn't come and I can download excel.

Below are my code

Route::get('/export', function () {
  return Excel::download(new ReportExport(request()->exams, request()->course, request()->semesterName), ''.request()->course.' '.request()->semesterName.'('.request()->exams.').xlsx');
})

// ReportExcel.php
$data = Fee::with(['backpapers' => function($bp) use ($myCourse, $semName) {
            $bp->where('course_name', $myCourse)->where('semester', $semName);
        }])->get();

 $table = '<table class="table table-border table-hover"><thead><tr><th>#</th><th>Roll No</th><th>Name</th><th>Reg_No</th><th>Father\'s Name</th><th>Gender</th><th>Aadhaar No</th><th>Mobile</th><th>Email</th><th>Paper 1</th><th>Paper 2</th><th>Paper 3</th><th>Paper 4</th><th>Exam Fee</th></tr></thead><tbody>';

 $count = 1;
 foreach ($data as $key => $bp) {
  if (count($bp->backpapers) > 0) {
    $table .= '<tr><td>'.$count.'</td><td>'.$bp->roll_number.'</td><td>'.$bp->name.'</td><td>'.($bp->registration_number != null ? $bp->registration_number : '-').'</td><td>'.$bp->father_name.'</td><td>'.($bp->gender == 1 ? 'Male' : 'Female').'</td><td>'.($bp->aadhaar_number != null ? $bp->aadhaar_number : '-').'</td><td>'.$bp->mobile_number.'</td><td>'.($bp->email != null ? $bp->email : '-').'</td>';
    
    $paperFee = BackpaperFee::where('number_of_paper', count($bp->backpapers))->pluck('fee_amount')->first();
    if (count($bp->backpapers) == 4) {
        foreach ($bp->backpapers as $bpp) {
            $table .= '<td>'.$bpp->paper_name.'</td>';
        }
    } elseif (count($bp->backpapers) == 3) {
        foreach ($bp->backpapers as $bpp) {
            $table .= '<td>'.$bpp->paper_name.'</td>';
        }
        $table .= '<td>-</td>';
    } elseif (count($bp->backpapers) == 2) {
        foreach ($bp->backpapers as $bpp) {
            $table .= '<td>'.$bpp->paper_name.'</td>';
        }
        $table .= '<td>-</td><td>-</td>';
    } elseif (count($bp->backpapers) == 1) {
        foreach ($bp->backpapers as $bpp) {
            $table .= '<td>'.$bpp->paper_name.'</td>';
        }
        $table .= '<td>-</td><td>-</td><td>-</td>';
    }

    $table .= '<td>'.$paperFee.'</td></tr>';
    $count++;
  }
 }
 $table .= '</tbody></table>';
 
 return view('excel', compact('table'));

Like I said earlier, the error occurs when semester=First Sem only.

I tried:

  1. php artisan cache:clear
  2. php artisan route:clear
  3. php artisan view:clear
  4. php artisan config:clear

But nothing works.. Thanks in advance

Huzzah answered 19/11, 2020 at 7:11 Comment(0)
L
6

Remove unwanted attributes from html element. In my case i removed

 <table id="employee" cellpadding="0" cellspacing="0" border="0" class="display table table-bordered" id="hidden-table-info">

and added <table> got fixed the issue

Laid answered 3/12, 2020 at 19:41 Comment(0)
T
4

Also! Check your texts for symbols like ">" and "<" and change them with entities names:

&gt;
&lt;
Topeka answered 1/9, 2021 at 10:44 Comment(2)
also "&" for meWellmannered
w3schools.com/html/html_entities.aspButtonhook
L
3

For me, Error occurred because of &

First, it was <td><b>Created & Published </b></td> so it was generating an error.

So I changed it to <td><b>Created and Published </b></td> and it is resolved.

If you have "&" in your label or in values in loop, It may cause an error

Looksee answered 23/11, 2022 at 6:58 Comment(1)
You can replace & with &amp; to still show the & signReticulation
G
1

The Issue was that there was a Closing Anchor Tag, that was making the issue because while generating the Excel there must be no extra things or missing only Pure HTML table works for excel file generation nothing else

Giuseppinagiustina answered 21/5, 2021 at 0:54 Comment(0)
J
0

Mine was I have an extra .

Delete it, and now its working again

Jasperjaspers answered 4/10, 2022 at 10:53 Comment(0)
C
0

I have a button tag in my view which is the reference view file for export.

What I did was, I created another view file then copied the html format of the first view file reference. I deleted the button tag.

Then it works fine now.

Corr answered 20/2 at 8:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.