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.91k stars 356 forks source link

always get java.lang.NullPointerException: Cannot read field "families" because "spec" is null #952

Closed wunaidage closed 10 months ago

wunaidage commented 10 months ago

here's the stacktrace.

Caused by: java.lang.NullPointerException: Cannot read field "families" because "spec" is null
    at com.openhtmltopdf.pdfboxout.PdfBoxFontResolver.resolveFont(PdfBoxFontResolver.java:90) ~[openhtmltopdf-pdfbox-1.0.10.jar:na]
    at com.openhtmltopdf.layout.SharedContext.getFont(SharedContext.java:335) ~[openhtmltopdf-core-1.0.10.jar:na]
    at com.openhtmltopdf.pdfboxout.PdfBoxPerDocumentFormState.getControlFont(PdfBoxPerDocumentFormState.java:99) ~[openhtmltopdf-pdfbox-1.0.10.jar:na]
    at com.openhtmltopdf.pdfboxout.PdfBoxPerDocumentFormState.processControls(PdfBoxPerDocumentFormState.java:149) ~[openhtmltopdf-pdfbox-1.0.10.jar:na]
    at com.openhtmltopdf.pdfboxout.PdfBoxFastOutputDevice.processControls(PdfBoxFastOutputDevice.java:299) ~[openhtmltopdf-pdfbox-1.0.10.jar:na]
    at com.openhtmltopdf.pdfboxout.PdfBoxFastOutputDevice.finish(PdfBoxFastOutputDevice.java:904) ~[openhtmltopdf-pdfbox-1.0.10.jar:na]
    at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.writePDFFast(PdfBoxRenderer.java:674) ~[openhtmltopdf-pdfbox-1.0.10.jar:na]
    at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPdfFast(PdfBoxRenderer.java:564) ~[openhtmltopdf-pdfbox-1.0.10.jar:na]
    at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:490) ~[openhtmltopdf-pdfbox-1.0.10.jar:na]
    at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:427) ~[openhtmltopdf-pdfbox-1.0.10.jar:na]
    at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:409) ~[openhtmltopdf-pdfbox-1.0.10.jar:na]
    at com.openhtmltopdf.pdfboxout.PdfRendererBuilder.run(PdfRendererBuilder.java:46) ~[openhtmltopdf-pdfbox-1.0.10.jar:na]

here's the code

PdfRendererBuilder builder = new PdfRendererBuilder();
builder.withHtmlContent(html, "");
builder.useFastMode();
builder.toStream(outputStream);
builder.run();

it only happens for form controls inside

tag. I tried all the ways to provide font but nothing works (the font actually applied across the document but control still get null). I also tried to remove all font related code still get the same issue. Please help!

wunaidage commented 10 months ago

turns out the control style unit must be SCREEN UNIT like px, em, rem. if you use PRINT UNIT like cm mm, you will get that NPE

llech commented 1 month ago

turns out the control style unit must be SCREEN UNIT like px, em, rem. if you use PRINT UNIT like cm mm, you will get that NPE

I'm getting the same error on one of the test input files, and I have no idea what have you done to solve the problem...

Which optionn have you set on PdfRendererBuilder?