Technical Article

Excel to PDF/A: Archival Workbook Export from Delphi

A PDF whose font dictionary reads /BaseFont /Helvetica with no FontFile entry is legal under ISO 32000-1, and practically every viewer renders it, because Helvetica is one of the 14 standard fonts a reader is expected to supply on its own. ISO 19005 rejects that same file. PDF/A wants the glyph program of every font the page uses stored inside the document, the standard 14 included, and it wants an output intent that pins down what the RGB numbers in a cell fill actually mean. A spreadsheet that went out through a print driver usually misses both, and you only find out when an archive's intake check reads the bytes

The demand rarely starts with a developer. It comes from a retention policy: financial statements, lab results, regulatory filings, records a public body must keep for decades. A workbook is a live calculation. Its archived copy has to be a fixed picture of one, and that picture has to look the same on a machine that has never seen your fonts

What makes a PDF an archival PDF?

Three properties carry most of the weight, and TXLSPDFExport in HotXLS writes all three as soon as a PDF/A profile is selected. The file must be self-contained, so every glyph comes from an embedded font. Its colour must be unambiguous, which ISO 19005 expresses as an output intent naming the colour space the file was prepared for. And it must identify itself through XMP metadata that declares the part number and the conformance level

In the bytes, that looks like this. The exporter embeds an sRGB IEC61966-2.1 ICC profile as a three-component stream with /DeviceRGB as its alternate, points a GTS_PDFA1 output intent at it with both /OutputCondition and /OutputConditionIdentifier set to sRGB IEC61966-2.1, and adds an XMP description carrying pdfaid:part and pdfaid:conformance. The header follows the part: %PDF-1.7 for PDF/A-2 and PDF/A-3, %PDF-1.4 for PDF/A-1. The ICC bytes are compiled into the library, so there is no profile file to ship next to the executable and nothing to go missing on a build server

Diagram of the three PDF/A archival requirements and the apparatus HotXLS writes from Delphi: embedded font programs, a GTS_PDFA1 sRGB output intent and pdfaid XMP identification
A PDF/A file must be self-contained, colour-unambiguous and self-identifying, and HotXLS writes the matching apparatus once a PDFAProfile is selected

The PDF/A writer lives in the lxPdfCompliance unit of the Enterprise edition. A Delphi or Lazarus project turns it on by listing that unit in a uses clause; a statically linked C++Builder application calls XlsEnablePdfCompliance once at startup. Leave it out and SaveAsPDF raises EXLSEnterpriseRequired before any output is produced. That is the right failure mode for an archive job: a loud exception beats a plain PDF sitting in the archive under an archival file name

uses
  SysUtils, lxHandleX, lxPDF, lxPdfCompliance;

procedure ExportQuarterToArchive(const Source, Target: string);
var
  Book: TXLSXWorkbook;
  Exporter: TXLSPDFExport;
begin
  Book := TXLSXWorkbook.Create;
  Exporter := TXLSPDFExport.Create;
  try
    if Book.Open(Source) <> 1 then
      raise Exception.CreateFmt('Cannot open %s', [Source]);

    Exporter.PDFAProfile := xlsPdfA2b;      // ISO 19005-2, level B
    // Fonts shipped with the application, not whatever the server has
    Exporter.SetFontFallbackChain('', False, False,
      'C:\ArchiveFonts\NotoSans-Regular.ttf');
    Exporter.SetFontFallbackChain('', True, False,
      'C:\ArchiveFonts\NotoSans-Bold.ttf');

    // Written to the Info dictionary and to dc:title / dc:creator in XMP
    Exporter.DocumentTitle := 'Quarterly results 2026 Q2';
    Exporter.DocumentAuthor := 'Group Finance';

    if Exporter.SaveAsPDF(Book, Target) <> 1 then
      raise Exception.CreateFmt('PDF/A export of %s failed', [Source]);
  finally
    Exporter.Free;
    Book.Free;
  end;
end;

Two details in that listing trip people up. The title and author go on the exporter, through DocumentTitle and DocumentAuthor; setting Title on the workbook changes the XLSX document properties and leaves the PDF metadata empty. And when SaveAsPDF writes to a file name it renders into a temporary file first and only moves it into place on success, so a failed export leaves no half-written PDF at the target path

Which PDF/A part and level should you pick?

The TXLSPDFAProfile enumeration offers xlsPdfA1a, xlsPdfA1b, xlsPdfA2a, xlsPdfA2b, xlsPdfA3a and xlsPdfA3b, plus xlsPdfANone. UsePDFA is a shortcut over the same setting: assigning True selects PDF/A-2b when no profile is set yet, assigning False resets the profile to xlsPdfANone, and reading it simply reports whether a profile is active

