Convert HTML to PDF in .NET

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

    As a representative of HiQPdf Software I believe the best solution is HiQPdf HTML to PDF converter for .NET. It contains the most advanced HTML5, CSS3, SVG and JavaScript rendering engine on market. There is also a free version of the HTML to PDF library which you can use to produce for free up to 3 PDF pages. The minimal C# code to produce a PDF as a byte[] from a HTML page is:

    HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
    
    // set PDF page size, orientation and margins
    htmlToPdfConverter.Document.PageSize = PdfPageSize.A4;
    htmlToPdfConverter.Document.PageOrientation = PdfPageOrientation.Portrait;
    htmlToPdfConverter.Document.Margins = new PdfMargins(0);
    
    // convert HTML to PDF 
    byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);
    

    You can find more detailed examples both for ASP.NET and MVC in HiQPdf HTML to PDF Converter examples repository.

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

    I recently performed a PoC regarding HTML to PDF conversion and wanted to share my results.

    My favorite by far is OpenHtmlToPdf

    Advantages of this tool:

    • Very good HTML compatibility (e.g. it was the only tool in my example that correctly repeated table headers when a table spanned multiple pages)
    • Fluent API
    • Free and OpenSource (Creative Commons Attribution 3.0 license)
    • Available via NuGet

    Other tools tested:

    • ExpertPDF (http://www.html-to-pdf.net/)
    • IronPDF (http://ironpdf.com/)
    • iTextSharp (https://sourceforge.net/projects/itextsharp/)
    • NReco PDF Creator for .NET (http://www.nrecosite.com/pdf_generator_net.aspx)
    • HTML renderer for PDF Sharp (https://www.nuget.org/packages/HtmlRenderer.PdfSharp/)
    • SelectPDF community edition (http://selectpdf.com/community-edition/)
    0 讨论(0)
  • 2020-11-22 00:41

    I used ExpertPDF Html To Pdf Converter. Does a decent job. Unfortunatelly, it's not free.

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

    This is a free library and works very easily : OpenHtmlToPdf

    string timeStampForPdfName = DateTime.Now.ToString("yyMMddHHmmssff");
    
    string serverPath = System.Web.Hosting.HostingEnvironment.MapPath("~/FolderName");
    string pdfSavePath = Path.Combine(@serverPath, "FileName" + timeStampForPdfName + ".FileExtension");
    
    
    //OpenHtmlToPdf Library used for Performing PDF Conversion
    var pdf = Pdf.From(HTML_String).Content();
    
    //FOr writing to file from a ByteArray
     File.WriteAllBytes(pdfSavePath, pdf.ToArray()); // Requires System.Linq
    
    0 讨论(0)
  • 2020-11-22 00:42

    Update: I would now recommend PupeteerSharp over wkhtmltopdf.

    Try wkhtmtopdf. It is the best tool I have found so far.

    For .NET, you may use this small library to easily invoke wkhtmtopdf command line utility.

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

    It seems like so far the best free .NET solution is the TuesPechkin library which is a wrapper around the wkhtmltopdf native library.

    I've now used the single-threaded version to convert a few thousand HTML strings to PDF files and it seems to work great. It's supposed to also work in multi-threaded environments (IIS, for example) but I haven't tested that.

    Also since I wanted to use the latest version of wkhtmltopdf (0.12.5 at the time of writing), I downloaded the DLL from the official website, copied it to my project root, set copy to output to true, and initialized the library like so:

    var dllDir = AppDomain.CurrentDomain.BaseDirectory;
    Converter = new StandardConverter(new PdfToolset(new StaticDeployment(dllDir)));
    

    Above code will look exactly for "wkhtmltox.dll", so don't rename the file. I used the 64-bit version of the DLL.

    Make sure you read the instructions for multi-threaded environments, as you will have to initialize it only once per app lifecycle so you'll need to put it in a singleton or something.

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