Convert between PDF and HTML in .NET, in both directions.
- PDF to HTML, two ways. 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. 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.
- 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.
- Scanned pages. An optional OCR package recognises text on scans and on pages whose text layer is broken, in both PDF to HTML modes.
Pure managed code on OnePDF, OneImage, OneHtml and OneRender. net10.0, NativeAOT- and trim-compatible, no native libraries, no script and no network use unless you ask for it.
Install
Command line (a native executable for linux-x64, linux-arm64, osx-arm64 and win-x64, so a short document converts in tens of milliseconds; a framework-dependent build for every other platform):
dotnet tool install -g RecursiveAI.OnePdf.Html.ToolLibraries:
dotnet add package RecursiveAI.OnePdf.Html # PDF to HTML dotnet add package RecursiveAI.OnePdf.Html.Ocr # optional: recognise text on scanned pages (Latin, Greek, Cyrillic) dotnet add package RecursiveAI.OneOcr.Models.Cjk # optional: also Chinese, Japanese, Korean (.Arabic, .Hebrew, .Indic likewise) dotnet add package RecursiveAI.OnePdf.Html.Render # HTML to PDF
| Package | What it is | Pulls in |
|---|---|---|
RecursiveAI.OnePdf.Html | The PDF to HTML converter. | OnePdf, OnePdf.Fonts, OneFont, OneImage, OneImage.Pdf, OneHtml |
RecursiveAI.OnePdf.Html.Ocr | A text source that recognises text on scanned pages and on pages with a broken text layer. | OnePdf.Html, OneOcr (and its models, which carry the model weights) |
RecursiveAI.OnePdf.Html.Render | HTML to PDF. A separate package, so PDF to HTML users do not get a layout engine and its fonts. | OneRender.Pdf (OneRender, its image back end and OFL fonts), OneHtml |
RecursiveAI.OnePdf.Html.Tool | The onepdf-html command, both directions. | The three packages above |
All four share one version. THIRD-PARTY-NOTICES.md lists what ships inside them.
PDF to HTML in a minute
using OnePdf.Html; string html = PdfHtml.Convert("report.pdf"); // reflowing HTML, as text PdfHtml.Convert("report.pdf", "fixed.html", // fixed-layout pages, one file new PdfHtmlOptions { Mode = PdfHtmlMode.FixedLayout }); PdfHtml.Convert("report.pdf", "pages", // a file per page, images and fonts as files new PdfHtmlOptions { Layout = PdfHtmlOutputLayout.PagePerFile, Assets = PdfHtmlAssetMode.Files, PageRanges = PdfHtmlPageRanges.Parse("1-2") });
Per-page control and your own output go through PdfHtmlConverter:
using OnePdf.Html; using PdfHtmlConverter converter = PdfHtmlConverter.Open("report.pdf", new PdfHtmlOptions { Password = "secret" }); PdfHtmlPage page = converter.ConvertPage(0); // one page: HTML fragment, assets, diagnostics using var sink = new PdfHtmlFileSink("whole.html"); converter.ConvertTo(sink); // any IPdfHtmlSink; pages arrive in order foreach (PdfHtmlDiagnostic d in converter.Diagnostics) { Console.WriteLine(d); // repairs and dropped content }
The guide, docs/pdf-to-html.md, covers every option, the semantic mapping, fixed-layout output, sinks, text sources and OCR.
HTML to PDF in a minute
using OnePdf.Html.Render; byte[] pdf = HtmlToPdf.ConvertFile("page.html"); // relative CSS, images and fonts load from the file's folder HtmlToPdf.ConvertFile("page.html", "letter.pdf", new HtmlToPdfOptions { PageSize = HtmlPageSize.Letter, Orientation = HtmlPageOrientation.Landscape, Margins = HtmlMargins.FromMillimeters(20), Header = new HtmlHeaderFooter { Left = "Quarterly report", Right = "<span class=\"title\"></span>" }, Footer = HtmlHeaderFooter.PageXOfY(), // "Page 3 of 12" on every page Profile = HtmlPdfProfile.PdfA2aAndUA1, // PDF/A-2a and PDF/UA-1 in one file });
HtmlToPdf.ConvertUrl fetches a page and its resources, ConvertFiles and ConvertAll run batches in parallel, and HtmlToPdfConverter is the low level. The guide, docs/html-to-pdf.md, covers every option.
Command line
onepdf-html report.pdf # report.html beside the input (reflowing) onepdf-html report.pdf --mode fixed -o fixed.html onepdf-html report.pdf --per-page --assets files --pages 1-5 onepdf-html scan.pdf --ocr auto # recognise text where a page has none onepdf-html page.html # page.pdf beside the input onepdf-html https://example.org/ -o page.pdf --page-size A4 --landscape --page-numbers --profile ua1 onepdf-html a.html b.html -o out/ # several files in parallel cat page.html | onepdf-html - -o page.pdf
The direction follows the input: a PDF becomes HTML; an HTML file, a URL or standard input becomes a PDF. Every option is in docs/cli.md.
The two PDF to HTML modes
| Semantic (default) | Fixed layout | |
|---|---|---|
| Use it for | Reading, search, accessibility, content extraction, republishing | Looking like the PDF in a browser |
| Output | h1..h6, p, ul/ol, table, figure, aside, links; in reading order | One box per page: SVG vector graphics, images, selectable text in positioned, transformed lines |
| Structure from | The tag tree of a tagged PDF; otherwise inferred (headings from the outline and type sizes, hanging-indent lists, ruled and unruled tables, footnotes, captions) | Not applicable; the text is the extraction's text, so copying a selection gives the page's text |
| Fonts | The reader's own, or the PDF's glyph shapes as WOFF2 subsets built once per document (Fonts, default web fonts) | The PDF's glyph shapes as WOFF2 subsets, or CSS families chosen from the font name |
| What cannot be expressed | Vector figures become pictures cut from the page | Mesh and function shadings, a few pattern forms and some masks come from a page picture, always reported in a diagnostic |
Tagged and untagged pages, partly tagged pages, headers and footers (marked, kept or dropped), page boundaries (markers or sections), form fields (text or controls), annotations and right-to-left text are described in the guide.
Safe by construction
PDF files are untrusted input, and HTML is executable in a browser, so the output is built to be inert:
- It never contains script and never refers to a remote resource. Nothing is fetched.
- Every piece of text and every attribute value goes through one serialiser (OneHtml's), which escapes it. No markup is built by concatenating document text.
- Only
http,https,mailtoand in-document link targets are kept.javascript:,file:,data:and the rest are dropped with a diagnostic, and PDF JavaScript and actions are never converted. - Parsing and output are bounded: OnePDF's
PdfLimitsplusMaxInputBytes,MaxOutputBytesPerPage,MaxAssetsPerPageandMaxRasterPixels. - The same input and options give byte-identical output at any degree of parallelism.
HTML to PDF runs no script either. A document reads files only below its input's folder, and reads the network only when the input is a URL or you turn it on, with http/https only, size, count, time and redirect limits, and loopback, link-local and private addresses refused unless you allow them.
Comparison with pdf2htmlEX
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. The measured comparison is made with the harness in tools/OnePdf.Html.Eval, which runs both converters as external commands on the same pages: Chrome opens each converter's HTML, and the result is compared with the OneImage.Pdf render of the page (visual similarity), with the text OnePDF extracts (text equality), and by output size and conversion time.
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.
Method. Quality: tools/OnePdf.Html.Eval fixed on the set above, one full session shortly before the release candidate (the fixed-layout code differs from the release only by the picture-piece limit and the glyph-width and code-point guards listed in the changelog, which touch no page of the set except by the rows they are written for), on a Ryzen 9 7950X3D machine (Linux 7.0), Chrome 155.0.8059.84 headless, the NativeAOT onepdf-html tool, pdf2htmlEX 0.18.8.rc1 with its default flags (--dest-dir, no other options). Each page is rendered by Chrome from each converter's HTML and compared with the OneImage.Pdf render of the page at 96 dpi (SSIM and ink mismatch); the text is compared with OnePDF's extraction of the page. The quality session ran with other jobs on the machine, so its times are not published. Speed: one process per file, the pages the set lists (the whole file where it says all), converting from a file to a file, three repetitions, the median of the repetitions per file; wall time from /usr/bin/time and peak resident set (%M) of the process, run alone on the machine (a bench lease on a 16-core, 32-thread machine; load average 3 to 5 from the machine's idle services before, during and after). Process start-up is included in both, which favours a native tool. The speed rows come from tools/OnePdf.Html.Eval/scripts/speed.sh and speed-summary.py. One file (raw-broken-truncated-tail.pdf) gives pdf2htmlEX no output and is left out of its rows. Numbers from one machine and one session are indicative of the ratio, not of any other machine.
Semantic output has no counterpart in pdf2htmlEX, so it is judged on its own: structure accuracy against tagged PDFs (tags hidden for the untagged run) and reading order against OnePDF's extraction.
Semantic results on the 54 files of testdata/eval/semantic.csv (generated documents with exact truth, and real producers' tagged files with the tags removed before conversion), 7,415 truth items; F1 % per kind, higher is better:
| Kind | Truth items | F1 |
|---|---|---|
| Heading (with level) | 171 | 74.8 |
| Paragraph | 836 | 80.8 |
| List | 72 | 66.2 |
| List item | 198 | 79.9 |
| Table | 97 | 85.9 |
| Table cell (position, spans, header) | 5,861 | 77.7 |
| Figure | 155 | 72.5 |
| Caption | 6 | 70.6 |
| Link | 17 | 97.1 |
| Note | 2 | 100.0 |
| All kinds | 7,415 | 77.9 |
The generated documents and the ruled, partly ruled, unruled, continued and beside-text table sets score 100.0; the real producers' files carry the loss (slides 69.5, tables 55.5, rotated pages 50.5). Reading order against the truth text (content error / order error, lower is better): 3.2 % / 4.4 % overall, 0.0 / 0.0 on the generated documents. Figure alt text is not recovered from untagged files (0 of 8). One session on the machine of the table above, with other jobs running (load 14 on a 16-core machine), so no time is published for it.
Limits of this version
- Text on a PDF page is converted as the PDF's text layer says. Without OCR, a scan stays a picture of text.
- OCR reads Latin, Greek and Cyrillic script, and Chinese, Japanese, Korean, Arabic, Persian, Urdu, Hebrew and nine Indic languages once the matching
RecursiveAI.OneOcr.Models.*package is added (measured: scanned CJK pages 0.4 % text error, Arabic and Hebrew 2.9 %). Other scripts, such as Thai, are left unread. - In semantic output, a figure drawn with paths becomes a picture that includes any text inside its area (kept as hidden text for search and screen readers).
- Fixed layout does not simulate overprint, does not apply a shading pattern's graphics state, and draws a knockout group that holds transparent content from a page picture. Meshes and function shadings, some patterns and some masks also come from a page picture. Each case is a diagnostic.
- Table detection in untagged files is heuristic: it refuses charts, prose columns and line-number grids on purpose, and a ruled form can still be taken for a table.
- HTML to PDF follows what OneRender supports of CSS for print; the document's own
@pagerules apply unless you set the options.
See docs/diagnostics.md for how each case is reported and docs/architecture.md for how the converter is built.
Documentation
- PDF to HTML guide
- HTML to PDF guide
- Command-line reference
- Diagnostics and limits
- Architecture (for maintainers)
- Changelog
Every code sample in the README and the guides is compiled and run by tests/OnePdf.Html.Docs.Tests.
Build
dotnet build OnePdf.Html.slnx dotnet test --solution OnePdf.Html.slnx dotnet publish tests/OnePdf.Html.AotSmoke -r linux-x64 # NativeAOT check; use your own RID dotnet publish tests/OnePdf.Html.Render.AotSmoke -r linux-x64
Set ONEPDF_HTML_ARTIFACTS to move build output out of the checkout.
Licensed under either of MIT or Apache-2.0 at your option. Copyright Recursive AI, LLC.