C# code of this sample
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;
}
}
}