0
public const String INTENT = "sRGB Color Space Profile.icm";
static void Main(string[] args)
{
    String HTML = "<h1>Test</h1><p>Hello World</p>";            
    PdfWriter writer = new PdfWriter("hello.pdf");
    PdfADocument pdf = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_3A, new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", new FileStream(INTENT, FileMode.Open)));
    pdf.SetTagged();
    ConverterProperties properties = new ConverterProperties();
    properties.SetBaseUri("");
    HtmlConverter.ConvertToPdf(HTML, pdf, properties);
}

When I run this code, I get a PdfAConformanceException "All the fonts must be embedded. This one is not: Times-Bold".

I try to get the registered fonts through these lines:

var fonts = "";
foreach (string fontname in iTextSharp.text.FontFactory.RegisteredFonts)
{
    fonts += fontname + " ";
}

and i get: "courier courier-bold courier-oblique courier-boldoblique helvetica helvetica-bold helvetica-oblique helvetica-boldoblique symbol times-roman times-bold times-italic times-bolditalic zapfdingbats", so times-bold is in there.

Am I missing something?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mario Rossi
  • 171
  • 4
  • 9
  • You're asking: "Am I missing something?" Yes, you missed [this comment](https://stackoverflow.com/questions/49857928/when-creating-a-pdf-a-file-i-get-the-error-helvetica-is-not-embedded#comment86735766_49858161). As explained in both answers to your question, you should read the tutorials. Plenty of hours went into writing that documentation and the examples that go with it, but what use are they if people don't read them? – Bruno Lowagie Apr 17 '18 at 07:52

2 Answers2

4

The issue here is that there are 14 default fonts in PDF and the default fonts aren't embedded by pdfHtml/iText by default. You can tell pdfHtml to "skip" registering the default fonts by instantiating and configuring a FontProvider like this:

DefaultFontProvider fontProvider = new DefaultFontProvider(false, true, true);
properties.setFontProvider(fontProvider);

This will produce a PDF file with an embedded subset of "Freesans".

See the following for more information:

Michaël Demey
  • 1,567
  • 10
  • 18
1

When you ask iText for the registered fonts, it gives you all the fonts that are known by the font factory. That means that you can use all of those fonts when creating an ordinary PDF file (ISO 32000).

However, not all of the registered fonts are fonts that can be embedded in a PDF, and embedding a font is one of the requirements of PDF/A (ISO 19005). Allow me to quote from chapter 1 of the building blocks tutorial:

iText supports the Standard Type 1 fonts, because the io-jar contains the Adobe Font Metrics (AFM) files of those 14 fonts. These files contain the metrics that are needed to calculate the width and the height of words and lines. This is needed to create the layout of the text.

If we want to embed a font, we need a font program. In the case of the Standard Type 1 fonts, this font program is stored in PostScript Font Binary (PFB) files. In the case of the 14 standard Type 1 fonts, those files are proprietary; they can't be shipped with iText because iText Group doesn't have a license to do so. We are only allowed to ship the metrics files.

As a consequence, iText can't embed these 14 fonts, but this doesn't mean that iText can't embed fonts.

All the fonts you list as registered fonts are Standard Type 1 fonts. You can't use those fonts in a PDF/A file if you don't have the corresponding PFB files. You should provide font programs (e.g. .ttf or .otf files) and make sure those are used when converting HTML to PDF.

How to do this? That's also explained in a tutorial on the official web site. See chapter 4 of the HTML to PDF tutorial. In that example, we defined the font like this:

<body style="font-family: FreeSans">

As documented in chapter 7 of the same tutorial, the font FreeSans is shipped with the pdfHTML add-on.

Change your HTML snippet to:

<body style="font-family: FreeSans"><h1>Test</h1><p>Hello World</p></body>

This way, you'll avoid using a Standard Type 1 font that isn't embedded, and you'll comply with the requirements of PDF/A.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165