Level B guarantees that the visual appearance is reproducible. Level A adds a tagged structure tree and a defined reading order so assistive technology can walk the document. With HotXLS the A profiles switch tagging on by themselves, whatever TagMode says, and the cells become table structure in that tree. DocumentLanguage, which defaults to 'en-US', reaches the catalog as /Lang only when the file is tagged, so set it to the language of the content whenever you pick an A profile or set TagMode to xlsPdfTagsAutomatic

The part number changes more than the header. PDF/A-1 sits on PDF 1.4, so a PDF/A-1 export always uses the classic cross-reference table even if UseXRefStream is on; parts 2 and 3 honour UseXRefStream and pack small dictionaries into compressed object streams, which keeps long workbook exports smaller. Embedded files are where the parts really split. Part 3 allows arbitrary attachments, which is how electronic invoice formats carry machine-readable XML next to the human-readable page. Under PDF/A-1 or PDF/A-2, an export that has any attachment registered fails and SaveAsPDF returns something other than 1

For an archived spreadsheet, PDF/A-2b is the pragmatic default. It is widely accepted by archive intake, it does not impose level A's tagging discipline on a grid of cells, and it allows the compact structures. Move to PDF/A-3b only when the archive must also hold the source data, and then say what the attachment is

// PDF/A-3b: the archived PDF carries the workbook it was rendered from
// ReadFileBytes is a helper you write yourself, not part of HotXLS
function ReadFileBytes(const FileName: string): AnsiString;
var
  Stream: TFileStream;
begin
  Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  try
    SetLength(Result, Stream.Size);
    if Stream.Size > 0 then
      Stream.ReadBuffer(Result[1], Stream.Size);
  finally
    Stream.Free;
  end;
end;

...
Exporter.PDFAProfile := xlsPdfA3b;
Exporter.AddAttachment('quarterly-results.xlsx',
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  'Source workbook for this archive copy', Now,
  xlsPdfAttachmentSource, ReadFileBytes(Source));
if Exporter.SaveAsPDF(Book, Target) <> 1 then
  raise Exception.Create('PDF/A-3 export failed');

Under PDF/A-3 each attachment's file specification gets an /AFRelationship entry from the TXLSPDFAttachmentRelationship value you pass (/Source, /Data, /Alternative or /Supplement), and the catalog lists it in /AF. The short three-argument AddAttachment overload files everything as xlsPdfAttachmentData, which is wrong for a source workbook, so the longer overload is worth the extra arguments here

Every glyph through an embedded font

Every glyph a PDF/A file draws must come from an embedded font, and this is where otherwise tidy exports fall over. The file looks perfect on the machine that produced it because that machine has the font installed; a validator, or a reader on another machine, sees a font reference with no program behind it

In PDF/A mode HotXLS does not use the standard-14 shortcut at all. An ordinary export can draw plain WinAnsi text with a non-embedded Helvetica, which costs nothing in file size; with a profile set, even an ASCII digit is routed through an embedded TrueType or OpenType font. The exporter looks for a font in this order: the chain registered with SetFontFallbackChain for the cell's font family and style, the same family in regular style, the global chain registered under an empty family name, any folders supplied through OnGetFontDirectories, the file in UnicodeFontFile, and finally a set of fonts in the Windows Fonts folder, with Arial for Latin text and CJK or symbol fonts chosen by code point range

That last step is the one that bites in production. The export works on a developer workstation, then runs on a build or document server that has a different font set, and the output changes face, or the export stops. An explicit chain pointing at font files you deploy with the application removes that variable, which is why the first listing registers one. Colour-only fonts are not usable here; TrueType fonts with colour tables render through their monochrome outlines

When a code point has no glyph anywhere in the chain, the exporter draws a ? from an embedded font. When no embeddable font can be found at all, the PDF/A export fails and SaveAsPDF returns something other than 1. Refusing is the better behaviour for an archive: a non-conformant file that renders fine today is far harder to catch later than a batch that stops now. The same rule applies to an explicit opt-out. Setting FontEmbedding to xlsPdfFontEmbedNone, or returning that mode from OnFontEmbed, makes a PDF/A export fail instead of writing a map-only font. TrueType faces are subset to the glyphs actually used; OpenType files with CFF outlines are embedded whole under the default xlsPdfFontEmbedAuto, and forcing xlsPdfFontEmbedSubset on them fails the export. The mechanics of subsetting are covered in TrueType font subsetting

