blade
DOMDocument::loadHTML(): error parsing attribute name in Entity, line: 1

DOMDocument::loadHTML(): error parsing attribute name in Entity, line: 1

You might get this issue while trying to download report in Laravel. Please note, this issue fix is only with the platform Laravel and while trying to download your data in excel file.

You will get this issue while dealing with FromView approach. Here is an example.

namespace App\Exports;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;

class CustomerExport implements FromView
{
    public function view(): View
    {
        return view('exports.customers', [
            'customer' => Customer::all()
        ]);
    }
}

Now let’s create exports.customers blade file.

!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<table "width: 100%">
    <thead>
    <tr>
        <th>Name</th>
        <th>Email</th>
    </tr>
    </thead>
    <tbody>
    @foreach($customers as $customer)
        <tr>
            <td>{{ $customer->name }}</td>
            <td>{{ $customer->email }}</td>
        </tr>
    @endforeach
    </tbody>
</table>
</body>

As you can see, if you try to run this project, you will issue as follows:

DOMDocument::loadHTML(): error parsing attribute name in Entity, line: 1

What is going wrong you might say. But the rule behind creating blade file for Export should not contain any inline css codes. As you can see "width: 100%" is added in table tag. Either it is inline tags or other header related tags like <title>, <body> or <script>, they should not be included in blade file. Rather it should only be related to table in plan format. So here is the corrected one.

<table>
    <thead>
    <tr>
        <th>Name</th>
        <th>Email</th>
    </tr>
    </thead>
    <tbody>
    @foreach($customers as $customer)
        <tr>
            <td>{{ $customer->name }}</td>
            <td>{{ $customer->email }}</td>
        </tr>
    @endforeach
    </tbody>
</table>

Now it will download excel file.

I hope, this will be helpful. Let me know if any confusion down below in comment section.

Thanks

Leave a Reply