Convert HTML to PDF in .NET

后端 未结 30 1808
不知归路
不知归路 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:50

    I highly recommend NReco, seriously. It has the free and paid version, and really worth it. It uses wkhtmtopdf in background, but you just need one assembly. Fantastic.

    Example of use:

    Install via NuGet.

    var htmlContent = String.Format("<body>Hello world: {0}</body>", DateTime.Now);
    var pdfBytes = (new NReco.PdfGenerator.HtmlToPdfConverter()).GeneratePdf(htmlContent);
    

    Disclaimer: I'm not the developer, just a fan of the project :)

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

    I'm the author of the Rotativa package. It allows to create PDF files directly from razor views:

    https://www.nuget.org/packages/Rotativa/

    Trivial to use and you have full control on the layout since you can use razor views with data from your Model and ViewBag container.

    I developed a SaaS version on Azure. It makes it even easier to use it from WebApi or any .Net app, service, Azure website, Azure webjob, whatever runs .Net.

    http://www.rotativahq.com/

    Free accounts available.

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

    It depends on any other requirements you have.

    A really simple but not easily deployable solution is to use a WebBrowser control to load the Html and then using the Print method printing to a locally installed PDF printer. There are several free PDF printers available and the WebBrowser control is a part of the .Net framework.

    EDIT: If you Html is XHtml you can use PDFizer to do the job.

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

    Below is an example of converting html + css to PDF using iTextSharp (iTextSharp + itextsharp.xmlworker)

    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using iTextSharp.tool.xml;
    
    
    byte[] pdf; // result will be here
    
    var cssText = File.ReadAllText(MapPath("~/css/test.css"));
    var html = File.ReadAllText(MapPath("~/css/test.html"));
    
    using (var memoryStream = new MemoryStream())
    {
            var document = new Document(PageSize.A4, 50, 50, 60, 60);
            var writer = PdfWriter.GetInstance(document, memoryStream);
            document.Open();
    
            using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssText)))
            {
                using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
                {
                    XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, htmlMemoryStream, cssMemoryStream);
                }
            }
    
            document.Close();
    
            pdf = memoryStream.ToArray();
    }
    
    0 讨论(0)
  • 2020-11-22 00:56

    There's also a new web-based document generation app - DocRaptor.com. Seems easy to use, and there's a free option.

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

    2018's update, and Let's use standard HTML+CSS=PDF equation!

    There are good news for HTML-to-PDF demands. As this answer showed, the W3C standard css-break-3 will solve the problem... It is a Candidate Recommendation with plan to turn into definitive Recommendation in 2017 or 2018, after tests.

    As not-so-standard there are solutions, with plugins for C#, as showed by print-css.rocks.

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