Search code examples
font-awesomehtml-to-pdf

Does ITextRenderer support font awesome


I am try to render an pdf from html free marker template, I use font awesome fonts, but when I try to render in pdf, the font is not shown, does itextrenderer support such custom font?


Solution

  • I struggled with this as well, but I figured out that I have to add the font manually to get it working. Here's my code:

    def html2Pdf(html: String): Array[Byte] = {
        val output: ByteArrayOutputStream = new ByteArrayOutputStream
    
        val renderer: ITextRenderer = new ITextRenderer()
        renderer.getFontResolver.addFont("fontawesome-webfont.ttf", BaseFont.IDENTITY_H, true)
    
        renderer.setDocumentFromString(html)
        renderer.layout()
        renderer.createPDF(output)
        renderer.finishPDF()
        output.flush()
        output.close()
    
        output.toByteArray
    }