Convert HTML to PDF in .NET

后端 未结 30 1872
不知归路
不知归路 2020-11-22 00:28

I want to generate a PDF by passing HTML contents to a function. I have made use of iTextSharp for this but it does not perform well when it encounters tables and the layout

相关标签:
30条回答
  • 2020-11-22 00:30

    Winnovative offer a .Net PDF library that supports HTML input. They offer an unlimited free trial. Depending on how you wish to deploy your project, this might be sufficient.

    0 讨论(0)
  • 2020-11-22 00:30

    ABCpdf.NET (http://www.websupergoo.com/abcpdf-5.htm)

    We use and recommend.

    Very good component, it not only convert a webpage to PDF like an image but really convert text, image, formatting, etc...

    It's not free but it's cheap.

    0 讨论(0)
  • 2020-11-22 00:32

    Last Updated: October 2020

    This is the list of options for HTML to PDF conversion in .NET that I have put together (some free some paid)

    • GemBox.Document

      • https://www.nuget.org/packages/GemBox.Document/
      • Free (up to 20 paragraphs)
      • $680 - https://www.gemboxsoftware.com/document/pricelist
      • https://www.gemboxsoftware.com/document/examples/c-sharp-convert-html-to-pdf/307
    • PDF Metamorphosis .Net

      • https://www.nuget.org/packages/sautinsoft.pdfmetamorphosis/
      • $539 - $1078 - https://www.sautinsoft.com/products/pdf-metamorphosis/order.php
      • https://www.sautinsoft.com/products/pdf-metamorphosis/convert-html-to-pdf-dotnet-csharp.php
    • HtmlRenderer.PdfSharp

      • https://www.nuget.org/packages/HtmlRenderer.PdfSharp/1.5.1-beta1
      • BSD-UNSPECIFIED License
    • PuppeteerSharp

      • https://www.puppeteersharp.com/examples/index.html
      • MIT License
      • https://github.com/kblok/puppeteer-sharp
    • EO.Pdf

      • https://www.nuget.org/packages/EO.Pdf/
      • $799 - https://www.essentialobjects.com/Purchase.aspx?f=3
    • WnvHtmlToPdf_x64

      • https://www.nuget.org/packages/WnvHtmlToPdf_x64/
      • $750 - $1600 - http://www.winnovative-software.com/Buy.aspx
      • demo - http://www.winnovative-software.com/demo/default.aspx
    • IronPdf

      • https://www.nuget.org/packages/IronPdf/
      • $399 - $1599 - https://ironpdf.com/licensing/
      • https://ironpdf.com/examples/using-html-to-create-a-pdf/
    • Spire.PDF

      • https://www.nuget.org/packages/Spire.PDF/
      • Free (up to 10 pages)
      • $599 - $1799 - https://www.e-iceblue.com/Buy/Spire.PDF.html
      • https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Convert-HTML-to-PDF-Customize-HTML-to-PDF-Conversion-by-Yourself.html
    • Aspose.Html

      • https://www.nuget.org/packages/Aspose.Html/
      • $599 - $1797 - https://purchase.aspose.com/pricing/html/net
      • https://docs.aspose.com/html/net/html-to-pdf-conversion/
    • EvoPDF

      • https://www.nuget.org/packages/EvoPDF/
      • $450 - $1200 - http://www.evopdf.com/buy.aspx
    • ExpertPdfHtmlToPdf

      • https://www.nuget.org/packages/ExpertPdfHtmlToPdf/
      • $550 - $1200 - https://www.html-to-pdf.net/Pricing.aspx
    • Zetpdf

      • https://zetpdf.com
      • $299 - $599 - https://zetpdf.com/pricing/
      • Is not a well know or supported library - ZetPDF - Does anyone know the background of this Product?
    • PDFtron

      • https://www.pdftron.com/documentation/samples/cs/HTML2PDFTes
      • $4000/year - https://www.pdftron.com/licensing/
    • WkHtmlToXSharp

      • https://github.com/pruiz/WkHtmlToXSharp
      • Free
      • Concurrent conversion is implemented as processing queue.
    • SelectPDF

      • https://www.nuget.org/packages/Select.HtmlToPdf/
      • Free (up to 5 pages)
      • $499 - $799 - https://selectpdf.com/pricing/
      • https://selectpdf.com/pdf-library-for-net/

    If none of the options above help you you can always search the NuGet packages:
    https://www.nuget.org/packages?q=html+pdf

    0 讨论(0)
  • 2020-11-22 00:33

    I found the following library more effective in converting html to pdf.
    nuget: https://www.nuget.org/packages/Select.HtmlToPdf/

    0 讨论(0)
  • 2020-11-22 00:34

    Quite likely most projects will wrap a C/C++ Engine rather than implementing a C# solution from scratch. Try Project Gotenberg.

    To test it

    docker run --rm -p 3000:3000 thecodingmachine/gotenberg:6
    

    Curl Example

    curl --request POST \
        --url http://localhost:3000/convert/url \
        --header 'Content-Type: multipart/form-data' \
        --form remoteURL=https://brave.com \
        --form marginTop=0 \
        --form marginBottom=0 \
        --form marginLeft=0 \
        --form marginRight=0 \
        -o result.pdf
    

    C# sample.cs

    using System;
    using System.Net.Http;
    using System.Threading.Tasks;
    using System.IO;
    using static System.Console;
    
    namespace HelloWorld
    {
        class Program
        {
            public static async Task Main(string[] args)
            {
                try
                {
                    var client = new HttpClient();            
                    var formContent = new MultipartFormDataContent
                        {
                            {new StringContent("https://duckduckgo.com/"), "remoteURL"},
                            {new StringContent("0"), "marginTop" }
                        };
                    var result = await client.PostAsync(new Uri("http://localhost:3000/convert/url"), formContent);
                    await File.WriteAllBytesAsync("duckduck.com.pdf", await result.Content.ReadAsByteArrayAsync());
                }
                catch (Exception ex)
                {
                    WriteLine(ex);
                }
            }
        }
    }
    

    To compile

    csc sample.cs -langversion:latest -reference:System.Net.Http.dll && mono ./sample.exe
    
    0 讨论(0)
  • 2020-11-22 00:36

    I was also looking for this a while back. I ran into HTMLDOC http://www.easysw.com/htmldoc/ which is a free open source command line app that takes an HTML file as an argument and spits out a PDF from it. It's worked for me pretty well for my side project, but it all depends on what you actually need.

    The company that makes it sells the compiled binaries, but you are free to download and compile from source and use it for free. I managed to compile a pretty recent revision (for version 1.9) and I intend on releasing a binary installer for it in a few days, so if you're interested I can provide a link to it as soon as I post it.

    Edit (2/25/2014): Seems like the docs and site moved to http://www.msweet.org/projects.php?Z1

    0 讨论(0)
提交回复
热议问题