Convert HTML to PDF file in Mac using C#

3 Jul 20243 minutes to read

The Syncfusion HTML to PDF converter is a .NET library for converting webpages, SVG, MHTML, and HTML to PDF using C#. It is reliable and accurate. Using this library, you can convert HTML to PDF document in Mac.

Steps to convert HTML to PDF in ASP.NET Core MVC

Step 1: Create a new C# ASP.NET Core Web Application project.
Create ASP.NET Core Web application

Step 2: Select the Target Framework of your project.
Select target framework

Step 3: Configure your application and click Create.
Configure the application

Step 4: Install the Syncfusion.HtmlToPdfConverter.Net.Mac NuGet package as reference to your .NET Standard applications from NuGet.org.
NuGet package installation

NOTE

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion license key in your application to use our components.

Step 5: A default controller with name HomeController.cs gets added on creation of ASP.NET MVC project. Include the following namespaces in that HomeController.cs file.

  • C#
  • using Syncfusion.Pdf;
    using Syncfusion.HtmlConverter;
    using System.IO;
    using Microsoft.AspNetCore.Hosting;

    Step 6: A default action method named Index will be present in HomeController.cs. Right click on Index method and select Go To View where you will be directed to its associated view page Index.cshtml. Add a new button in the Index.cshtml as shown below.

  • C#
  • @{Html.BeginForm("ExportToPDF", "Home", FormMethod.Post);
        {
            <div>
                <input type="submit" value="Convert PDF" style="width:150px;height:27px" />
            </div>
         }
            Html.EndForm();
     }

    Step 7: Add a new action method ExportToPDF in HomeController.cs and include the below code example to convert HTML to PDF file using Convert method in HtmlToPdfConverter class.

  • C#
  • public IActionResult ExportToPDF()
    {
        //Initialize HTML to PDF converter.
        HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
        //Convert URL to PDF.
        PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
        MemoryStream stream = new MemoryStream();
        document.Save(stream);
        //Download the PDF document in the browser
        return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "HTML-to-PDF.pdf");
    }

    Step 8: Right click the project and select Build.
    Build project

    NOTE

    Once the build succeeded, unzip the chromium.app file in bin folder (bin-> Debug-> net6.0-> runtimes-> osx-> native-> Chromium.app)`

    Step 9: Run the application.
    Run application

    By executing the program, you will get the PDF document as follows.
    HTML to PDF output document

    A complete working sample can be downloaded from Github.

    Click here to explore the rich set of Syncfusion HTML to PDF converter library features.

    An online sample link to convert HTML to PDF document in ASP.NET Core.