danfickle / openhtmltopdf

An HTML to PDF library for the JVM. Based on Flying Saucer and Apache PDF-BOX 2. With SVG image support. Now also with accessible PDF support (WCAG, Section 508, PDF/UA)!
https://danfickle.github.io/pdf-templates/index.html
Other
1.93k stars 359 forks source link

Font list empty when using font from File #906

Closed JanvanCasteren closed 1 year ago

JanvanCasteren commented 1 year ago

When I try to use a font from file, as in this:

builder.useFont(new File("/path/to/any-font.ttf"), "my-regular");

I get the message 'Font list is empty'

com.openhtmltopdf.load INFO:: Loading font(my-regular) from PDFont supplier now. com.openhtmltopdf.render WARNING:: Font list is empty.

I am sure the font file is at the given path. I can read and write it with java, eg with

        try {
            fis = new FileInputStream("/path/to/any-font.ttf");
            bis = new BufferedInputStream( fis);
            fos = new FileOutputStream("/path/to/out.ttf");
            bos = new BufferedOutputStream(fos);

            int b;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }
            bos.flush();
        }
        catch(IOException ex) {
            System.err.println(ex.getMessage());
        }
        finally {
            if(fis!=null) fis.close();
            if(bis!=null) bis.close();
            if(fos!=null) fos.close();
            if(bos!=null) bos.close();
        }

the file at path/to/out.ttf can be displayed correctly with a font viewer

used openhtmltodpf version is 1.0.10

JanvanCasteren commented 1 year ago

Update: I found my way around Java (I don't develop in Java normally) and I am debugging the thing now myself. I will come back on it later with my findings.

JanvanCasteren commented 1 year ago

I found the issue.

The solution was in the guideline:

_PDF/UA prohibits the use of built-in fonts so make sure to provide a font and specify it for the body element and any page margin rules (eg. @top-center).

I missed that one!