timrs2998 / pdf-builder

PDF builder written in Kotlin with a statically typed DSL
https://timrs2998.github.io/pdf-builder/
GNU General Public License v3.0
58 stars 16 forks source link

Change font encoding #35

Open kskarzynski opened 1 year ago

kskarzynski commented 1 year ago

Hi, is there any way to change the encoding of the pdFont property? I need to render a PDF using Polish diacritics but I get a following error:

Exception in thread "main" java.lang.IllegalArgumentException: U+017C ('zdot') is not available in the font Helvetica, encoding: WinAnsiEncoding at org.apache.pdfbox.pdmodel.font.PDType1Font.encode(PDType1Font.java:428) at org.apache.pdfbox.pdmodel.font.PDFont.encode(PDFont.java:333) at org.apache.pdfbox.pdmodel.font.PDFont.getStringWidth(PDFont.java:364) at com.github.timrs2998.pdfbuilder.TextElement.width(TextElement.kt:59) at com.github.timrs2998.pdfbuilder.TextElement.wrapText(TextElement.kt:45) at com.github.timrs2998.pdfbuilder.TextElement.instanceHeight(TextElement.kt:11) at com.github.timrs2998.pdfbuilder.Element.height(Element.kt:103) at com.github.timrs2998.pdfbuilder.Element.height$default(Element.kt:101) at com.github.timrs2998.pdfbuilder.RowElement.instanceHeight(RowElement.kt:14) at com.github.timrs2998.pdfbuilder.Element.height(Element.kt:103) at com.github.timrs2998.pdfbuilder.Element.height$default(Element.kt:101) at com.github.timrs2998.pdfbuilder.TableElement.renderInstance(TableElement.kt:63) at com.github.timrs2998.pdfbuilder.TableElement.render(TableElement.kt:48) at com.github.timrs2998.pdfbuilder.Element.render$default(Element.kt:122) at com.github.timrs2998.pdfbuilder.Document.renderInstance(Document.kt:60) at com.github.timrs2998.pdfbuilder.Element.render(Element.kt:128) at com.github.timrs2998.pdfbuilder.Document.render(Document.kt:44) at com.github.timrs2998.pdfbuilder.KotlinBuilderKt.document(KotlinBuilder.kt:23)

timrs2998 commented 1 year ago

pdf-builder uses apache pdfbox under-the-hood, and let's you use any PDFont from apache pdfbox

You might have better luck searching for how to handle encoding with PDFont in apache pdfbox, but nothing stands out to me as an answer to your question:

darktimes commented 5 months ago

Hey, I have faced same problem and I don't see any solution either. There is no way to access the PDDocument before it is rendered. The only thing we have are the Document and Element Interfaces to work with and none of them exposes any functionality to access the PDDocument hence no way to use the fun load(): PDType0Font (since it requies the PDDocument to be attached to.

timrs2998 commented 5 months ago

@darktimes I added a way to override the font in https://github.com/timrs2998/pdf-builder/commit/0a42fbac7b76f148fc20c9c6fec41ddcfdff02c0#diff-e2ec79f2bc4f5018a8a8729a91af5e6f315441d255ece8c38cbb6eda8439e92b version 2.1.0. Let me know if that helps otherwise I may still be confused on what needs to be added for this.

darktimes commented 5 months ago

@timrs2998 Hey, that looks promising. I will look into the exposed property and will respond if that is enough. Thank you!