Xhtmlrenderer xhtml to pdf font problem

I am using org.xhtmlrenderer.pdf.ITextRenderer

to convert my (x) html page to pdf using Java.

I have most of the work except the font part.

I am using verdana

in my page and pdf using default font.

I have added verdana.ttf to my jar and am using 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

+2


a source to share


1 answer


To work xhtmlrenderer

, the CSS must read:

font-family: Verdana;

instead



font-family:verdana;

It is case sensitive.

+2


a source







All Articles