I create PDFs with spatie/laravel-pdf on Laravel 11, styled with TailwindCSS. My layout has
@vite(['resources/css/app.css'])
I was working locally with vite running. PDfs were looking great.
My deployment includes building the css with
npm ci
npm run build
rm -rf node_modules
The website renders just fine with the built stylesheets from /build/assets/...
.
But: When I deployed my app to my forge server, the PDFs were missing the stylesheet after generation.
I tried motomedialab/laravel-vite-helper
and including the CSS with variants of
<link rel="stylesheet" href="{{ vite('resources/css/app.css', 'build', false, false) }}">
and even
<link rel="stylesheet" href="/build/assets/app-Bq_2mYPq.css">
This did both not work. But I want to use my build CSS, and not keeping vite running 24/7 on my production server.
The solution was presented to me from a nice guy on Stack Overflow. You can just include the CSS inline in your template with
<style>{!! Vite::content('resources/css/app.css') !!}</style>
Now my styles are always included in the PDF and everything renders just find 🙂