简体   繁体   中英

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...

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()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM