将 HTML 转换为 PDF/A 时,我得到 "All the fonts must be embedded. This one is not: Times-Bold"

When converting HTML to PDF/A, I get "All the fonts must be embedded. This one is not: Times-Bold"

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);
}

当我 运行 这段代码时,我得到一个 PdfAConformanceException "All the fonts must be embedded. This one is not: Times-Bold"。

我尝试通过这些行获取注册的字体:

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

我得到:"courier courier-bold courier-oblique courier-boldoblique helvetica helvetica-bold helvetica-oblique helvetica-boldoblique symbol times-roman times-bold times-italic times-bolditalic zapfdingbats",所以里面有倍粗体。

我是不是漏掉了什么?

这里的问题是 PDF 中有 14 种默认字体,默认情况下 pdfHtml/iText 没有嵌入默认字体。您可以通过像这样实例化和配置 FontProvider 来告诉 pdfHtml "skip" 注册默认字体:

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

这将生成一个包含 "Freesans".

嵌入子集的 PDF 文件

有关详细信息,请参阅以下内容:

当您向 iText 询问已注册的字体时,它会为您提供字体工厂已知的所有字体。这意味着您可以在创建普通 PDF 文件 (ISO 32000) 时使用所有这些字体。

但是,并非所有已注册的字体都是可以嵌入到 PDF 中的字体,嵌入字体是 PDF/A (ISO 19005) 的要求之一。请允许我引用 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.

您列为注册字体的所有字体都是标准 Type 1 字体。如果您没有相应的 PFB 文件,则不能在 PDF/A 文件中使用这些字体。您应该提供字体程序(例如 .ttf.otf 文件)并确保在将 HTML 转换为 PDF 时使用这些程序。

如何做到这一点? official web site. See chapter 4 of the HTML to PDF tutorial 上的教程中也对此进行了解释。在那个例子中,我们定义了这样的字体:

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

chapter 7 of the same tutorial 中所述,FreeSans 字体随 pdfHTML 附加组件一起提供。

将您的 HTML 代码段更改为:

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

这样,您将避免使用未嵌入的标准 Type 1 字体,并且您将符合 PDF/A 的要求。