NPE while loading font metrics
Stavrenas opened this issue · 2 comments
Stavrenas commented
Providing the stacktrace and the small code block that causes the issue:
Code:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
org.jsoup.nodes.Document document = Jsoup.parse(inputHtml, "UTF-8");
document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.useFont(new File(getClass().getClassLoader().getResource("fonts/Gotham-Book.ttf").getFile()), "Gotham", 400, BaseRendererBuilder.FontStyle.NORMAL, true);
builder.useFont(new File(getClass().getClassLoader().getResource("fonts/Gotham-Bold.ttf").getFile()), "Gotham", 700, BaseRendererBuilder.FontStyle.NORMAL, true);
builder.toStream(outputStream);
builder.withW3cDocument(new W3CDom().fromJsoup(document), "/");
builder.run();
Stacktrace:
java.lang.NullPointerException
at com.openhtmltopdf.pdfboxout.PdfBoxFontResolver$FontDescription.loadMetrics(PdfBoxFontResolver.java:614)
at com.openhtmltopdf.pdfboxout.PdfBoxFontResolver$FontDescription.realizeFont(PdfBoxFontResolver.java:633)
at com.openhtmltopdf.pdfboxout.PdfBoxFontResolver$FontDescription.getFontMetrics(PdfBoxFontResolver.java:713)
at com.openhtmltopdf.pdfboxout.PdfBoxTextRenderer.getFSFontMetrics(PdfBoxTextRenderer.java:83)
at com.openhtmltopdf.layout.LayoutContext.getFSFontMetrics(LayoutContext.java:466)
at com.openhtmltopdf.css.style.CalculatedStyle.getFSFontMetrics(CalculatedStyle.java:774)
at com.openhtmltopdf.css.style.CalculatedStyle.getLineHeight(CalculatedStyle.java:468)
at com.openhtmltopdf.layout.InlineBoxing.layoutContent(InlineBoxing.java:117)
at com.openhtmltopdf.render.BlockBox.layoutInlineChildren(BlockBox.java:1308)
at com.openhtmltopdf.render.AnonymousBlockBox.layout(AnonymousBlockBox.java:48)
at com.openhtmltopdf.layout.BlockBoxing.layoutBlockChild0(BlockBoxing.java:388)
at com.openhtmltopdf.layout.BlockBoxing.layoutBlockChild(BlockBoxing.java:366)
at com.openhtmltopdf.layout.BlockBoxing.layoutContent(BlockBoxing.java:106)
at com.openhtmltopdf.render.BlockBox.layoutChildren(BlockBox.java:1284)
at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:1085)
at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:1028)
at com.openhtmltopdf.layout.BlockBoxing.layoutBlockChild0(BlockBoxing.java:388)
at com.openhtmltopdf.layout.BlockBoxing.layoutBlockChild(BlockBoxing.java:366)
at com.openhtmltopdf.layout.BlockBoxing.layoutContent(BlockBoxing.java:106)
at com.openhtmltopdf.render.BlockBox.layoutChildren(BlockBox.java:1284)
at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:1085)
at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:1028)
at com.openhtmltopdf.layout.BlockBoxing.layoutBlockChild0(BlockBoxing.java:388)
at com.openhtmltopdf.layout.BlockBoxing.layoutBlockChild(BlockBoxing.java:366)
at com.openhtmltopdf.layout.BlockBoxing.layoutContent(BlockBoxing.java:106)
at com.openhtmltopdf.render.BlockBox.layoutChildren(BlockBox.java:1284)
at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:1085)
at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:1028)
at com.openhtmltopdf.layout.BlockBoxing.layoutBlockChild0(BlockBoxing.java:388)
at com.openhtmltopdf.layout.BlockBoxing.layoutBlockChild(BlockBoxing.java:366)
at com.openhtmltopdf.layout.BlockBoxing.layoutContent(BlockBoxing.java:106)
at com.openhtmltopdf.render.BlockBox.layoutChildren(BlockBox.java:1284)
at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:1085)
at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:1028)
at com.openhtmltopdf.layout.BlockBoxing.layoutBlockChild0(BlockBoxing.java:388)
at com.openhtmltopdf.layout.BlockBoxing.layoutBlockChild(BlockBoxing.java:366)
at com.openhtmltopdf.layout.BlockBoxing.layoutContent(BlockBoxing.java:106)
at com.openhtmltopdf.render.BlockBox.layoutChildren(BlockBox.java:1284)
at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:1085)
at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:1028)
at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.layout(PdfBoxRenderer.java:353)
at com.openhtmltopdf.pdfboxout.PdfRendererBuilder.run(PdfRendererBuilder.java:45)
I can also share the font file used. I couldn't find anything similar online
Stavrenas commented
More info. This piece of code lies on a Spring Boot app. When run through the IDE the document is generated correctly. When uploaded to the dev environment and executed via the generated .jar, I get the afforementioned error
Stavrenas commented
Ok turns out I needed to use the following piece of code to load the font files:
final var bookInputStream = new ClassPathResource("fonts/Gotham-Book.ttf").getInputStream();
final var boldInputStream = new ClassPathResource("fonts/Gotham-Bold.ttf").getInputStream();
builder.useFont(() -> bookInputStream, "Gotham", 400, BaseRendererBuilder.FontStyle.NORMAL, true);
builder.useFont(() -> boldInputStream, "Gotham", 700, BaseRendererBuilder.FontStyle.NORMAL, true);