Convert EML or MSG to a PDF Document in ASP.NET Core.
The Syncfusion HTML to PDF converter for .NET Core PDF Library is used to convert webpages, SVG, MHTML, and HTML to PDF using C#. It is reliable and accurate. The result preserves all graphics, images, text, fonts, and the layout of the original HTML document or webpage.
Using this library, convert EML or MSG to a PDF document in ASP.NET Core. The EML or MSG files can be converted to HTML using a third-party reader, and Syncfusion HTML converter converts HTML to PDF files.
Steps to convert EML/MSG to PDF document programmatically
-
Create a new C# ASP.NET Core Web Application project.
-
Install Syncfusion.HtmlToPdfConverter.Net.Windows and MsgReader NuGet packages as references to your .NET Standard applications from NuGet.org.
-
Include the following namespace in the HomeController.cs file.
C#
using MsgReader;
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
- Use the following code example to convert EML or MSG to a PDF document.
C#
public ActionResult ExportToPDF()
{
//Indicates whether the current application is running in Windows or Linux.
bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
bool isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
//string fullPath = System.IO.Path.GetFullPath(@"Data/MsgReaderContent.msg");
string fullPath = System.IO.Path.GetFullPath(@"Data/Input.eml");
byte[] finalBytes = null;
string filename = "";
try
{
//Convert EML or MSG to HTML using a third-party reader.
var msgReader = new Reader();
string tempFolder = GetTemporaryFolder();
//File contains the HTML file converted from MSG.
var files = msgReader.ExtractToFolder(fullPath, tempFolder);
var error = msgReader.GetErrorMessage();
if (!string.IsNullOrEmpty(error))
throw new Exception(error);
//Initialize HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
if (!string.IsNullOrEmpty(files[0]))
{
if (isLinux)
{
filename = "LinuxOutput.pdf";
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
//Initialize HTML to PDF converter.
htmlConverter = new HtmlToPdfConverter();
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Set command line arguments to run without sandbox.
blinkConverterSettings.CommandLineArguments.Add("--no-sandbox");
blinkConverterSettings.CommandLineArguments.Add("--disable-setuid-sandbox");
htmlConverter.ConverterSettings = blinkConverterSettings;
}
else if (isWindows)
{
filename = "WindowsOutput.pdf";
//Initialize HTML to PDF converter.
htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink);
BlinkConverterSettings settings = new BlinkConverterSettings();
//Assign Blink converter settings to HTML converter.
htmlConverter.ConverterSettings = settings;
}
}
//Create a new PDF document.
PdfDocument pdFDocument = new PdfDocument();
//Create a memory stream to save a PDF document.
MemoryStream outputStream = new MemoryStream();
//Convert HTML file to a PDF document.
pdFDocument = htmlConverter.Convert(System.IO.File.ReadAllText(files[0]), tempFolder);
//Save a PDF document.
pdFDocument.Save(outputStream);
finalBytes = outputStream.ToArray();
//Close the document.
pdFDocument.Close(true);
//Close the stream.
outputStream.Close();
}
catch(Exception ex)
{
throw ex;
}
return File(finalBytes, System.Net.Mime.MediaTypeNames.Application.Pdf, filename);
}
The conversion of EML or MSG to PDF also functions on the Linux platform. To enable this on Linux, it’s necessary to incorporate the corresponding NuGet packages and their dependencies.
https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/linux
Download the complete working sample from Convert-MSG-or-EML-to-PDF-document.zip.
By executing the program, you will get a PDF document as follows:
Take a moment to peruse the documentation, where you can find converting HTML pages to a PDF document along with respective customization options and features.
Refer to here to explore the rich set of Syncfusion Essential PDF features.
An online sample link for converting HTML to PDF.
Note: Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering the Syncfusion license key in your application to use the components without a trail message.