Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use custom fonts #4

Open
RobertCordes opened this issue Jan 9, 2022 · 2 comments
Open

How to use custom fonts #4

RobertCordes opened this issue Jan 9, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@RobertCordes
Copy link

Hi, I can't get custom fonts working. They’re in the public directory (eg. /public/fonts/Inter-Regular.ttf) which is an allowed directory as in the default configuration:

'allowed_directories' => [
    public_path(),
],
'base_dir' => '/',

In my CSS I've set the following:

@font-face {
    font-family: "Inter";
    src: url("/public/fonts/Inter-Regular.ttf") format("ttf");
    /* I also tried src: url("/fonts/Inter-Regular.ttf") format("ttf"); */
    font-weight: 400;
}

But I had no luck. I also tried using .woff. Do you have any idea what the problem could be?

PS: I love your package! ❤️

@jsiefer
Copy link
Contributor

jsiefer commented Jan 10, 2022

Hi (otf, ttf, woff (not woff2)) should work.

Most likely there is an issue with the path mapping. Sadly the Laravel plugin does "swallow" those warnings at the moment. If you can xdebug into it, checkout \Typesetsh\LaravelWrapper\Typesetsh::render() and check the $this->uriResolver->errors after html2pdf->render is called.

Since its its properly relative to your project root, you can try to do:

@font-face {
    font-family: "Inter";
    src: url("./fonts/Inter-Regular.ttf") format("ttf");
    /* I also tried src: url("/fonts/Inter-Regular.ttf") format("ttf"); */
    font-weight: 400;
}
'allowed_directories' => [
    public_path(),
],
'base_dir' => public_path(),

Doing above should tell the UriResolver to prepend the public path to all ./ resources.


  • Improve error reporting using the UriResolver
  • default base path should probably be empty string

@jsiefer jsiefer added the enhancement New feature or request label Jan 10, 2022
@RobertCordes
Copy link
Author

Ok so setting 'base_dir' => public_path() actually helped. When using @font-face directly inside the HTML head, I could declare the path as ./fonts/.... Also, when using an external stylesheet, I have to set it as ../fonts/.... Which makes sense of course, but I just realised that now. That also means that I didn’t really have to mess with the configuration in the first place, sorry about that.

One note though: my .ttf font still doesn’t work, but my .woff does. It’s not really a problem for me though 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants