PDF Creator ASP.NET Core samples · .NET 10 and .NET 11

ZUGFeRD / Factur-X Invoice

Creates a ZUGFeRD / Factur-X electronic invoice: the invoice HTML is rendered into a PDF/A-3 document and the matching invoice XML (EN 16931 profile) is embedded as factur-x.xml.

C# code of this sample

Pages/ZugferdInvoiceDemo.cshtml.cs
using System;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using ExpertPdf.PdfCreator;

namespace ExpertPdf.PdfCreator.Samples.Pages
{
    public class ZugferdInvoiceDemoModel : PageModel
    {
        [BindProperty, DisplayFormat(ConvertEmptyStringToNull = false)]
        public string InvoiceHtml { get => _InvoiceHtml; set => _InvoiceHtml = Helper.HostedSampleFile(value); }
        private string _InvoiceHtml = Path.Combine(Helper.FilesFolder, "invoice.html");

        [BindProperty, DisplayFormat(ConvertEmptyStringToNull = false)]
        public string InvoiceXml { get => _InvoiceXml; set => _InvoiceXml = Helper.HostedSampleFile(value); }
        private string _InvoiceXml = Path.Combine(Helper.FilesFolder, "factur-x.xml");

        [BindProperty]
        public int Engine { get; set; } = 0;

        [BindProperty]
        public int Standard { get; set; } = 0;

        public static readonly string[] StandardNames = { "PDF/A-3b", "PDF/A-3u", "PDF/A-3a (tagged, Chromium or Blink)" };

        public string Message { get; set; }

        public void OnGet()
        {
        }

        public IActionResult OnPost()
        {
            try
            {
                return File(CreatePdf(), "application/pdf", "Invoice-INV-2026-0042.pdf");
            }
            catch (Exception ex)
            {
                Message = "The PDF document was not created. " + ex.Message;
                return Page();
            }
        }

        private byte[] CreatePdf()
        {
            // LicensingManager.LicenseKey = "your license key";

            // a ZUGFeRD / Factur-X invoice is a PDF/A-3 document
            PdfStandardSubset standard = Standard == 1 ? PdfStandardSubset.Pdf_A_3u
                : Standard == 2 ? PdfStandardSubset.Pdf_A_3a : PdfStandardSubset.Pdf_A_3b;

            Document document = new Document(standard);
            document.DocumentInformation.Title = "Invoice INV-2026-0042";
            document.DocumentInformation.Author = "Northwind Web Services GmbH";

            // PDF/A-3a is tagged (accessible): the HTML is tagged by the Chromium or Blink engine, not by WebKit
            if (standard == PdfStandardSubset.Pdf_A_3a)
            {
                document.Tagged = true;
                document.Language = "en-US";
            }

            // the visible invoice, rendered from HTML
            document.Margins = new Margins(30);
            PdfPage page = document.Pages.AddNewPage(PageSize.A4, document.Margins, PageOrientation.Portrait);
            HtmlToPdfElement invoice = new HtmlToPdfElement(0, 0, -1, -1, new Uri(InvoiceHtml.Trim()).AbsoluteUri);
            invoice.RenderingEngine = SelectedEngine();
            page.AddElement(invoice);

            // the machine-readable invoice: the XML is embedded as factur-x.xml and the Factur-X metadata is written.
            // The XML must describe the same invoice as the visible document, with the data of the profile given here.
            document.AddZugferdInvoice(InvoiceXml.Trim(), ZugferdProfile.En16931, AttachmentRelationship.Alternative);

            byte[] pdf = document.Save();
            document.Close();
            return pdf;
        }

        // The HTML rendering engines, each deployed next to the application by its NuGet package: WebKit (built in,
        // ExpertPdf.PdfCreator.NetCore), Blink (ExpertPdf.PdfCreator.NetCore.Blink) and Chromium
        // (ExpertPdf.PdfCreator.NetCore.Chromium.Windows), the one recommended for modern pages.
        public static readonly string[] EngineNames = { "Chromium", "Blink", "WebKit" };

        private RenderingEngine SelectedEngine()
        {
            switch (Engine)
            {
                case 1:
                    return RenderingEngine.Blink;
                case 2:
                    return RenderingEngine.WebKit;
                default:
                    return RenderingEngine.Chromium;
            }
        }
    }
}