Recursive AI
LibraryC#Available

onepdf-html

Convert between PDF and HTML in .NET, in both directions: semantic or fixed-layout HTML from a PDF, and a tagged PDF from HTML.

dotnet add package RecursiveAI.OnePdf.Html --version 1.0.0

A quick look

An HTML report becomes a PDF/UA-1 file, and the PDF becomes reflowing HTML again. The table comes back as a table, with its header row of th cells, from the PDF’s tags.

HTML to PDF is RecursiveAI.OnePdf.Html.Render; PDF to HTML is RecursiveAI.OnePdf.Html. The input is report.html.

Getting started
Program.csC#
using System.Text.RegularExpressions;
using OnePdf.Html;
using OnePdf.Html.Render;
 
// HTML to a tagged PDF/UA-1 file
HtmlToPdf.ConvertFile("report.html", "report.pdf",
    new HtmlToPdfOptions { Profile = HtmlPdfProfile.PdfUA1 });
 
// and back to HTML: print the table, one row per line
string html = PdfHtml.Convert("report.pdf");
foreach (Match row in Regex.Matches(html, "<tr>.*?</tr>"))
{
    var cells = Regex.Matches(row.Value, "<(t[hd])[^>]*>(?:<[^>]+>)*([^<]*)");
    var text = cells.Select(c => c.Groups[2].Value);
    Console.WriteLine($"{cells[0].Groups[1]}: {string.Join(" | ", text)}");
}
Output
th: Collection | September | October
td: Fiction | 4,210 | 4,580
td: Nonfiction | 2,975 | 3,120
td: Children | 3,340 | 3,615
What it does

What it does

PDF to semantic HTML

Semantic output is “clean, reflowing HTML in reading order: headings, paragraphs, lists, tables, figures, links and notes, built from the PDF’s tags or inferred from its layout.”

PDF to fixed-layout HTML

Fixed-layout output “looks like the page: positioned, selectable text in the PDF’s own glyph shapes over vector graphics and images, one box per page.” The glyphs are WOFF2 subsets; vector content is SVG.

HTML to PDF

“A string, file, stream or URL in, a tagged PDF out, with page size, margins, headers and footers, links, an outline, and PDF/UA-1 or PDF/A-2a.” Layout is onerender’s.

Scanned pages

RecursiveAI.OnePdf.Html.Ocr “recognises text on scans and on pages whose text layer is broken, in both PDF to HTML modes,” with oneocr. Latin, Greek and Cyrillic are built in; add RecursiveAI.OneOcr.Models.Cjk, .Arabic, .Hebrew or .Indic for those scripts.

Safe output

The HTML “never contains script and never refers to a remote resource. Nothing is fetched.” Only http, https, mailto and in-document links are kept. HTML to PDF runs no script and reads the network only for a URL input or when you turn it on.

Command line

onepdf-html converts in both directions: “a PDF becomes HTML; an HTML file, a URL or standard input becomes a PDF.” It installs as a native executable on linux-x64, linux-arm64, osx-arm64 and win-x64.

dotnet tool install -g RecursiveAI.OnePdf.Html.Tool

Compared with pdf2htmlEX

From the README at v1.0.0 (README.md:126–145): “pdf2htmlEX is a native command-line program that makes fixed-layout HTML; OnePdf.Html is a managed library (and tool) with two output modes and the reverse direction.”

“Fixed layout, 100 pages of the 62 files in testdata/eval/fixed.csv (13 categories: text, multi-column, tables and math, forms, images, vector maps, transparency, Type 3, CJK, right-to-left, rotated, scans with a text layer, repaired broken files):”

Quality (harness session)OnePdf.Htmlpdf2htmlEX
Visual similarity to the page render (SSIM, mean of pages; higher is better)0.9800.887
Ink that differs from the render, % (lower is better)1.426.1
Text content error, % against the page’s text (lower is better)0.49.9
Reading-order error, % (lower is better)0.718.5
HTML per page, KiB (total bytes over total pages)41147
Speed (bench session)OnePdf.Htmlpdf2htmlEX
Conversion time, ms per page, median of the 62 files (mean in brackets)15 (30)80 (113)
Peak memory of one conversion, MiB, median of the files (largest)36 (137)32 (59)

“The memory row is not a win: pdf2htmlEX’s resident set is a little smaller, because OnePdf.Html draws a page picture for anything a browser cannot express and holds it while the page is converted.”

“Numbers from one machine and one session are indicative of the ratio, not of any other machine.”

The method and the semantic results

The packages

Go further