java - html View 中的 iText 等宽字体,带有 css 样式的粗体、斜体

标签 java pdf-generation itext

我正在使用 html 生成 PDF 文档,因为它是 EMR 记录,所以我必须使用等宽字体。

PDF 生成得很好,但粗体和斜体的 css 样式被忽略,因为我使用单个 .otf 文件作为字体,因此没有粗体和斜体。

我想知道如何启用它。下面是代码片段。

字体工厂:

public static class MyFontFactory implements FontProvider,Serializable {
        public Font getFont(String fontname,
                String encoding, boolean embedded, float size,
                int style, BaseColor color) {
                BaseFont bf3 = null;
            try {
                bf3 = BaseFont.createFont("Inconsolata.otf",BaseFont.CP1252, BaseFont.EMBEDDED);
            } catch (Exception e) {
                e.printStackTrace();
            } 

            return new Font(bf3, 6);
        }

        public boolean isRegistered(String fontname) {
            return false;
        }
    }

PDF生成代码:

public void createPdf(Object object) throws Exception, DocumentException{
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(new File("test.pdf")));
        // step 3
        document.open();
       // create extra properties
        HashMap<String,Object> map = new HashMap<String, Object>();
        map.put(HTMLWorker.FONT_PROVIDER, new MyFontFactory());
        // step 4
        String snippet;
        // create the snippet
        snippet = createHtmlSnippet(object);
        Map<Object,Object> model = new HashMap<Object,Object>();
        model.put("object", object);

        StyleSheet css = new StyleSheet();
        Map<String, String> stylemap = new HashMap<String, String>();
        stylemap.put("font-style", "italic");
        stylemap.put("font-size", "small");
        stylemap.put("font-weight", "bold");
        css.loadStyle("header",(HashMap<String, String>) stylemap);
        css.loadStyle("strongClass", "text-decoration", "underline");
        List<Element> objects = HTMLWorker.parseToList(new StringReader(snippet), css, map);

       for (Element element : objects)
              document.add(element);
        // step 5
        document.close();
    }

In the above code css supplied does not produce any effect on output as I mentioned due to single font defined, if I want to have bold and italics how can that be achieved?

如果有人提供相关的指导或帮助,我真的很感激。

谢谢。

注意:如果我删除等宽字体,则会应用 css。

最佳答案

您将字体系列与字体混淆了。

Inconsolata 是一个由不同字体组成的字体系列:

  • Inconsolata 常规,如 inconsolata.ttf 中定义
  • Inconsolata 粗体,如 inconsolata-Bold.ttf 中定义

参见http://code.google.com/p/googlefontdirectory/source/browse/ofl/inconsolata/

我不知道有任何粗体、斜体或粗斜体版本,因为我假设 "there is no bold or italic for Inconsolata."如果没有其他样式的字体程序,您就不应期望 iText 支持这些样式 (*)。

然后我找到了一个带有粗体字体 TTF 的存储库:http://code.google.com/p/googlefontdirectory/source/browse/ofl/inconsolata/

搜索 StackOverflow 我读到了有关 Inconsolata Italic in MacVim 的问题在 StackOverflow 上;不幸的是,这些字体不能在 iText 中使用。

(*) 当字体不支持粗体或斜体时,iText 可以通过更改渲染模式和/或倾斜来模仿这些样式。但是,选择其他等宽字体会获得更好的结果。

关于java - html View 中的 iText 等宽字体,带有 css 样式的粗体、斜体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15195257/

相关文章:

java - 跨项目的继承和可见性

node.js - 如何使用puppeteer异步从多个html文件生成pdf?

c# - 如何在 iTextSharp 中绘制垂直渐变?

php - 如何在共享服务器上使用 wkhtmltopdf

java - 如何使用 Java iText 检查所有使用的字体是否嵌入到 PDF 中?

java - 从大量数据生成大 PDF

java - GC 从范围返回后收集引用的对象

Java 8 Lambda 同时分组 X 和 Y

java - 使用列表进行预测<Double>

pdf - 将禁用智能收缩与 wkhtmltopdf 一起使用不会影响标题字体大小