spatie/laravel-pdf

[Bug]: inlinedImage Blade directive

Closed this issue · 6 comments

What happened?

can't using @inlinedImage('logo.png') blade directive beacuse const request = args[0].startsWith('-f ') ^ TypeError: Cannot read properties of undefined (reading 'startsWith') at Object. error

How to reproduce the bug

i fallow the directives in https://github.com/spatie/laravel-pdf/discussions/71 and trying to add image to header passing the path to @inlinedImage('logo.png') blade directive as its.

controller
return $pdf = LaravelPdf::view('pdf.ereport2', $viewData) ->margins(25, 0, 20, 0) ->headerView('pdf.ereport.header', $headerData) ->footerView('pdf.ereport.footer') ->save(public_path('pdfs/' . $fileName)) ;

header
`

                        <figure style="width: 100px; height: 50px">
                            @inlinedImage('logo.png')
                        </figure>
                    </td>
                    <td class="bordered" style="" rowspan="4">
                        <div data-empty="true" style="text-align: center;font-size:16px;">
                            {{ $eReport->form->doc->name }}</div>
                    </td>
                    <td class="bordered" style="width: 10%; font-size:8px">doc No</td>
                    <td class="bordered " style="width: 10%;font-size: 8px;">{{ $eReport->form->doc->no }}</td>

                </tr>
                <tr>
                    <td class="bordered" style="font-size: 8px">date</td>
                    <td class="bordered " style="font-size: 8px">
                        {{ $eReport->form->doc->publish_date?->format('d.m.Y') }}</td>
                </tr>
                <tr>
                    <td class="bordered " style="font-size: 8px">revision</td>
                    <td class="bordered " style="font-size: 8px">{{ $eReport->form->doc->revision_no }}</td>
                </tr>
                <tr>
                    <td class="bordered " style="font-size: 8px">revision date</td>
                    <td class="bordered " style="font-size: 8px">
                        {{ $eReport->form->doc->revision_date?->format('d.m.Y') }}
                    </td>
                </tr>
            </tbody>
        </table>

`

Package Version

1.5

PHP Version

8.2.12

Laravel Version

10.48.15

Which operating systems does with happen with?

Windows

Notes

No response

@BeratDen did you try with an absolute path, just to see if it works, if that works then you should probably start by checking if the image is set in the public directory

I'm struggling with the same issue, after hours of testing I can affirm that the error occurs when the headerTemplate passed as option to Browsershot is too long (this happens for example when the base64 encoding of the image is on average long or by adding multiple images with a short encoding that individually might work).

The problem also occurs even if I add the header directly via customizing a Browsershot instance, so this can be considered a Browsershot issue.

return pdf()
  ->withBrowsershot(function (Browsershot $browsershot) {
      $browsershot->headerHtml(view('my-pdf-header')->toHtml());
  })
  ->view('my-pdf-main-view');

@ArielMejiaDev i tried to give a absolute path now i get timeout error
// config brand.php
[ 'logo' => 'http://127.0.0.1:8000/logo.png' ]

// pdf.ereport.header
@inlinedImage(config('brand.logo'))
cURL error 28: Operation timed out after 30002 milliseconds with 0 bytes received (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://127.0.0.1:8000/logo.png

@andrecolza have you found a workaround?

@waris-mohammad Unfortunately I haven't found any workaround yet, I've opened a discussion thread in Browsershot (issues tab is not available), but I haven't received any response yet.

dmuy commented

I made it work by using this as reference.

Just add $browsershot->writeOptionsToFile(); in your code like so:

pdf()
->withBrowsershot(function (Browsershot $browsershot) {
    $browsershot->writeOptionsToFile();
})

You still have to use base64 for the images though - I haven't tried with svg, but I'm guessing it'll work as well.