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.
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.; PDF to HTML is RecursiveAI.OnePdf.. The input is report.html.
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)}"); }
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
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. “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., .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.ToolCompared 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.Html | pdf2htmlEX |
|---|---|---|
| Visual similarity to the page render (SSIM, mean of pages; higher is better) | 0.980 | 0.887 |
| Ink that differs from the render, % (lower is better) | 1.4 | 26.1 |
| Text content error, % against the page’s text (lower is better) | 0.4 | 9.9 |
| Reading-order error, % (lower is better) | 0.7 | 18.5 |
| HTML per page, KiB (total bytes over total pages) | 41 | 147 |
| Speed (bench session) | OnePdf.Html | pdf2htmlEX |
|---|---|---|
| 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 packages
- .Html
RecursiveAI.OnePdf.is the PDF to HTML converter, built on onepdf, oneimage, onefont and onehtml. 1.0.0.Html - .Html.Ocr
RecursiveAI.OnePdf.is “a text source that recognises text on scanned pages and on pages with a broken text layer.” It brings oneocr and its models. 1.0.0.Html. Ocr - .Html.Render
RecursiveAI.OnePdf.converts HTML to PDF. “A separate package, so PDF to HTML users do not get a layout engine and its fonts.” 1.0.0.Html. Render - .Html.Tool
RecursiveAI.OnePdf.is “theHtml. Tool onepdf-htmlcommand, both directions,” with a framework-dependent build for platforms that have no native one. 1.0.0. - .NET
.NET 10. “Pure managed code”, “NativeAOT- and trim-compatible, no native libraries.” All four packages share one version. MIT or Apache-2.0.