HTML to PDF Converter ASP.NET Core samples · .NET 10 and .NET 11

ExpertPdf Html To Pdf Converter for .NET - ZUGFeRD / Factur-X Electronic Invoice - C# / ASP.NET Sample

A ZUGFeRD or Factur-X invoice is a hybrid invoice: a PDF/A-3 document that people read, with the same invoice embedded as structured XML that accounting software reads without retyping. This sample converts an HTML invoice to PDF/A-3 and embeds its Factur-X XML (profile EN 16931) with Document.AddZugferdInvoice.

With the Chromium or the Blink engine the invoice is PDF/A-3a, which is also tagged and accessible; with the WebKit engine it is PDF/A-3b.

Invoice page:

Rendering engine:


C# code of this sample

Pages/ZugferdInvoice.cshtml.cs
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using ExpertPdf.HtmlToPdf;
using ExpertPdf.HtmlToPdf.PdfDocument;

namespace ExpertPdf.Samples.Pages
{
    public class ZugferdInvoiceModel : PageModel
    {
        public void OnGet()
        {
            TxtUrl = Helper.WWWRoot(Request) + Url.Content("~/files/invoice.html");
        }

        [BindProperty]
        public string TxtUrl { get; set; }

        [BindProperty]
        public string DdlRenderingEngine { get; set; } = "Chromium";

        public List<SelectListItem> RenderingEngines { get; } = Helper.RenderingEngines();

        public IActionResult OnPost()
        {
            RenderingEngine engine = (RenderingEngine)Enum.Parse(typeof(RenderingEngine), DdlRenderingEngine);

            // instantiate a html to pdf converter object
            PdfConverter converter = new PdfConverter() { AllowLocalAccess = false };
            // converter.LicenseKey = "your license key";

            // the rendering engine: Chromium is the recommended engine for modern pages
            converter.RenderingEngine = engine;
            converter.PdfDocumentOptions.LeftMargin = 20;
            converter.PdfDocumentOptions.RightMargin = 20;
            converter.PdfDocumentOptions.TopMargin = 20;
            converter.PdfDocumentOptions.BottomMargin = 20;
            converter.PdfDocumentInfo.Title = "Invoice F-2026-1017";
            converter.PdfDocumentInfo.AuthorName = "Alpine Data Systems GmbH";

            // ZUGFeRD / Factur-X invoices are PDF/A-3; Chromium and Blink can also make them tagged (PDF/A-3a)
            if (engine == RenderingEngine.WebKit || engine == RenderingEngine.WebKit2)
            {
                converter.PdfStandardSubset = PdfStandardSubset.Pdf_A_3b;
            }
            else
            {
                converter.PdfStandardSubset = PdfStandardSubset.Pdf_A_3a;
                converter.Language = "en-US";
            }

            // convert the invoice page to a Document object
            Document doc = converter.GetPdfDocumentObjectFromUrl(TxtUrl);

            // embed the invoice XML as factur-x.xml and write the Factur-X metadata; the profile must match the XML
            // (EN 16931, also called COMFORT) and Alternative says the XML is another form of the visible invoice
            string xmlFile = Path.Combine(Startup.WebRootPath, "files", "factur-x.xml");
            doc.AddZugferdInvoice(xmlFile, ZugferdProfile.En16931, AttachmentRelationship.Alternative);

            // save pdf document
            byte[] pdf = doc.Save();

            // close pdf document
            doc.Close();

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "Invoice-F-2026-1017.pdf";
            return fileResult;
        }
    }
}