How to convert HTML to PDF in ASP.NET Core Mac
Syncfusion HTML to PDF for .NET used to convert webpages, SVG, MHTML, and HTML to PDF. Using this library, you can convert HTML to PDF in ASP.NET Core application using .Net Standard 2.0.
Minimum product version: 16.1.0.24
Minimum .NET core version: 2.0.0
The following assemblies or NuGet package is required for converting HTML to PDF in ASP.NET Core Mac.
Assemblies required:
- Syncfusion.Compression.Portable.dll
- Syncfusion.Pdf.Portable.dll
- Syncfusion.HtmlConverter.Portable.dll
- QtBinaries
You can get all the previously mentioned assemblies, QtBinaries, and working samples by installing the HTML converter installer for Mac available in the following link.
HTML converter Mac (pkg file): https://www.syncfusion.com/downloads/latest-version
NuGet
Install the Syncfusion.HtmlToPdfConverter.QtWebKit.Net.Core NuGet package as reference to your .NET Core application from NuGet.org. The NuGet package contains QtBinaries for all platforms (Windows, Linux and Mac). Copy the QtBinariesMac folder from the NuGet package installed location and paste it into your application folder to use it for conversion.
NuGet Installed location: /.nuget/packages/syncfusion.htmltopdfconverter.qtwebkit.net.core/XX.X.X.XX/lib
Use the following code snippet to convert HTML to PDF in ASP.NET Core Mac.
// [C# Code] //To get content root path of the project private readonly IHostingEnvironment _hostingEnvironment; public HomeController(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; } public IActionResult ExportToPDF() { //Initialize HTML to PDF converter HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); WebKitConverterSettings settings = new WebKitConverterSettings(); //Set WebKit path settings.WebKitPath = Path.Combine(_hostingEnvironment.ContentRootPath, "QtBinariesMac"); //Assign WebKit settings to HTML converter htmlConverter.ConverterSettings = settings; //Convert URL to PDF PdfDocument document = htmlConverter.Convert("https://www.google.com"); MemoryStream stream = new MemoryStream(); document.Save(stream); return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "Output.pdf"); }
A complete working sample can be downloaded from HTMLtoPDF_Mac.zip
Take a moment to peruse the documentation for Converting HTML to PDF, where you will find various options for URL to PDF, HTML string to PDF, and hyperlinks.