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

Add {{ gotenberg_font() }} twig function #151

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

StevenRenaux
Copy link
Collaborator

@StevenRenaux StevenRenaux commented Feb 28, 2025

Q A
Gotenberg API version ? 8.x
Bug fix ? no
New feature ? yes
BC break ? yes
Issues Fix #148

Description

Twig file

The {{ gotenberg_font() }} function helps generate an @font-face declaration with the correct asset path expected by gotenberg.

You can provide an absolute path.

Example

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>PDF with Custom Font</title>
        <style>
            {{ gotenberg_font('fonts/custom-font.ttf', 'my_font') }}
            h1 {
                color: red;
                font-family: "my_font";
            }
        </style>
    </head>
    <body>
        <h1>This text uses the custom font.</h1>
    </body>
</html>

Output

```html
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>PDF with Custom Font</title>
        <style>
            @font-face {
                font-family: "my_font";
                src: url("custom-font.ttf");
            }
            h1 {
                color: red;
                font-family: "my_font";
            }
        </style>
    </head>
    <body>
        <h1>This text uses the custom font.</h1>
    </body>
</html>

And in your controller nothing needs to be changed.

namespace App\Controller;

use Sensiolabs\GotenbergBundle\GotenbergPdfInterface;

class YourController
{
    public function yourControllerMethod(GotenbergPdfInterface $gotenberg): Response
    {
        return $gotenberg->html()
            ->content('twig_simple_pdf.html.twig')
            ->generate()
            ->stream()
         ;
    }
}

HTML file

If your file is an HTML file (not a Twig template), you can still include fonts manually.

The only requirement is that their paths in the HTML file are on the root level.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>PDF with Custom Font</title>
        <style>
            @font-face {
                font-family: "my_font";
                src: url("custom-font.ttf");
            }
        </style>
    </head>
    <body>
        <p style="font-family: 'my_font';">This text uses the custom font.</p>
    </body>
</html>

All you need to do is to add the path of the asset file to either
assets(...string) or addAsset(string) function.

namespace App\Controller;

use Sensiolabs\GotenbergBundle\GotenbergPdfInterface;

class YourController
{
    public function yourControllerMethod(GotenbergPdfInterface $gotenberg): Response
    {
        return $gotenberg->html()
            ->contentFile('content.html')
            ->assets('fonts/my-font.ttf') // By default, the assets are fetch in the `assets` folder of your application.
            ->generate()
            ->stream()
        ;
    }
}

@StevenRenaux StevenRenaux force-pushed the features/Add-twig-font-function branch from bb5fc1c to 68b1f57 Compare February 28, 2025 10:29
@StevenRenaux StevenRenaux force-pushed the features/Add-twig-font-function branch from 68b1f57 to 1558cde Compare February 28, 2025 10:31
{
$this->addAsset($path, 'gotenberg_font');

$name = htmlspecialchars($name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed ? might be optional and fallback on basename ?
also if htmlspecialchars is needed to be sure it is compatible then I'd check if there are any diff between $name and htmlspecialchars($name) to throw error. Because then I assume that this name is referenced by the developper somewhere else right ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basename give us the name with the extension who is not really user friendly I think 🤷‍♂️​​
And the value for font-family can sometimes be an other name.

About htmlspecialchars, it's more because I put the option is_safe into the TwigFunction argument, to avoid the default behavior of escaping. https://twig.symfony.com/doc/3.x/advanced.html#automatic-escaping

But I need, I think, to convert special char if the developper pass some wrong values...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe ensure the name is not empty ?

Copy link
Collaborator Author

@StevenRenaux StevenRenaux Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Neirda24 You mean empty string? Same for the path argument then?

@StevenRenaux StevenRenaux force-pushed the features/Add-twig-font-function branch from 33b9797 to 985d9a1 Compare February 28, 2025 15:32
@StevenRenaux StevenRenaux requested a review from Neirda24 March 3, 2025 12:57
Copy link
Contributor

@Neirda24 Neirda24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth creating an issue to allow adding fonts from the configuration itself ?

{
$this->addAsset($path, 'gotenberg_font');

$name = htmlspecialchars($name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe ensure the name is not empty ?

@StevenRenaux
Copy link
Collaborator Author

StevenRenaux commented Mar 6, 2025

Might be worth creating an issue to allow adding fonts from the configuration itself ?

@Neirda24
I'm not really in favor of this, because we're back to the other solution. Loop over all the other assets to finally update the template on the fly.

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

Successfully merging this pull request may close these issues.

Automatically add fonts
2 participants