You must Sign In to post a response.
  • Category: ASP.NET

    Convert Html to pdf in C#

    I want to convert my html to pdf using C#. I tried itextsharp dll but it not properly working as its not support styles in html. i need a free dll to achieve my purpose.

    Thanks
  • #765668
    You can get Community Edition (FREE) tool to Html To Pdf.
    The free html to pdf converter offers most of the features the professional sdk offers, the most notable limitation is that it can only generate pdf documents up to 5 pages long.

    Please try the following link you can get more details.
    https://code.msdn.microsoft.com/Convert-from-HTML-to-PDF-09ce2a1d

    By Nathan
    Direction is important than speed

  • #765679
    Here is a sample for converting Html to Pdf in C# using free pdf library, check:

    https://code.msdn.microsoft.com/Convert-HTML-to-PDF-with-C-b16fc75f

  • #765686
    Hi Anil,

    HtmlRenderer.PdfSharp is a 100% fully c# managed code, easy to use, thread safe and most importantly FREE (New BSD License) solution.

    Usage

    Download HtmlRenderer.PdfSharp nuget package.
    Use Example Method.
    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;
    }
    A very Good Alternate Is a Free Version of iTextSharp

    Until version 4.1.6 iTextSharp was licensed under the LGPL licence and versions until 4.16 (or there may be also forks) are available as packages and can be freely used. Of course someone can use the continued 5+ paid version.

    I tried to integrate wkhtmltopdf solutions on my project and had a bunch of hurdles.

    I personally would avoid using wkhtmltopdf based solutions on Hosted Enterprise applications for the following reasons.

    First of all wkhtmltopdf is c++ implemented not c#, and you will experience various problems embedding it within your c# code, especially while switching between 32bit and 64bit builds of your project. Had to try several workarounds including conditional project building etc etc just to avoid "invalid format exceptions" on different machines.
    If you manage your own virtual machine its ok. But if your project is running within a constrained environment like (Azure (Actually is impossible withing azure as mentioned by the TuesPenchin author) , Elastic Beanstalk etc) its a nightmare to configure that environment only for wkhtmltopdf to work.
    wkhtmltopdf is creating files within your server so you have to manage user permissions and grant "write" access to where wkhtmltopdf is running.
    Wkhtmltopdf is running as a standalone application, so its not managed by your IIS application pool. So you have to Either host it as a service on another machine or you will experience huge processing spikes and memory consumption withing your production server.
    It uses temp files to generate the pdf, and in cases Like AWS EC2 which has really slow disk i/o it is a big performance problem.
    The most hated "Unable to load DLL 'wkhtmltox.dll'" error reported by many users.

  • #765710
    Hello,

    These articles discuss converting to PDF file using libraries other than itextsharp:
    http://www.codeproject.com/Articles/7089/Generate-PDF-documents-from-a-HTML-page-using-ASP

    http://www.codeproject.com/Articles/852084/Converting-and-Viewing-Documents-with-LEADTOOLS

  • #765711
    Hi,

    Refer below link, this might be helpful to you.

    http://www.aspose.com/docs/display/pdfnet/How+to+convert+HTML+to+PDF+using+InLineHTML+approach

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #767847
    The number 1 option for me was http://www.hiqpdf.com/free-html-to-pdf-converter.aspx . You can find a good demo project at http://hiqpdf.codeplex.com/ . This is the code I used:

    protected void buttonConvertToPdf_Click(object sender, EventArgs e)
    {
    // create the HTML to PDF converter
    HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

    // set browser width
    htmlToPdfConverter.BrowserWidth = int.Parse(textBoxBrowserWidth.Text);

    // set browser height if specified, otherwise use the default
    if (textBoxBrowserHeight.Text.Length > 0)
    htmlToPdfConverter.BrowserHeight = int.Parse(textBoxBrowserHeight.Text);

    // set HTML Load timeout
    htmlToPdfConverter.HtmlLoadedTimeout = int.Parse(textBoxLoadHtmlTimeout.Text);

    // set PDF page size and orientation
    htmlToPdfConverter.Document.PageSize = GetSelectedPageSize();
    htmlToPdfConverter.Document.PageOrientation = GetSelectedPageOrientation();

    // set PDF page margins
    htmlToPdfConverter.Document.Margins = new PdfMargins(0);

    // set a wait time before starting the conversion
    htmlToPdfConverter.WaitBeforeConvert = int.Parse(textBoxWaitTime.Text);

    // convert HTML to PDF
    byte[] pdfBuffer = null;

    if (radioButtonConvertUrl.Checked)
    {
    // convert URL to a PDF memory buffer
    string url = textBoxUrl.Text;

    pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);
    }
    else
    {
    // convert HTML code
    string htmlCode = textBoxHtmlCode.Text;
    string baseUrl = textBoxBaseUrl.Text;

    // convert HTML code to a PDF memory buffer
    pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlCode, baseUrl);
    }

    // inform the browser about the binary data format
    HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");

    // let the browser know how to open the PDF document, attachment or inline, and the file name
    HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("{0}; filename=HtmlToPdf.pdf; size={1}",
    checkBoxOpenInline.Checked ? "inline" : "attachment", pdfBuffer.Length.ToString()));

    // write the PDF buffer to HTTP response
    HttpContext.Current.Response.BinaryWrite(pdfBuffer);

    // call End() method of HTTP response to stop ASP.NET page processing
    HttpContext.Current.Response.End();
    }


Sign In to post your comments