Convert HTML to PDF file in Mac using C#

21 Feb 20256 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

Prerequisites:

  • Install .NET SDK: Ensure that you have the .NET SDK installed on your system. You can download it from the .NET Downloads page.
  • Install Visual Studio: Download and install Visual Studio Code from the official website.

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

Step 2: Select the Target Framework (e.g., .NET 8.0, .NET 9.0) 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

    Prerequisites:

    • Install .NET SDK: Ensure that you have the .NET SDK installed on your system. You can download it from the .NET Downloads page.
    • Install Visual Studio Code: Download and install Visual Studio Code from the official website.
    • Install C# Extension for VS Code: Open Visual Studio Code, go to the Extensions view (Ctrl+Shift+X), and search for ‘C#’. Install the official C# extension provided by Microsoft.

    Step 1: Open the terminal (Ctrl+` ) and run the following command to create a new ASP.NET Core Web Application project.

    dotnet new mvc -n CreatePdfMacOSApp
    

    Step 2: Replace **CreatePdfMacOSApp with your desired project name.

    Step 3: Navigate to the project directory using the following command

    cd CreatePdfMacOSApp
    

    Step 4: Use the following command in the terminal to add the Syncfusion.HtmlToPdfConverter.Net.Mac package to your project.

    dotnet add package Syncfusion.HtmlToPdfConverter.Net.Mac
    

    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: Build the project.

    Run the following command in terminal to build the project.

    dotnet build
    

    Step 9: Run the project.

    Run the following command in terminal to build the project.

    dotnet run
    

    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.