0

I am beginner in Laravel. I use in my project Laravel 5.8.

I have this code for generate Word file:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
        $month = $request->input('month');
        if ($month == null) {
            $now = Carbon::now();
            $month = $now->month;
        }
        $events = $this->frontendGateway->getEventCalendarDownload($request, $month);
        $logo = public_path('assets/images/logo3.jpg');
        $view_content = View::make('psCMS.prints.events-view', ['events' => $events, 'logo' => $logo])->render();
        $section = $phpWord->addSection();
        $text = $section->addText('aaaaaaaa');
        $text = $section->addText('bbbbbbbbbb');
        $text = $section->addText('ccccccccccc');
        $text = $section->addText($view_content);

        //ob_clean();
        $fileName = 'Event_calendar' . '-' . now()->toDateString() . '.doc';
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save($fileName);

        header('Content-Type: application/octet-stream');
        header("Cache-Control: no-cache, must-revalidate");
        header("Pragma: no-cache");
        header("Content-Disposition: attachment; filename=$fileName");
        ob_clean();
        readfile($fileName);

and this is my Blade file:

<div id="header" class="fontSize14">
    <table width="100%">
        <tr>
            <td align="left" style="width: 20%;">
                <img src="{{ $logo }}" class="logo" />
            </td>
            <td align="left" style="width: 80%;">
                <span class="fontSize19"><b>my name</b></span><br />
                street<br />
            </td>
        </tr>
    </table>
</div>

<div id="content" class="fontSize11">
    <b class="fontSize19">Kalendarz wydarzeń</b><br /><br />


    <table width="100%">
        <thead style="background-color: lightgray;">
            <tr>
                <th>#</th>
                <th>Data</th>
                <th>Godzina</th>
                <th>Nazwa imprezy</th>
                <th>Miejsce</th>
            </tr>
        </thead>
        <tbody>
            @foreach($events as $event)
            @php
                $hourFromX = explode(":", $event->hour_from);
                $hourToX = explode(":", $event->hour_to);
                $hourFrom = $hourFromX['0'].":".$hourFromX['1'];
                $hourTo = $hourToX['0'].":".$hourToX['1'];
            @endphp
            <tr>
                <th scope="row">{{ $loop->iteration }}</th>
            <td>{{ $event->date_from }}</td>
            <td align="left">{{ $hourFrom }}-{{ $hourTo }}</td>
            <td align="left">{{ $event->title }}</td>
            <td align="left">@if(isset($event->localization)) {{ $event->localization->name }},
                {{ $event->localization->city }}
                {{ $event->localization->street }} @endif</td>
            </tr>
            @endforeach
        </tbody>
    </table>


</div>

When I run this code, I have error: Word file is corrupted.

When I comment this line:

$text = $section->addText($view_content);

Word file is okey. Generated file was open in Word without any problems.

How can I repair it?

3
  • Check if the $view_content proper html. There might be spacing etc maybe? Commented Jan 2, 2020 at 11:19
  • pastebin.pl/view/fd5f84ed - that's my value in this variable. I want to insert the above content into my file :) Commented Jan 2, 2020 at 11:29
  • pastebin.com/i4K96Jp8 Commented Jan 2, 2020 at 13:53

1 Answer 1

1

Can you try adding addHtml instead of addText

\PhpOffice\PhpWord\Shared\Html::addHtml($section, $view_content , false, false);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.