none
HTML to PDF method or library RRS feed

  • Question

  • As I mentioned in the title, I am trying to create a HTML to PDF conversion method for .NET to serve some of the current C # applications I am developing. I have made use of iTextSharp for this but it does not perform well when it encounters tables and the layout just gets messy.

    Is there a better way? Thank you.
    Wednesday, August 7, 2019 11:29 PM

Answers

  • Try wkhtmtopdfFor .NET, you may use this Office Component

    Or you can use HtmlRenderer.PdfSharp nuget package.

    public static Byte[] PdfSharpConvert(String html)
    {
        Byte[] res = null;
        using (MemoryStream ms = new MemoryStream())
        {
            var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4);
            pdf.Save(ms);
            res = ms.ToArray();
        }
        return res;
    }

    Thursday, August 8, 2019 12:49 AM
  • Hi Donald,

    Thank you for posting here.

    You could install-Package HtmlRenderer.PdfSharp to use the following code to convert html to pdf.

    using PdfSharp.Pdf;
    using System.IO;
    using TheArtOfDev.HtmlRenderer.PdfSharp;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string html = File.ReadAllText("D:\\test.html");
                PdfDocument pdf = PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.Letter);
                pdf.Save("document.pdf");
            }
        }
    }
    

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 8, 2019 2:46 AM
    Moderator

All replies

  • Try wkhtmtopdfFor .NET, you may use this Office Component

    Or you can use HtmlRenderer.PdfSharp nuget package.

    public static Byte[] PdfSharpConvert(String html)
    {
        Byte[] res = null;
        using (MemoryStream ms = new MemoryStream())
        {
            var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4);
            pdf.Save(ms);
            res = ms.ToArray();
        }
        return res;
    }

    Thursday, August 8, 2019 12:49 AM
  • Hi Donald,

    Thank you for posting here.

    You could install-Package HtmlRenderer.PdfSharp to use the following code to convert html to pdf.

    using PdfSharp.Pdf;
    using System.IO;
    using TheArtOfDev.HtmlRenderer.PdfSharp;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string html = File.ReadAllText("D:\\test.html");
                PdfDocument pdf = PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.Letter);
                pdf.Save("document.pdf");
            }
        }
    }
    

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 8, 2019 2:46 AM
    Moderator
  • Try Spire.PDF for .NET library. Here is the online documentation for your reference:

    Convert HTML to PDF with New Plugin

    Thursday, August 8, 2019 2:50 AM