使用itext library.using spring boot从html转换为pdf时无法添加字体

uelo1irk  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(500)

我正在使用itext库将html转换为pdf。我可以正确转换,但不能添加字体。我的转换代码在这里

Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
cfg.setObjectWrapper(new DefaultObjectWrapper(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS));
Template t = new Template("templateName", new StringReader(templateHTML), cfg);
Writer out = new FileWriter(new File(TARGET + documentName + ".html"));
t.process(model, out);
out.close();

ConverterProperties properties = new ConverterProperties();
properties.setBaseUri(TARGET);
File output = new File(TARGET + documentName + ".pdf");

FontProvider fontProvider = new DefaultFontProvider();
FontProgram fontProgram = FontProgramFactory.createFont(FONT);
fontProvider.addFont(fontProgram);
properties.setFontProvider(fontProvider);
HtmlConverter.convertToPdf(
    new FileInputStream(TARGET + documentName + ".html"),
    new FileOutputStream(TARGET + documentName + ".pdf"),
    properties);

我的目标和字体在这里

public static String TARGET = "target/classes/documentsTemplate/";
  public static final String FONT = "./src/main/resources/fonts/special.ttf";

我也试着像这样给html添加字体


* {

    font-family: myFirstFont;
    src: local("C:/font/special.ttf");
}

.text-font {
    font-family: "myFirstFont";
}
mitkmikd

mitkmikd1#

我决定用这个密码。

FontProvider fontProvider = new DefaultFontProvider(false, false, false);
fontProvider.addFont(FONT);
properties.setFontProvider(fontProvider);

但我不能使我的字体加粗

相关问题