iText 5 HTML+CSS to PDF/A-2 : Helvetica font not embedded error

匆匆过客 提交于 2019-11-26 18:01:40

Add the Font Provider to HtmlPipelineContext instead of null

XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.register("arial.ttf", "Arial");
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);

HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

For each tag in HTML file define font in the CSS file

p, h2 {
 font-family: "Arial";
 font-style: normal;
}

Alternatively, define a common font for complete html file

html {
 font-family: "Arial";
 font-style: normal;
}

We are using our very own PrintFontProvider. This should get you going:

private static final CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
private static final HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
static {
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
    htmlContext.autoBookmark(false);

    // Our own PrintFontProvider provides Fonts as needed...
    htmlContext.setCssAppliers(new CssAppliersImpl(new PrintFontProvider()));

    // We apply some default styles to the pdf generation...
    try {
        String styling  = "* { font-size: 8pt; line-height: 1.3em; }\n";
        styling += "* * { font-size: inherit; }\n";
        styling += "p      { margin: 5px 2.5pt; }\n";
        styling += "ul, ol { margin: 2px 0; }\n";
        cssResolver.clear();
        cssResolver.addCss(styling, true);
    }
    catch (CssResolverException ex) {
        // Handle errors as needed...
    }
}

Here is our implementation:

public class PrintFontProvider extends FontFactoryImp {

    public static final String  DEFAULT_FONT    = "LiberationSans";

    @Override
    public Font getFont(String fontName, String encoding, boolean embedded, float size, int style, BaseColor color, boolean cached) {
        LogUtils.debug(PrintFontProvider.class, "in getFont with fontName = '" + fontName + "'");

        // ZapfDingbats ist ein Sonderfall... die wollen wir auch liefern!
        if ("ZapfDingbats".equals(fontName)) {
            return new Font(this.load("fonts/ZapfDingbats/zapf-dingbats-bt.ttf"), size, style, color);
        }

        // LiberationSans – http://de.wikipedia.org/wiki/Liberation_(Schriftart) – http://scripts.sil.org/cms/scripts/page.php?item_id=OFL_web
        if (style == Font.NORMAL)     return new Font(this.load("fonts/Liberation/LiberationSans-Regular.ttf"),    size, Font.NORMAL, color);
        if (style == Font.BOLD)       return new Font(this.load("fonts/Liberation/LiberationSans-Bold.ttf"),       size, Font.NORMAL, color);
        if (style == Font.BOLDITALIC) return new Font(this.load("fonts/Liberation/LiberationSans-BoldItalic.ttf"), size, Font.NORMAL, color);
        if (style == Font.ITALIC)     return new Font(this.load("fonts/Liberation/LiberationSans-Italic.ttf"),     size, Font.NORMAL, color);
        return new Font(this.load("fonts/Liberation/LiberationSans-Regular.ttf"), size, style, color);
    }

    private BaseFont load(String path) {
        LogUtils.debug(PrintFontProvider.class, "path = {0}", path);
        try {
            URL fontResource = PrintSettings.class.getResource(path);
            if (fontResource == null) {
                LogUtils.warn(PrintFontProvider.class, "Kann Font {0} nicht finden... liefere null!", path);
                return null;
            }

            String fontPath = fontResource.toExternalForm();
            if (fontPath.startsWith("vfs:")) fontPath = fontPath.substring(4);
            if (fontPath.startsWith("/content/")) fontPath = fontPath.substring(fontPath.indexOf("/our/package"));

            BaseFont baseFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            baseFont.setSubset(true); // Nur die benutzen Chars einbetten!
            return baseFont;
        }
        catch (DocumentException ex) {
            LogUtils.warn(PrintFontProvider.class, "Kann Font {0} nicht erzeugen... liefere DEFAULT_FONT!", path);
        }
        catch (IOException ex) {
            LogUtils.warn(PrintFontProvider.class, "Kann Font {0} nicht erzeugen... liefere DEFAULT_FONT!", path);
        }
        return FontFactory.getFont(DEFAULT_FONT, "UTF-8", true, 8f, Font.NORMAL, PrintSettings.COLOR_TEXT).getBaseFont();
    }

    private static boolean notInitialized = true;
    public static void initFactory() {
        if (notInitialized) {
            FontFactory.defaultEmbedding = true;
            FontFactory.setFontImp(new PrintFontProvider());
            notInitialized = false;
        }
    }
}

You should call PrintFontProvider.initFactory() to make it your default.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!