danfickle / openhtmltopdf-native

A proof-of-concept PDF generator with GraalVM native-image tool.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to make custom fonts to work

andreaselias opened this issue · comments

I was able to run this as a servless lambda function on AWS using quarkus native build. It is working great but the only issue I was unable to work it out is the fact that even though the font file is loaded properly it is not rendering on the PDF. This issue only occurs with the native build.

I use the following code to load the files from the resource and it does not throw any message.

builder.useUriResolver((baseUri, uri) -> {
	if (baseUri != null) {
		URL resource = RelatorioHandler.class.getClassLoader().getResource(uri);
		if (resource == null)
			throw new RuntimeException("Resource não encontrado");

		return resource.toString();
	}

	return uri;
});

expected.pdf
actual.pdf

commented

i'm facing also the same issue with native builds on AWS Lambda. i'm trying to load and use arialmt.ttf arial-mt-bold.ttf files. it seems like files are loaded but somehow not reflected in pdf as arial font. Is it known issue or is there any way to do so ?

`

        PdfRendererBuilder builder = new PdfRendererBuilder();
        builder.useFastMode();

        builder.useFont(getFileStream("/arialmt.ttf"), "ArialMT");
        builder.useFont(getFileStream("/arial-mt-bold.ttf"), "ArialMTBold");

        builder.withHtmlContent(finalHtmlTemplate, null);
        builder.toStream(os);
        builder.run();

`

private FSSupplier<InputStream> getFileStream(String fileName){
    return new FSSupplier<InputStream>() {
        @Override
        public InputStream supply() {
            final InputStream is = FqcsDocumentBuilder.class.getResourceAsStream(fileName);
            LOG.warn(String.format("fileName: %s  stream null: %s", fileName, is == null));
            return is;
        }
    };
}`

`