locked
Create PDF Page from HTML in Asp.Net Core 2.2 RRS feed

  • Question

  • User-197255323 posted

    I'm searching a library to use creating pdf from html page. 

    When I've developed in WinForm or WPF, iTextSharp is well for my all aims. But, I cant find simple, free or more using pdf creating library due to I'm new in Asp.Net Core.

    Is there any idea for me?

    Thanks a lot from now.

    Tuesday, May 28, 2019 1:12 AM

Answers

  • User585649674 posted

    iTextsharp will work in dotnet core as well. I have used it. There is javascript library to convert html to pdf directly called jsPdf

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 28, 2019 6:46 AM
  • User-854763662 posted

    Hi drakislow ,

    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 Windows.

    • Install the Syncfusion.HtmlToPdfConverter.QtWebKit.Net.Core NuGet package as reference to your .NET Core application from NuGet.org
    • Copy the QtBinariesDotNetCore folder( all files including inner folders and files) from the installed HtmltoPdfConverter package and paste it into the folder, which contains the projectName.csproj file.
    • Add an Export To PDF button in index.cshtml.
    <div class="btn">
        @using (Html.BeginForm("ExportToPDF", "Home", FormMethod.Post))
        {
            <input type="submit" value="Export To PDF"/>
        }
            
    </div>
    • Include the following namespaces and code snippet for converting HTML to PDF in ASP.NET Core Windows.
    using Syncfusion.HtmlConverter;
    using Syncfusion.Pdf;
    using System.IO;
    using Microsoft.AspNetCore.Hosting;
    
    public class HomeController : Controller
        {
            //To get content root path of the project
            private readonly IHostingEnvironment _honstingEnvironment;
            public HomeController(IHostingEnvironment hostingEnvironment)
            {
                _honstingEnvironment = hostingEnvironment;
            }
            public IActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public IActionResult ExportToPDF(string submit)
            {
    //Initialize HTML to PDF converter HtmlToPdfConverter converter = new HtmlToPdfConverter(); WebKitConverterSettings settings = new WebKitConverterSettings();
    //Set WebKit path settings.WebKitPath = Path.Combine(_honstingEnvironment.ContentRootPath, "QtBinariesDotNetCore"); //Assign WebKit settings to HTML converter converter.ConverterSettings = settings; //Convert URL to PDF PdfDocument document = converter.Convert("https://www.google.com"); MemoryStream ms = new MemoryStream(); document.Save(ms); document.Close(true); ms.Position = 0; FileStreamResult fileStreamResult = new FileStreamResult(ms, "application/pdf"); fileStreamResult.FileDownloadName = "Sample.pdf"; return fileStreamResult; } }

    You could refer to here for more details , and the following link covers others method that you could choose as per your need.

    https://www.reddit.com/r/dotnet/comments/bh4uwx/net_core_html_to_pdf/

    Best Regards ,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 28, 2019 8:19 AM

All replies

  • User585649674 posted

    iTextsharp will work in dotnet core as well. I have used it. There is javascript library to convert html to pdf directly called jsPdf

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 28, 2019 6:46 AM
  • User-854763662 posted

    Hi drakislow ,

    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 Windows.

    • Install the Syncfusion.HtmlToPdfConverter.QtWebKit.Net.Core NuGet package as reference to your .NET Core application from NuGet.org
    • Copy the QtBinariesDotNetCore folder( all files including inner folders and files) from the installed HtmltoPdfConverter package and paste it into the folder, which contains the projectName.csproj file.
    • Add an Export To PDF button in index.cshtml.
    <div class="btn">
        @using (Html.BeginForm("ExportToPDF", "Home", FormMethod.Post))
        {
            <input type="submit" value="Export To PDF"/>
        }
            
    </div>
    • Include the following namespaces and code snippet for converting HTML to PDF in ASP.NET Core Windows.
    using Syncfusion.HtmlConverter;
    using Syncfusion.Pdf;
    using System.IO;
    using Microsoft.AspNetCore.Hosting;
    
    public class HomeController : Controller
        {
            //To get content root path of the project
            private readonly IHostingEnvironment _honstingEnvironment;
            public HomeController(IHostingEnvironment hostingEnvironment)
            {
                _honstingEnvironment = hostingEnvironment;
            }
            public IActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public IActionResult ExportToPDF(string submit)
            {
    //Initialize HTML to PDF converter HtmlToPdfConverter converter = new HtmlToPdfConverter(); WebKitConverterSettings settings = new WebKitConverterSettings();
    //Set WebKit path settings.WebKitPath = Path.Combine(_honstingEnvironment.ContentRootPath, "QtBinariesDotNetCore"); //Assign WebKit settings to HTML converter converter.ConverterSettings = settings; //Convert URL to PDF PdfDocument document = converter.Convert("https://www.google.com"); MemoryStream ms = new MemoryStream(); document.Save(ms); document.Close(true); ms.Position = 0; FileStreamResult fileStreamResult = new FileStreamResult(ms, "application/pdf"); fileStreamResult.FileDownloadName = "Sample.pdf"; return fileStreamResult; } }

    You could refer to here for more details , and the following link covers others method that you could choose as per your need.

    https://www.reddit.com/r/dotnet/comments/bh4uwx/net_core_html_to_pdf/

    Best Regards ,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 28, 2019 8:19 AM
  • User-197255323 posted

    Hi drakislow ,

    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 Windows.

    • Install the Syncfusion.HtmlToPdfConverter.QtWebKit.Net.Core NuGet package as reference to your .NET Core application from NuGet.org
    • Copy the QtBinariesDotNetCore folder( all files including inner folders and files) from the installed HtmltoPdfConverter package and paste it into the folder, which contains the projectName.csproj file.
    • Add an Export To PDF button in index.cshtml.
    <div class="btn">
        @using (Html.BeginForm("ExportToPDF", "Home", FormMethod.Post))
        {
            <input type="submit" value="Export To PDF"/>
        }
            
    </div>
    • Include the following namespaces and code snippet for converting HTML to PDF in ASP.NET Core Windows.
    using Syncfusion.HtmlConverter;
    using Syncfusion.Pdf;
    using System.IO;
    using Microsoft.AspNetCore.Hosting;
    
    public class HomeController : Controller
        {
            //To get content root path of the project
            private readonly IHostingEnvironment _honstingEnvironment;
            public HomeController(IHostingEnvironment hostingEnvironment)
            {
                _honstingEnvironment = hostingEnvironment;
            }
            public IActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public IActionResult ExportToPDF(string submit)
            {
    //Initialize HTML to PDF converter HtmlToPdfConverter converter = new HtmlToPdfConverter(); WebKitConverterSettings settings = new WebKitConverterSettings();
    //Set WebKit path settings.WebKitPath = Path.Combine(_honstingEnvironment.ContentRootPath, "QtBinariesDotNetCore"); //Assign WebKit settings to HTML converter converter.ConverterSettings = settings; //Convert URL to PDF PdfDocument document = converter.Convert("https://www.google.com"); MemoryStream ms = new MemoryStream(); document.Save(ms); document.Close(true); ms.Position = 0; FileStreamResult fileStreamResult = new FileStreamResult(ms, "application/pdf"); fileStreamResult.FileDownloadName = "Sample.pdf"; return fileStreamResult; } }

    You could refer to here for more details , and the following link covers others method that you could choose as per your need.

    https://www.reddit.com/r/dotnet/comments/bh4uwx/net_core_html_to_pdf/

    Best Regards ,

    Sherry



    Syncfusion is really well but as I wrote in the question, I am looking for a free library because I will use it for academic purposes. Thank you for your detailed description.

    Friday, May 31, 2019 2:16 PM
  • User-197255323 posted

    iTextsharp will work in dotnet core as well. I have used it. There is javascript library to convert html to pdf directly called jsPdf



    I'll look for jsPdf. Thank you Sir.

    Friday, May 31, 2019 2:16 PM