0

I'm trying to convert html to pdf using the open-html-to-pdf library, but I'm having issues with special characters. Instead of ěščř, only #š## is displayed.

From this issue I understand I need to somehow register a proper font, yet I am left clueless on how to do this.

My code looks like this:

fun main(args: Array<String>) {
    val file = File("input.html")
    val xdoc = Jsoup.parse(file, "UTF-8")
    xdoc.outputSettings().syntax(Document.OutputSettings.Syntax.xml)
    val os = FileOutputStream("output.pdf")

    val builder = PdfRendererBuilder()
    builder.toStream(os)
    builder.withW3cDocument(W3CDom().fromJsoup(xdoc), null)
    builder.run()
    os.close()
}

and the input.html looks like this

<!DOCTYPE html>
<html lang="en">
<body>
<h2>Special characters: ěščř</h2>
</body>
</html>

I am aware I need to use the PdfRendererBuilder().usefont() method, but have not found any way to supply the necessary argument of type FSSupplier<InputStream>, or what it might even be. Can anyone please point me in the right direction?

Edit1: I tried to add the useFont() function as follows:

fun main(args: Array<String>) {
    val file = File("sinput.html")
    val xdoc = Jsoup.parse(file, "UTF-8")
    xdoc.outputSettings().syntax(Document.OutputSettings.Syntax.xml)
    val os = FileOutputStream("output.pdf")

    val builder = PdfRendererBuilder()
    builder.useFont({ File("Roboto-Light.ttf").inputStream() }, "Roboto")
    builder.toStream(os)
    builder.withW3cDocument(W3CDom().fromJsoup(xdoc), null)
    builder.run()
    os.close()
}

with the Roboto-Light.ttf present in the project directory, but to no avail. If I use a different font file, the font does not change, so the function call still has to be incorrect.

Edit2: Success! One should not forget to add the necessary style to the html paragraph, so it now looks like this:

<html>
<head>

</head>
<body>
<p style="font-family:Roboto">Special characters: ěščř</p>
</body>
</html>
  • You don't have to use a `FSSupplier`, you can also simply use a `File`, as written in the answers to the question you linked. But you'll need to have the font file somewhere in your resources or some directory. – Joffrey Sep 01 '23 at 11:39

0 Answers0