Diagram of the PDF/A font rule in HotXLS Delphi exports: every font the sheets use resolves to an embedded font program, and a font that cannot be resolved and embedded stops the export
In PDF/A mode every glyph comes from an embedded font and the standard-14 path is never taken, so register a font chain you deploy with the application and test on a machine without your corporate fonts

What the exporter changes on the page

A PDF/A profile changes some drawing decisions, and you can see them in the output. Fills with partial opacity are painted fully opaque, gradient fills lose their alpha, and picture shadows are omitted, because no transparency group or soft mask is written at all. PDF/A-1 forbids transparency outright; PDF/A-2 permits it under conditions, and the exporter still applies the same opaque rendering to every part, so a 2b file contains no transparency for a validator to weigh. Suppose a status column uses a 40 percent tint over white: in the archive copy it appears at full strength. That is the most visible difference between a normal export and an archival one, and it is worth a look before a dashboard-style sheet goes into the batch

The export writes the identification apparatus and keeps its own drawing inside the rules. Proving that a file meets every clause of ISO 19005 (annotation types, optional content, everything else the standard covers) is a validator's job, and a separate step. Spreadsheets seldom carry those constructs, so most exports pass. For a real archival pipeline the architecture is still export, then validate with an independent tool, with a validation failure treated as a pipeline error that stops the batch. The same split for general PDF documents is described in PDF/A preflight validation

Diagram of the export-then-validate architecture for HotXLS Delphi PDF/A output: the exporter writes the identification apparatus and an independent validator routes the file to the archive or raises a pipeline error
The exporter builds the identification apparatus while an independent validator proves conformance, and a failed validation stops the batch instead of logging a warning

Page setup decides whether the archive is readable

A conformant file that nobody can read has failed at its actual job. Page setup matters more here than in an everyday print, because nobody will be able to reflow the archive copy later. The exporter reads the worksheet's own page setup by default (UseWorksheetPageSetup is True), so the settings belong on the sheet

Fit-to-width prevents the familiar failure where a table 14 columns wide arrives as seven pages of two columns each. It has a floor, though: the fitted scale never goes below 10 percent, and it will go that low if the columns demand it. A sheet with 60 columns fitted to one page wide can therefore come out with 11-point text rendered at around 1 point, perfectly conformant and useless. Repeating title rows keep every page readable on its own, with one condition worth knowing: they repeat only when they start at the first row of the print area, so $1:$3 works with a print area beginning at row 1 and is ignored for one beginning at row 5. And when PrintArea holds several comma-separated ranges, the export uses the first

// Archive-ready page setup; Sheets is 1-based, so Sheets[0] returns nil
Sheet := Book.Sheets[1];
Sheet.PageLandscape := True;
Sheet.FitToWidth := 1;          // also switches PageFitToPages on
Sheet.FitToHeight := 0;         // as many pages tall as needed
Sheet.PrintArea := '$A$1:$N$480';
Sheet.PrintTitleRows := '$1:$3';

// Check the fitted scale before the batch commits to it
Model := Exporter.CreatePaginationModel(Book);
try
  for I := 0 to Model.Count - 1 do
    if Model.Pages[I].Scale < 0.6 then
      raise Exception.CreateFmt('Sheet %s page %d prints at %d%%',
        [Model.Pages[I].SheetName, Model.Pages[I].SheetPageNumber,
         Round(Model.Pages[I].Scale * 100)]);
finally
  Model.Free;
end;

The 0.6 threshold is a starting point you choose, not a rule from the library; pick whatever minimum your archive's readers can live with. CreatePaginationModel hands back a model you own and free, built from the exporter's current settings and the sheet's page setup. The full set of print controls is covered in protection, page setup and printing

Checks before the first batch runs

Open the first exported file in a text editor and search for pdfaid:part, pdfaid:conformance and GTS_PDFA1; check the first line for %PDF-1.7 on a part 2 or 3 export. Confirm in a viewer's font list that every font reports as embedded. Compare a few pages against what the sheet looks like in a spreadsheet application, paying attention to tinted fills and to anything printed at a small scale

Then run an independent validator over a representative sample, including a sheet with non-Latin text, and keep the report. Archives often ask for validation evidence alongside the file, and producing it at export time costs far less than reconstructing it years later. The everyday, non-archival export path and its rendering options are described in worksheet PDF export

PDF/A export, font chains and the pagination engine described here all ship in one library for Delphi and C++Builder; editions and the complete feature list are on the HotXLS Delphi spreadsheet component page