Recursive AI

Getting started with OnePdf.Html

All onepdf-html docs

Install OnePdf.Html for .NET 10, turn an HTML page into a tagged PDF/UA-1 file, turn that PDF back into reflowing HTML and into fixed-layout HTML, read a scanned page with OCR, and do the same from the command line.

Install

OnePdf.Html targets net10.0, so you need the .NET 10 SDK. The four packages share one version; this guide uses 1.0.0.

Terminal
dotnet new console -o PdfHtmlStart
cd PdfHtmlStart
dotnet add package RecursiveAI.OnePdf.Html.Render --version 1.0.0
dotnet add package RecursiveAI.OnePdf.Html --version 1.0.0
dotnet add package RecursiveAI.OnePdf.Html.Ocr --version 1.0.0

RecursiveAI.OnePdf.Html converts PDF to HTML. RecursiveAI.OnePdf.Html.Render converts HTML to PDF; it is a separate package so that PDF to HTML users don’t get a layout engine and its fonts. RecursiveAI.OnePdf.Html.Ocr is only needed for scanned pages. All three are managed code with no native libraries.

The sample files

report.html is a one-page library report with headings, a list, a table and a link. scan-article.pdf is a picture of a page with no text layer: a scan. Download both into the project folder:

Terminal
curl -O https://recursiveai.org/onepdf-html/samples/report.html
curl -O https://recursiveai.org/onepdf-html/samples/scan-article.pdf

HTML to PDF

HtmlToPdf.ConvertFile reads the HTML file, loads its CSS, images and fonts from the file’s folder, and writes a tagged PDF. HtmlPdfProfile.PdfUA1 makes it a PDF/UA-1 file and checks it: the conversion fails if a rule is broken. PDF/UA-1 needs a title and a language, which come from the page’s <title> and <html lang>.

C#
using OnePdf.Html.Render;
 
var options = new HtmlToPdfOptions
{
    Profile = HtmlPdfProfile.PdfUA1,
    Footer = HtmlHeaderFooter.PageXOfY(),   // "Page 1 of 1"
};
HtmlToPdfResult result = HtmlToPdf.ConvertFile("report.html", "report.pdf", options);
Console.WriteLine($"{result.PageCount} page, title: {result.Title}");
Console.WriteLine($"{result.Diagnostics.Count} diagnostics, {new FileInfo("report.pdf").Length:N0} bytes");
Text
1 page, title: Harbor Street Library: October report
0 diagnostics, 23,061 bytes

The result is report.pdf. HtmlPdfProfile.PdfA2a writes PDF/A-2a instead, and PdfA2aAndUA1 writes both in one file. Page size, margins, headers and footers, and URLs as input are in the HTML to PDF guide.

PDF to HTML: semantic

PdfHtml.Convert with no options writes semantic HTML: reflowing text in reading order. report.pdf is tagged, so the headings, list and table come from its tags.

C#
using System.Text.RegularExpressions;
using OnePdf.Html;
 
string html = PdfHtml.Convert("report.pdf");
string main = html[html.IndexOf("<main>")..];
Console.WriteLine(Regex.Replace(main, "(?=<(h[1-6]|p|li|tr)[ >])", "\n"));   // a line per block
HTML
<main>
<span id="page-1" class="page-marker" role="doc-pagebreak" aria-label="Page 1"></span>
<h1><span class="wf1">October report</span></h1>
<p><span class="wf0">Harbor Street Library, branch services</span></p>
<h2><span class="wf1">Summary</span></h2>
<p><span class="wf0">Visits rose for the third month in a row. The reading room reopened on October 6 after the roof repair, and the new self-checkout desks handled more than half of all loans.</span></p>
<h2><span class="wf1">Highlights</span></h2><ul>
<li><span class="wf0">Story time moved to Saturday mornings.</span></li>
<li><span class="wf0">Twelve volunteers joined the homework club.</span></li>
<li><span class="wf0">The local history archive is now open on Tuesdays.</span></li></ul>
<h2><span class="wf1">Loans by collection</span></h2><table><thead>
<tr><th scope="col"><span class="wf1">Collection</span></th><th scope="col"><span class="wf1">September</span></th><th scope="col"><span class="wf1">October</span></th></tr></thead><tbody>
<tr><td><span class="wf0">Fiction</span></td><td><span class="wf0">4,210</span></td><td><span class="wf0">4,580</span></td></tr>
<tr><td><span class="wf0">Nonfiction</span></td><td><span class="wf0">2,975</span></td><td><span class="wf0">3,120</span></td></tr>
<tr><td><span class="wf0">Children</span></td><td><span class="wf0">3,340</span></td><td><span class="wf0">3,615</span></td></tr></tbody></table>
<p><span class="wf0">The full figures are on the</span> <a href="https://example.org/library/reports" rel="noopener noreferrer"><span class="wf0">reports page.</span></a></p>
 
</main></body></html>

The wf0 and wf1 classes set the PDF’s two fonts, written once in the document’s head as WOFF2 subsets; Fonts = PdfHtmlFontMode.CssFamilies writes no fonts and leaves the text to the reader’s. The “Page 1 of 1” footer isn’t there: the PDF marks it as page furniture, and tagged artifacts are dropped. The link keeps its https target with rel="noopener noreferrer". In Chrome, report-semantic.html looks like this:

