iText 5 HTML + CSS to PDF / A-2:Helvetica字体未嵌入错误

问题描述:

以下代码用于使用iText5将带有CSS的HTML文件转换为PDF / A-2(此代码来自在线提供的示例):

The following code is being used for converting HTML file with CSS to PDF/A-2 using iText5 (this code is from the example provided online):

public static final String HTML = "D:\\PDFA2\\html\\sample.html";
public static final String CSS = "D:\\PDFA2\\html\\sample.css";
public static final String DEST = "D:\\PDFA2\\html\\sample.pdf";

public void createPdf(String file) throws IOException, DocumentException {

    Document document = new Document();


    PdfAWriter writer = PdfAWriter.getInstance(document, new FileOutputStream(file),PdfAConformanceLevel.PDF_A_2A);
    writer.setInitialLeading(12.5f);


    document.open();


    CSSResolver cssResolver = new StyleAttrCSSResolver();
    CssFile cssFile = XMLWorkerHelper.getCSS(new FileInputStream(CSS));
    cssResolver.addCss(cssFile);


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


    PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);


    XMLWorker worker = new XMLWorker(css, true);
    XMLParser p = new XMLParser(worker);
    p.parse(new FileInputStream(HTML));


    document.close();
}


public static void main(String[] args) throws IOException, DocumentException {
    File file = new File(DEST);
    file.getParentFile().mkdirs();
    new HTMLtoPDFA2_V2().createPdf(DEST);
}

以下是HTML文件内容:

The following is the HTML file content:

<h1>Test</h1><p>Hello World</p>

CSS文件内容为:

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

但是这会产生以下异常:

However this gives the following exception:

Exception in thread "main" com.itextpdf.text.pdf.PdfAConformanceException: All the fonts must be embedded. This one isn't: Helvetica
at com.itextpdf.text.pdf.internal.PdfAConformanceImp.checkPdfAConformance(PdfAConformanceImp.java:90)
at com.itextpdf.text.pdf.PdfAWriter.checkPdfIsoConformance(PdfAWriter.java:204)
at com.itextpdf.text.pdf.PdfWriter.checkPdfIsoConformance(PdfWriter.java:3281)
at com.itextpdf.text.pdf.PdfWriter.addSimple(PdfWriter.java:2208)
at com.itextpdf.text.pdf.PdfContentByte.setFontAndSize(PdfContentByte.java:1624)
at com.itextpdf.text.pdf.PdfDocument.writeLineToContent(PdfDocument.java:1584)
at com.itextpdf.text.pdf.PdfDocument.flushLines(PdfDocument.java:1275)
at com.itextpdf.text.pdf.PdfDocument.newPage(PdfDocument.java:869)
at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:793)
at com.itextpdf.text.Document.close(Document.java:416)
at in.test.util.pdf.HTMLtoPDFA2_V2.createPdf(HTMLtoPDFA2_V2.java:67)
at in.test.util.pdf.HTMLtoPDFA2_V2.main(HTMLtoPDFA2_V2.java:76)

如何避免这种异常?我不需要使用Helvetica字体。关于SO的帖子很多,但似乎都没有提供解决方案。

How can this exception be avoided? I do not need to use Helvetica font. There are many posts on SO but none of them seem to provide a resolution.

将字体提供程序添加到HtmlPipelineContext而不是null

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());

对于HTML文件中的每个标记,在CSS文件中定义字体

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

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

或者,为完整的html文件定义一个通用字体

Alternatively, define a common font for complete html file

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