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 357 forks source link

Font in .otf format #277

Open taras19921 opened 6 years ago

taras19921 commented 6 years ago

I am using OCR-B-Regular.otf font. But I am getting the following error:

java.lang.NullPointerException: null at com.openhtmltopdf.pdfboxout.PdfBoxTextRenderer.getWidth(PdfBoxTextRenderer.java:300) at com.openhtmltopdf.render.InlineBox.getSpaceWidth(InlineBox.java:221) at com.openhtmltopdf.render.InlineBox.calcMinWidthFromWordLength(InlineBox.java:238) at com.openhtmltopdf.render.InlineBox.calcMinMaxWidth(InlineBox.java:378) at com.openhtmltopdf.render.BlockBox.calcMinMaxWidthInlineChildren(BlockBox.java:1748) at com.openhtmltopdf.render.BlockBox.calcMinMaxWidth(BlockBox.java:1624) at com.openhtmltopdf.render.BlockBox.calcShrinkToFitWidth(BlockBox.java:1501) at com.openhtmltopdf.render.BlockBox.calcShrinkToFitWidthIfNeeded(BlockBox.java:976) at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:869) at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:823) at com.openhtmltopdf.layout.InlineBoxing.layoutInlineBlockContent(InlineBoxing.java:426) at com.openhtmltopdf.layout.InlineBoxing.layoutContent(InlineBoxing.java:317) at com.openhtmltopdf.render.BlockBox.layoutInlineChildren(BlockBox.java:1068) at com.openhtmltopdf.render.BlockBox.layoutChildren(BlockBox.java:1049) at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:906) at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:823) at com.openhtmltopdf.layout.BlockBoxing.layoutBlockChild0(BlockBoxing.java:321) at com.openhtmltopdf.layout.BlockBoxing.layoutBlockChild(BlockBoxing.java:299) at com.openhtmltopdf.layout.BlockBoxing.layoutContent(BlockBoxing.java:90) at com.openhtmltopdf.render.BlockBox.layoutChildren(BlockBox.java:1052) at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:906) at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:823) at com.openhtmltopdf.layout.Layer.layoutAbsoluteChild(Layer.java:1014) at com.openhtmltopdf.layout.Layer.layoutAbsoluteChildren(Layer.java:949) at com.openhtmltopdf.layout.Layer.finish(Layer.java:935) at com.openhtmltopdf.layout.LayoutContext.popLayer(LayoutContext.java:306) at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:948) at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:823) at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.layout(PdfBoxRenderer.java:333) at com.openhtmltopdf.pdfboxout.PdfRendererBuilder.run(PdfRendererBuilder.java:40)

Is it possible to use .otf fonts ?

danfickle commented 6 years ago

Hi @taras19921,

No, .otf (opentype) fonts are not supported. Please see the fonts wiki page:

https://github.com/danfickle/openhtmltopdf/wiki/Fonts

danfickle commented 6 years ago

For many use cases, you can convert into TrueType format. Here is a free online font converter: https://www.fontsquirrel.com/tools/webfont-generator

Make sure you tick TrueType output format and if you need language support other than Western European, click no subsetting. Prepare to wait up to a minute for the converter to do its thing.

fz-29 commented 4 years ago

If other convertors failed, try using otf2ttf using afdko utility : https://github.com/adobe-type-tools/afdko It worked for Google Noto Fonts.

after installing run: otf2ttf <src path> <dest path>