Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xhtmlrenderer xhtml to pdf font problem

I'm using org.xhtmlrenderer.pdf.ITextRenderer to convert my (x)html page to pdf using Java.

I've got most of it working, except the font part.

I'm using verdana in my page and the pdf is rendered using default font.

I have added the verdana.ttf to my jar and use the following code:

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new StringBufferInputStream(html));      

File tmpFontFile = new File(TEMP_FOLDER + "/verdana.ttf");
      if(!tmpFontFile.exists())
      {
       tmpFontFile.createNewFile();

       InputStream fontIs = getClass().getResourceAsStream("/com/mycompany/util/font/verdana.ttf");   
       OutputStream fontOs = new FileOutputStream(tmpFontFile);

       byte buf[] = new byte[1024];
       int len;

       while((len = fontIs.read(buf)) > 0)
        fontOs.write(buf,0,len);

       fontOs.close();
       fontIs.close();
      }


      ITextRenderer renderer = new ITextRenderer();
      renderer.getFontResolver().addFont(
        tmpFontFile.getAbsolutePath(), BaseFont.IDENTITY_H ,BaseFont.EMBEDDED);
      renderer.setDocument(doc, null);

      String outputFile = TEMP_FOLDER + "/mypdf.pdf";
      OutputStream os = new FileOutputStream(outputFile);
      renderer.layout();
      renderer.createPDF(os);
      os.close();

What am I missing here?

Thanks, Bart

like image 706
B. T. Avatar asked Jun 16 '26 15:06

B. T.


1 Answers

For xhtmlrenderer to work, the CSS must read:

font-family: Verdana;

instead of

font-family:verdana;

It's case-sensitive.

like image 91
B. T. Avatar answered Jun 18 '26 06:06

B. T.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!