spatie / laravel-pdf

Create PDF files in Laravel apps
https://spatie.be/docs/laravel-pdf
MIT License
675 stars 53 forks source link

[Bug]: inlinedImage Blade directive #168

Open BeratDen opened 1 month ago

BeratDen commented 1 month ago

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 `

@inlinedImage('logo.png')
{{ $eReport->form->doc->name }}
doc No {{ $eReport->form->doc->no }}
date {{ $eReport->form->doc->publish_date?->format('d.m.Y') }}
revision {{ $eReport->form->doc->revision_no }}
revision date {{ $eReport->form->doc->revision_date?->format('d.m.Y') }}

`

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

ArielMejiaDev commented 1 month ago

@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

andrecolza commented 1 month ago

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');
BeratDen commented 1 month ago

@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

waris-mohammad commented 1 month ago

@andrecolza have you found a workaround?

andrecolza commented 1 month ago

@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 1 week ago

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.