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);
}
The following is the HTML file content:
<h1>Test</h1><p>Hello World</p>
And the CSS file content is:
{
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)
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.
font-family: "Times New Roman", Times, serif;
? Arial and Helvetica are interchangeable, and my guess is that iText may be checking for a backup font family. So you probably need to specify a backup family, or test another font. – Befit