首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xhtmlrenderer xhtml to pdf字体问题

xhtmlrenderer xhtml to pdf字体问题
EN

Stack Overflow用户
提问于 2010-05-05 16:38:39
回答 1查看 4.7K关注 0票数 2

我正在使用org.xhtmlrenderer.pdf.ITextRenderer将我的(x)html页面转换为使用Java的pdf。

我已经完成了大部分的工作,除了字体部分。

我在我的页面中使用了verdana,而pdf是使用默认字体呈现的。

我已经将verdana.ttf添加到我的jar中,并使用以下代码:

代码语言:javascript
复制
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();

这里我漏掉了什么?

谢谢,巴特

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-05-07 19:41:23

要使xhtmlrenderer正常工作,CSS必须为:

font-family: Verdana;

而不是

font-family:verdana;

它区分大小写。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2771623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档