Search code examples
itextitextpdf

how should one embed Helvetica to make itext tutorial example work?


I am trying to make the code at PDFA tutorial work by adapting it to Groovy, and as soon as I add some content to my PDF I get a PdfAConformanceError

All the fonts must be embedded. This one isn't. Helvetica

Should the tutorial help me here or am I just supposed to already know what to do? As I am not sure I do, what would be the best method?


Solution

  • As the example shows, you have to make sure all fonts are embedded.

    Create an embedded font:

    Font bold10 = FontFactory.getFont(
        "./src/main/resources/com/itextpdf/FreeSansBold.ttf",
        BaseFont.WINANSI, BaseFont.EMBEDDED, 10);
    

    Add content using that embedded font:

    document.add(new Paragraph("Invoice number: " + invoice.getNumber(), bold10));
    

    Note that Helvetica is the default font in iText, but the font program is not included. It is typically also not available on Windows systems. This is because of licensing.

    Even if you have a font program available, you have to make sure that you are allowed to distribute it by embedding it in a PDF file.