Lay out each HTML document separately, then append its pages to one PDF.
dotnet add package RecursiveAI.OneRender.PdfRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneRender.Image; using OneRender.Paged; using OneRender.Painting; using OneRender.Pdf; string[] documents = ["<h1>Invoice</h1><p>Total: 25 USD</p>", "<h1>Terms</h1><p>Payment due on receipt.</p>"]; using var output = File.Create("combined.pdf"); using var writer = new DisplayListPdfWriter(output, new PdfOutputOptions { Profile = PdfProfile.None, Tagged = false }); foreach (string html in documents) { var document = PagedDocument.Create(html, new PagedOptions(FontProviders.ForCurrentThread())); var list = new DisplayList(); document.PaintContent(list); for (int i = 0; i < document.PageCount; i++) { var page = document.GetPage(i); var size = page.Geometry; var setup = new PdfPageSetup(size.Width, size.Height, size.MarginTop, size.MarginRight, size.MarginBottom, size.MarginLeft); writer.AddPage(list, setup, page.ContentRegion, document.GetPageLayer(i)); } } writer.Finish(); Console.WriteLine($"combined.pdf: {writer.PageCount} pages");
Both inputs are inline: an invoice and its terms. Each gets its own layout and page regions. A display list is the recorded drawing commands for that layout. This example writes an untagged PDF with no archival or accessibility identification.
combined.pdf: 2 pagesUnzip the example, then dotnet run.
Variations
Replace the document array to reverse the order. The program still prints combined.pdf: 2 pages, with terms first.
string[] documents = ["<h1>Terms</h1><p>Payment due on receipt.</p>", "<h1>Invoice</h1><p>Total: 25 USD</p>"];
Load local HTML files from a reports folder in filename order by replacing the documents array with this line. With the invoice in 01-invoice.html and the terms in 02-terms.html, it prints combined.pdf: 2 pages.
string[] documents = Directory.GetFiles("reports", "*.html").OrderBy(path => path, StringComparer.Ordinal).Select(File.ReadAllText).ToArray();Related recipes
The short answer
Create one DisplayListPdfWriter, lay out each HTML document with PagedDocument.Create, and call AddPage for every page region in each resulting display list. Call Finish once after the last document. This produces one PDF without generating intermediate PDFs or merging existing PDF files.
The example writes an invoice followed by terms. The two documents are independent inputs, so each begins on its own page even when the invoice leaves unused space. This is page assembly, not continuous text flow across document boundaries.
How to combine multiple HTML files into one PDF?
Keep the output stream and PDF writer outside the document loop. Inside it, create a paged layout and paint its content into a display list. Iterate through that document’s pages, passing the page setup, content region and page layer to AddPage. The region matters: paged content is arranged side by side in the retained display list, so writing the same region repeatedly would not select each successive page.
The page layer contains page backgrounds and margin-box content. Passing it along preserves those parts of the page rather than copying just its content rectangle. The sample uses the geometry returned for each page, including its margins, instead of assuming every page has the same dimensions.
For local files, read their text first and put those strings into the documents array. Order that array deliberately. The writer adds pages in the order you call it; it does not sort by title or filename. The variation reverses the two small inputs without changing the writing loop.
How can I convert multiple HTML files to PDF in bulk?
Decide whether you want one combined output or one output per source. The shared writer in this recipe gives one output. A separate call to HtmlPdf.ToPdf for each source is the appropriate shape for separate files. Neither task requires joining complete HTML documents into a single markup string.
Independent layouts also keep the source documents’ styles separate. If an invoice and its terms have different page rules, each layout supplies its own page geometry. Concatenating entire HTML documents would not express that same isolation: their style rules and document structure would instead need to be reconciled before layout.
This example uses Profile = None and Tagged = false. It does not claim combined PDF/A or PDF/UA output. The PDF backend documentation distinguishes streaming output from identified profiles that need a finished document and a re-save. Keep those concerns separate from simply appending pages.
How can I combine multiple HTML files into one single file?
If the desired single file is a PDF, assemble the rendered pages as shown here. If you mean one HTML document, this recipe does not concatenate or rewrite the source files. Retain the original files when you need editable HTML as well as a printable deliverable.
The inline inputs have no external resources, so the zip runs without network access or sample downloads. Real reports may reference images and stylesheets; resolve those at layout time and give the writer any image resolver it needs. Appending a page cannot recover an asset that was absent from its display list. For long documents, expect the current document’s layout and display list to remain in memory even though profile None streams PDF output.