Recursive AI

Convert HTML to PDF/UA in C#

OnePdf.Html is a free, open-source .NET library from Recursive AI that converts PDF to HTML and HTML to PDF. MIT or Apache-2.0, no native dependencies.

PDF/UA is a PDF accessibility standard that uses tags to describe the document’s structure.

Terminal
dotnet add package RecursiveAI.OnePdf.Html.Render

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OnePdf.Html.Render;
 
var result = HtmlToPdf.ConvertFile(
    "report.html", "report.pdf",
    new() { Profile = HtmlPdfProfile.PdfUA1 });
Console.WriteLine($"Pages: {result.PageCount}");

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

The snippet writes report.pdf. Save this sample as report.html next to Program.cs, or use the copy in the download:

HTML
<!doctype html>
<html lang="en">
<head><title>Library hours</title></head>
<body>
  <h1>Library hours</h1>
  <ul>
    <li>Monday: 9 AM to 6 PM</li>
    <li>Tuesday: 10 AM to 6 PM</li>
  </ul>
</body>
</html>

dotnet run prints:

Text
Pages: 1

Open report.pdf to see the heading and the two list items. If a requested PDF/UA-1 rule is broken, the converter throws HtmlToPdfException with Failure set to ProfileViolation and lists the problems in its message.

Variations

Set the title and language in C#. If you can’t edit the input’s metadata, use these options as the third argument to ConvertFile:

C#
var options = new HtmlToPdfOptions
{
    Profile = HtmlPdfProfile.PdfUA1,
    Title = "Library opening hours",
    Language = "en"
};

Request PDF/A-2a as well as PDF/UA-1. Replace the profile assignment when the same file must serve both as an accessible copy and for archiving:

C#
Profile = HtmlPdfProfile.PdfA2aAndUA1

The short answer

To convert HTML to PDF/UA in C#, install RecursiveAI.OnePdf.Html.Render and call HtmlToPdf.ConvertFile with Profile = HtmlPdfProfile.PdfUA1. Give the document a title and a language. OnePdf.Html reads them from HTML’s <title> and lang by default, or from the options if you set them. Keep HtmlToPdfOptions.Tagged set to true (the default).

What is the difference between a tagged PDF and PDF/UA?

Tags describe what the content means: a heading starts a section, a list contains items, and a table header identifies the data under it. Assistive technology can use that structure in a way it can’t use text that’s simply drawn in bold or positioned in a grid. PDF/UA-1 specifies how to use tagged PDF for accessibility and has requirements beyond the presence of tags.

OnePdf.Html produces a tagged PDF by default. That is not the same as requesting the PDF/UA-1 profile: with PdfUA1, the converter writes the profile identifier and checks PDF/UA rules it can enforce. The HTML-to-PDF guide lists what the converter carries over: headings, lists, tables with header cells, figures with alternate text, and links.

How can I make the HTML source accessible before converting it?

Start with the document outline. Use a heading element for each section, in a logical order. Use list elements for a list and table header cells to label columns or rows of data. Keep the source in the order you want it read. For an informative image, write alternative text that conveys what someone needs to know from the image, not its filename. A converter can carry the description into the PDF, but it can’t tell if the description makes sense.

If conversion stops on a missing title or language, add the missing value to the source or supply it in C#. That fixes metadata; it doesn’t fix a heading made out of a styled paragraph. Review the reading order and details such as link labels and contrast as well. The PDF Association notes that PDF/UA conformance alone doesn’t ensure the content is accessible.

What is the difference between PDF/UA and PDF/A?

PDF/UA is for accessibility. PDF/A is for keeping a document usable over the long term. A request for PDF/A isn’t the same as a request for PDF/UA, even when the archival profile also requires tags. If you need both, the combined PdfA2aAndUA1 option asks the converter for both profiles in one file. The profile guide notes that these profiles build the entire PDF in memory instead of streaming it, so larger PDFs need more memory.

All onepdf-html recipes