Repository: danfickle/openhtmltopdf

Issue #183: Can't use own fonts
Created: 2018-03-15T12:23:41Z
Hi,
I tried to add custom fonts, but always with the same error. Can you advise what I am doing wrong or whether it is an internal error?
java.lang.NullPointerException
at com.openhtmltopdf.pdfboxout.PdfBoxFontResolver.resolveFont(PdfBoxFontResolver.java:70)
at com.openhtmltopdf.layout.SharedContext.getFont(SharedContext.java:391)
at com.openhtmltopdf.pdfboxout.PdfBoxOutputDevice.processControls(PdfBoxOutputDevice.java:316)
at com.openhtmltopdf.pdfboxout.PdfBoxOutputDevice.finish(PdfBoxOutputDevice.java:1000)
at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.writePDF(PdfBoxRenderer.java:607)
at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:546)
at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:485)
at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:481)
at com.openhtmltopdf.pdfboxout.PdfRendererBuilder.run(PdfRendererBuilder.java:86)
at cz.codeberry.oneclick.example.SimpleUsage.exportToPdfBox(SimpleUsage.java:30)
at cz.codeberry.oneclick.example.SimpleUsage.main(SimpleUsage.java:17)
My CSS:
@font-face {
src: url("./OpenSans.ttf");
font-family: "Open Sans Condensed";
font-weight: 700;
}
h1 {
font-family: "Open Sans Condensed", "Arial Narrow", Arial, sans-serif;
}
I tried I add the font directly to the builder, but also unsuccessfully:
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.withFile(file);
builder.useFont(new File("./OpenSans.ttf"), "Open Sans Condensed");
builder.toStream(os);
builder.run();

Duplicate issue #203: NullPointerException by ctrl.box.getStyle when no font is definied...
Created: 2018-04-10T13:09:21Z
Currently i resolved this for me with this.
diff --git a/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxOutputDevice.java b/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxOutputDevice.java
index eb4115a..a90a877 100644
--- a/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxOutputDevice.java
+++ b/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxOutputDevice.java
@@ -318,7 +318,13 @@
             if (!(ctrl.box.getElement().getAttribute("type").equals("checkbox") ||
                   ctrl.box.getElement().getAttribute("type").equals("radio") ||
                   ctrl.box.getElement().getAttribute("type").equals("hidden"))) {
-                PDFont fnt = ((PdfBoxFSFont) _sharedContext.getFont(ctrl.box.getStyle().getFontSpecification())).getFontDescription().get(0).getFont();
+            	FontSpecification spec = ctrl.box.getStyle().getFontSpecification();
+            	PDFont fnt = null;
+            	if (spec == null) {
+            		fnt = PDType1Font.COURIER ;
+                } else {
+                	fnt = ((PdfBoxFSFont) _sharedContext.getFont(ctrl.box.getStyle().getFontSpecification())).getFontDescription().get(0).getFont();
+                }
 
                 if (!controlFonts.containsKey(fnt)) {
                     fontName = "OpenHTMLFont" + controlFonts.size();


But why ... don't know exactly try to add soon a failing example html and css file who i can provide to public.

Closing comment (2018-04-10T13:40:13Z): Hi, I think this is a dupe of #183 If that's the case, you could try the
workaround mentioned there, until I develop a correct fix. Please close
this issue if you agree it is a duplicate. Thanks.