Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get rid of Helvetica in iText XMLWorker?
    primarykey
    data
    text
    <p>We're using iText to generate PDF files from Java code, which works pretty well in most cases. A few days ago we started to generate PDF/A instead of normal PDF files which needs to embed all fonts. The iText <code>Document</code> is mostly build of custom <code>PdfPTable</code> and other classes where we control the fonts directly. All used fonts are created from TTF files loaded via the following code - which works just fine:</p> <pre><code> private BaseFont load(String path) { try { URL fontResource = PrintSettings.class.getResource(path); if (fontResource == null) { return null; } String fontPath = fontResource.toExternalForm(); BaseFont baseFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); baseFont.setSubset(true); return baseFont; } catch (DocumentException ex) { Logger.getLogger(PrintSettings.class).warn("..."); } catch (IOException ex) { Logger.getLogger(PrintSettings.class).warn("..."); } return FontFactory.getFont(PrintSettings.FONT, "UTF-8", true, 8f, Font.NORMAL, PrintSettings.COLOR_TEXT).getBaseFont(); } </code></pre> <p>Now we use one specific content type in the PDF which generates from HTML code. We use the <code>XMLWorker</code>to handle that part. This worked just fine, as long as we didn't embed the fonts. But with PDF/A we <strong>need</strong> to embed all fonts and now we struggle with an unknown source of <strong>Helvetica</strong> usage.</p> <p>We've tried to solve this by using our own <strong>FontProvider</strong> class like this one:</p> <pre><code>public class PrintFontProvider extends FontFactoryImp { @Override public Font getFont(String fontName, String encoding, boolean embedded, float size, int style, BaseColor color, boolean cached) { // 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) { ... } } </code></pre> <p>It's connected with the <code>XMLWorker</code> using the following code:</p> <pre><code>HtmlPipelineContext html = new HtmlPipelineContext(null); html.setTagFactory(Tags.getHtmlTagProcessorFactory()); CSSResolver css = XMLWorkerHelper.getInstance().getDefaultCssResolver(true); // We need to control the FontProdiver! html.setCssAppliers(new CssAppliersImpl(new PrintFontProvider())); Pipeline&lt;?&gt; pipeline = new CssResolverPipeline(css, new HtmlPipeline(html, new PdfWriterPipeline(this.document, writer))); XMLWorker worker = new XMLWorker(pipeline, true); XMLParser p = new XMLParser(worker); p.parse(new ByteArrayInputStream(StringUtils.iTextHTML(string).getBytes())); </code></pre> <p>Most simple HTML elements work this way... but there are some which seem to ignore the FontProvider and keep using <strong>Helvetica</strong> which won't be embedded in the PDF/A (we don't have that font). For example <code>&lt;ol&gt;&lt;li&gt;...&lt;/li&gt;&lt;/ol&gt;</code> make use of this.</p> <pre><code>Caused by: com.itextpdf.text.pdf.PdfXConformanceException: All the fonts must be embedded. This one isn't: Helvetica at com.itextpdf.text.pdf.internal.PdfXConformanceImp.checkPDFXConformance(PdfXConformanceImp.java:225) at com.itextpdf.text.pdf.PdfWriter.addSimple(PdfWriter.java:2192) at com.itextpdf.text.pdf.PdfContentByte.setFontAndSize(PdfContentByte.java:1444) at com.itextpdf.text.pdf.PdfDocument.writeLineToContent(PdfDocument.java:1463) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:968) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:841) at com.itextpdf.text.pdf.ColumnText.showTextAligned(ColumnText.java:1189) at com.itextpdf.text.pdf.ColumnText.showTextAligned(ColumnText.java:1208) at com.itextpdf.text.pdf.PdfDocument.flushLines(PdfDocument.java:1193) at com.itextpdf.text.pdf.PdfDocument.newPage(PdfDocument.java:830) at com.itextpdf.text.Document.newPage(Document.java:367) </code></pre> <p>I've run out of ideas how to get rid of Helvetica for now... trying to solve this for 8+ hours now... any more ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload