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

ExpertPdf Html To Pdf Converter for .NET - PDF/A Documents - C# / ASP.NET Sample

This sample converts a web page to a PDF/A document. PDF/A is the ISO standard for long-term archiving: the fonts and the color profile are embedded and nothing depends on the outside world, so the document looks the same in many years.

PDF/A-1b, PDF/A-2b and PDF/A-3b guarantee the appearance; PDF/A-3u also guarantees that the text can be extracted as Unicode; PDF/A-3a is also tagged (accessible) and needs the Chromium or the Blink engine. PDF/A-3 documents can carry associated files: this sample can embed the converted HTML page in the PDF.

Url:


PDF standard:


Embed the HTML page as an associated file (PDF/A-3)

Rendering engine:


C# code of this sample

Pages/PdfStandards.cshtml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
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 PdfStandardsModel : PageModel
    {
        public void OnGet()
        {
            TxtUrl = Helper.WWWRoot(Request) + Url.Content("~/files/sample.html");
        }

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

        [BindProperty]
        public string DdlStandard { get; set; } = "Pdf_A_3b";

        public List<SelectListItem> Standards { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Pdf_A_1b", Text = "PDF/A-1b" },
            new SelectListItem { Value = "Pdf_A_2b", Text = "PDF/A-2b" },
            new SelectListItem { Value = "Pdf_A_3b", Text = "PDF/A-3b" },
            new SelectListItem { Value = "Pdf_A_3u", Text = "PDF/A-3u" },
            new SelectListItem { Value = "Pdf_A_3a", Text = "PDF/A-3a (tagged, Chromium or Blink)" },
        };

        [BindProperty]
        public bool CbAttachSource { get; set; } = true;

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

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

        public IActionResult OnPost()
        {
            PdfStandardSubset standard = (PdfStandardSubset)Enum.Parse(typeof(PdfStandardSubset), DdlStandard);

            // 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 = (RenderingEngine)Enum.Parse(typeof(RenderingEngine), DdlRenderingEngine);

            // the PDF/A level; PDF/A-3a is also tagged, so it needs a language for the text
            converter.PdfStandardSubset = standard;
            if (standard == PdfStandardSubset.Pdf_A_3a)
                converter.Language = "en-US";

            // PDF/A requires a title
            converter.PdfDocumentInfo.Title = "ExpertPdf HTML to PDF Converter - PDF/A Sample";

            // get the PDF as a Document object to add an associated file before saving it
            Document doc = converter.GetPdfDocumentObjectFromUrl(TxtUrl);

            // PDF/A-3 documents can embed any file, linked to the document by a relationship; here the HTML page the
            // PDF was created from (Source). The stream is read when the document is saved: keep it open until then.
            bool pdfA3 = standard == PdfStandardSubset.Pdf_A_3b || standard == PdfStandardSubset.Pdf_A_3u ||
                standard == PdfStandardSubset.Pdf_A_3a;
            MemoryStream htmlStream = null;
            if (CbAttachSource && pdfA3)
            {
                using (HttpClient http = new HttpClient())
                {
                    htmlStream = new MemoryStream(http.GetByteArrayAsync(TxtUrl).Result);
                }
                doc.AddAssociatedFile(htmlStream, "page.html", AttachmentRelationship.Source, "text/html",
                    "The HTML page this document was created from");
            }

            byte[] pdf;
            try
            {
                // save pdf document
                pdf = doc.Save();
            }
            finally
            {
                // close pdf document
                doc.Close();
                if (htmlStream != null)
                    htmlStream.Dispose();
            }

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "PdfA.pdf";
            return fileResult;
        }
    }
}