I am having some trouble getting custom fonts working with DomPDF and Laravel Vapor. (Localy the fonts work)
These are my CSS font definitions (fonts are stored inside public/fonts)
@font-face {
font-family: caveat;
font-style: normal;
font-weight: normal;
src: url('{{ public_path('fonts/caveat.ttf') }}') format('truetype');
}
@font-face {
font-family: Arial;
font-style: normal;
font-weight: normal;
src: url('{{ public_path('fonts/Arial.ttf') }}') format('truetype');
}
This is my dockerfile
FROM laravelphp/vapor:php81
RUN apk add imagemagick imagemagick-dev php81-pecl-imagick
&& pecl install imagick
&& docker-php-ext-enable imagick
COPY . /var/task
Locally these fonts Arial
and CaveAt
get rendering fine in the PDF. However after deployment to staging, the font in the PDF falls back to Helvetica
I probably need to add some commands to the dockerfile in order to get it working. My question is: What commands?
2
Answers
The thing about vapor is all asset files in public directory will be moved to s3 bucket that vapor created to use it with cloudfront as CDN. Therefor you cannot find your public files in within your domain unless you define the files in public files in your vapor config file.
So you have two options to fix this issue:
I highly discourage using the second option since it will cause inconsistence performance issue because of
lambda function
cold start which make site slower and therefor costs more money.Also Cloudfront is a CDN so its nice to have it for asset files.
For fixing it with cloudfront just remove
public_path
within the url and check the file actually exists in your bucket.More info:
https://docs.vapor.build/projects/deployments.html#urls-within-css
You can try and adjust your Dockerfile to make sure the custom fonts are accessible to DomPDF in the Laravel Vapor environment.
Make sure the
public/fonts
directory is correctly copied to the Docker image. That can be done with theCOPY
command in your Dockerfile;Some dependencies might be required to render the fonts correctly. Install font libraries like
fontconfig
andfonts-dejavu
.Sometimes, Laravel’s cache can cause issues. Clear the cache after deployment (however, in a fresh Docker build, especially when the entire application is being copied over, this cache might already be clear, making this step unnecessary).
That updated Dockerfile makes sure the
public/fonts
directory from your Laravel project is copied into the Docker image at the corresponding location (/var/task/public/fonts
). That should make the custom fonts available to DomPDF when running in the Docker environment.If the problem is related to DomPDF’s inability to generate the font cache inside the Docker container, this issue might be due to permissions or the specific way DomPDF handles font caching.
Make sure DomPDF has the necessary permissions to write to the directory where it stores its font cache. That can be typically in the
storage
directory in a Laravel application.You could pre-generate the font cache on your local system and then copy it into the Docker container, ensuring DomPDF does not need to generate it on the fly in the Docker environment.
Do replace
path_to_local_font_cache
with the actual path to your pre-generated font cache on your local system.That configuration makes sure DomPDF has access to the custom fonts and their cache, potentially resolving the issue with font rendering in the Docker container.