0

I'm using iText for .NET and I get a PdfAConformanceException with message:

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

How can I embed Helvetica?

This is my code

static void Main(string[] args)
{
   ConverterProperties properties = new ConverterProperties();
   properties.SetBaseUri(null);
   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 StreamReader(INTENT).BaseStream));                   

   pdf.SetTagged();

   var html = @"<!DOCTYPE html>
                <html>
                   <body>
                      <h1>My First Heading</h1>
                      <p>My first paragraph.</p>
                   </body>
                </html>
              ";

  HtmlConverter.ConvertToPdf(html, pdf, properties);
}
Mario Rossi
  • 171
  • 4
  • 9
  • Buy a license for Helvetica. iText only ships with AFM files because the PFB files are proprietary. Or **READ THE DOCUMENTATION** and learn how to use another font (such as Open Sans) so that you don't have to buy a license for your use of fonts. Also: which version of iText are you using. Since this question is a newbie question, I assume that you just started using iText, hence you are probably using the latest version which is iText 7. I'll provide an iText 7 answer. – Bruno Lowagie Apr 16 '18 at 13:01
  • [this solution](https://stackoverflow.com/questions/20534574/using-fonts-in-system-with-itextsharp) works for all fonts i could try and i don't get any error message. Could be the way you set the font that is wrong. – Franck Apr 16 '18 at 13:12
  • No @Franck, even if you sent the `embedded` parameter to `true` for Helvetica, that font won't be embedded because the PFB files for Helvetica are missing. This has been explained over and over again. See the section **Standard Type 1 fonts** in the [HTML to PDF tutorial](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-6-using-fonts-pdfhtml). – Bruno Lowagie Apr 16 '18 at 13:15
  • @BrunoLowagie In the link Helvetica is included as Standard Type1 font, does it mean there is no need to add an Helvetica-like font? Quote: "The shall in that last sentence means that you don't have to embed these fonts when creating a PDF document" – Cleptus Apr 16 '18 at 13:26
  • There is no need to embed Helvetica if you want to create a PDF file that complies with ISO 32000. However, if you want to create a PDF file that is in compliance with ISO 19005 (aka PDF/A), then **you shall embed all fonts.** You shouldn't confuse ISO 32000 with ISO 19005. – Bruno Lowagie Apr 16 '18 at 13:54
  • @BrunoLowagie I am not personally using Helvetica, But our users choose the font and our code has been working fine for the last 7 years and i didn't know there was exception. We learn new things everyday. I should probably revisit that code. – Franck Apr 16 '18 at 14:00
  • Well @Franck that exception never occurs when you create ordinary PDF files (ISO 32000). It only occurs (and *refuses to create a document*) if you try to create a PDF/A file (ISO 19005). If your users don't need PDF/A, then you don't have to worry. **What I do worry about** is that you claim that you're using iText for 7 years, and that you have external users. I'd like to see how you distribute the source code of your project (which is required if you use iText for free) or, if you didn't distribute your code for free, what your customer ID is at iText software. – Bruno Lowagie Apr 16 '18 at 14:47
  • @BrunoLowagie I don't know about ISO. I have been working with PDF for years and never once i have heard of a specific ISO requirements. As far as i know we had 2 licences running over the years, 3.1.0 and 4.1.6 for GPL and LGPL (currently) – Franck Apr 16 '18 at 15:13
  • ISO is the International Organization for Standards. If you're producing PDF's you *should* know about it, because Adobe owns a series of patents with respect to PDF that you are only allowed to use if you respect the ISO standards for PDF (and if you don't, then Adobe can sue you for patent infringement). As for the versions you are running, I hope you're not using them in a commercial context: https://developers.itextpdf.com/question/versions-older-than-5 How is it possible not to be aware of the legal implications of developing software? – Bruno Lowagie Apr 16 '18 at 15:19

1 Answers1

1

Please read the iText 7 Jump-start tutorial, more specifically Chapter 7: Creating PDF/UA and PDF/A documents!

I quote:

Creating PDFs for long-term preservation, part 1

Part 1 of ISO 19005 was released in 2005. It was defined as a subset of version 1.4 of Adobe's PDF specification (which, at that time, wasn't an ISO standard yet). ISO 19005-1 introduced a series of obligations and restrictions:

  • The document needs to be self-contained: all fonts need to be embedded; external movie, sound or other binary files are not allowed.
  • The document needs to contain metadata in the eXtensible Metadata Platform (XMP) format: ISO 16684 (XMP) describes how to embed XML metadata into a binary file, so that software that doesn't know how to interpret the binary data format can still extract the file's metadata.
  • Functionality that isn't future-proof isn't allowed: the PDF can't contain any JavaScript and may not be encrypted.

You are facing the problem that the font isn't embedded. This is because you don't provide a font program. iText ships with the font metrics of the 14 standard Type 1 fonts (there are 14 AFM files in the release). These are fonts that are supposed to be known by every PDF viewer. If you really want to use Helvetica, you need to provide the font binaries (PFB files). These can't be shipped with iText, because those files are proprietary. You need to purchase a license from the owner of the font if you want to use them.

I'm assuming that your question is wrong: "How can I embed Helvetica?" That is: that you don't want to purchase the required PFB file. As an alternative you can use a free font as is done in the tutorial:

public const String FONT = "resources/font/FreeSans.ttf";
PdfFont font = PdfFontFactory.CreateFont(FONT, PdfEncodings.WINANSI, true);
Paragraph p = new Paragraph()
    .SetFont(font).Add(new Text("Text with embedded font."));

This is a first step towards PDF/A conformance. It will solve the problem you describe in your question. However, as you don't share any code in your question (which goes against the rules of Stack Overflow), I'm assuming that you are missing plenty of other PDF/A requirements. You'll discover more about those requirements in the tutorials on the official web site.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I edit my open post. I'm trying to convert html to a PDF/A-3. How can I set a free font to ConvertToPdf method? – Mario Rossi Apr 16 '18 at 14:49
  • What's wrong with [the documentation](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/) that you refuse to read it? There's a PDF/A example in [chapter 4](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-4-creating-reports-using-pdfhtml) and fonts are discussed in [chapter 6](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-6-using-fonts-pdfhtml). Also: what makes you think you can suddenly change the scope of a question that was adequately answered? A new question requires a new post! – Bruno Lowagie Apr 16 '18 at 14:58
  • Changing a question the way you did is **NOT DONE**. You are limiting your own chances at getting an answer by doing so because people see an iText question, they see that *the original author of iText* answered it, and they say: OK, then we no longer have to look at the answer because Bruno already answered it. – Bruno Lowagie Apr 16 '18 at 14:59
  • How can this be done when trying to create PDF/A from HTML+CSS where we need to use `FontProvider`? – S_S Oct 10 '18 at 10:57