javaspring-bootflying-saucer

Include .ttf font into pdf generated with Flying Saucer and Spring-Boot


I only managed to generate the pdf with .ttf font included only if I have provided the absolute path in the index.html like this:

        @font-face {
        font-family: Asap-Regular;
        src: url("/home/user/Desktop/Asap-Regular.ttf");
        -fs-pdf-font-embed: embed;
        -fs-pdf-font-encoding: Identity-H;
        }

What I'm trying to achieve is to integrate the .ttf font as a relative path. I've also tried to integrate from java code like this:

renderer.getFontResolver().addFont(("path/Asap-Bold.ttf"),
                                       "Asap-Bold", "Identity-H", true, null);

and it didn't work...


Solution

  • After debugging Flying Saucer some time it seems that:

    _sharedContext.setBaseURL(url);
    

    that needs to point to .ttf file path(in my case it was resources dir). In order to set the url from sharedContext you need to call:

    renderer.setDocumentFromString(html, String.valueOf(resourceLoader.getResource("classpath:fonts/").getURI()));