report-semantic.html in Chrome: the heading “October report”, three section headings, a bulleted list of three items, a three-column table of loans and a linked “reports page”, as reflowing text

PDF to HTML: fixed layout

PdfHtmlMode.FixedLayout writes each page as a box the size of the page, with the text positioned and selectable in the PDF’s own glyph shapes. The path overload writes the file and returns the page count and diagnostics:

C#
using OnePdf.Html;
 
PdfHtmlResult result = PdfHtml.Convert("report.pdf", "report-fixed.html",
    new PdfHtmlOptions { Mode = PdfHtmlMode.FixedLayout });
Console.WriteLine($"{result.PageCount} page, {result.Diagnostics.Count} diagnostics");
Console.WriteLine($"report-fixed.html: {new FileInfo("report-fixed.html").Length:N0} bytes");
Text
1 page, 0 diagnostics
report-fixed.html: 21,959 bytes

report-fixed.html in Chrome, with the footer where the PDF draws it:

report-fixed.html in Chrome: one A4 page on a grey background, with the report’s headings, list, a ruled table with right-aligned numbers, and “Page 1 of 1” at the foot of the page

Scanned pages

scan-article.pdf has no text, only a picture of it. OcrTextSource from RecursiveAI.OnePdf.Html.Ocr recognizes text on such pages. In OcrMode.Auto, the default, it reads only the pages whose own text is missing or can’t be trusted. The models load the first time a page needs them; there is nothing to download.

C#
using System.Text.RegularExpressions;
using OnePdf.Html;
using OnePdf.Html.Ocr;
 
using var ocr = new OcrTextSource(new OcrTextSourceOptions { Mode = OcrMode.Auto, Languages = ["en"] });
PdfHtmlResult result = PdfHtml.Convert("scan-article.pdf", "scan-article.html",
    new PdfHtmlOptions { TextSource = ocr });
foreach (PdfHtmlDiagnostic d in result.Diagnostics)
    Console.WriteLine(d);
string html = File.ReadAllText("scan-article.html");
string main = html[html.IndexOf("<main>")..];
Console.WriteLine(Regex.Replace(main, "(?=<(h[1-6]|p|li|tr)[ >])", "\n"));   // a line per block
Text
Info: 89 words recognised from a 300 dpi picture (the page has no text layer over a picture with ink on it) [ocr-page] (page 1)
<main>
<span id="page-1" class="page-marker" role="doc-pagebreak" aria-label="Page 1"></span>
<h1>The harbour</h1>
<p>Harbour tides follow the moon, and the old pilots kept their own tables of high and low water. See the tide tables for this month.</p>
<h1>Early records</h1>
<p>Every spring the orchard behind the mill drops its blossom on the water, and the children race paper boats down the race.</p>
<h2>Later records</h2>
<p>The ledger shows a small surplus in the third quarter, mostly from repairs that were postponed rather than avoided.</p>
<p>Notes on method</p>
<p>Counts were made by hand each evening and checked against the harbour office log.</p>
 
</main></body></html>

All 89 words match the text of the page the scan was made from. With no text layer there are no tags, so the headings come from the size of the type. The package reads Latin, Greek and Cyrillic. For Chinese, Japanese, Korean, Arabic, Hebrew or the Indic languages, add RecursiveAI.OneOcr.Models.Cjk, .Arabic, .Hebrew or .Indic; no code changes.

The command line

The onepdf-html tool converts in both directions:

Terminal
dotnet tool install -g RecursiveAI.OnePdf.Html.Tool
Text
You can invoke the tool using the following command: onepdf-html
Tool 'recursiveai.onepdf.html.tool' (version '1.0.0') was successfully installed.

On linux-x64, linux-arm64, osx-arm64 and win-x64 it installs as a native executable; elsewhere it installs a framework-dependent build. You don’t name the platform: dotnet tool install picks the package. The direction follows the input, so an HTML file becomes a PDF and a PDF becomes HTML:

Terminal
onepdf-html report.html -o report.pdf --page-numbers --profile ua1
onepdf-html report.pdf -o report-semantic.html
onepdf-html report.pdf --mode fixed -o report-fixed.html
onepdf-html scan-article.pdf --ocr auto -o scan-article.html
Text
onepdf-html: wrote 1 page(s) to report.pdf
onepdf-html: wrote 1 page(s) to report-semantic.html
onepdf-html: wrote 1 page(s) to report-fixed.html
onepdf-html: wrote 1 page(s) to scan-article.html

The four files are byte-identical to the ones the programs above wrote. Without -o, a PDF becomes <input>.html beside it, so onepdf-html report.pdf here would replace report.html. -v lists every diagnostic. Every option is in the command-line reference.

Next

  • PDF to HTML: every option, the semantic mapping, fixed-layout output, files per page, sinks, text sources and OCR.
  • HTML to PDF: page size, margins, headers and footers, URLs, resources and the network, PDF/A, batches.
  • Diagnostics and limits: every diagnostic code and the bounds on untrusted input.