Thursday 12 January 2017

C# HTML to PDF Converter, iTextSharp

Hi All, we 've made lot of research to make this super useful function in c# to convert HTML to PDF,
with help of fantastic free library iTextSharp:

https://sourceforge.net/projects/itextsharp/
With only 6 rows it's done!
Warning!: HTML code must be very clear, simple and clean(CSS was not fully supported, is better use attributes to element if exists)
Example:

<!--lwebcode:inside yout html code-->
<div style='font-size:15px;text-align:left;'>Title Of Page<br/></div> ==> Works
<td align='center' style='width:15%' >cell content</td> ==> DOESN'T Works
<td align='center' width='15%' >cell content</td> ==> Works

//lwebcode:inside *.cs file
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using iTextSharp.tool.xml;

//
lwebcode:inside your class lwebcode
private void ConvertHtmlToPdf(string sHTML)
    {
        Document document = new Document();
        PdfWriter.GetInstance(document, new FileStream(Server.MapPath("~/OutPath/TargetName.pdf"), FileMode.Create));
        document.Open();
        iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
        hw.Parse(new StringReader(sHTML));
        document.Close();
    }

9 comments:

  1. Thanks for sharing, I also found a good pdf to text C# sdk, which contains HTML to PDF features, a free trial version and a more powerful official version.

    ReplyDelete
  2. free html to pdf converterHTML to PDF Converter | Convert from html to pdf online, using SelectPdf web to pdf convert

    ReplyDelete
  3. html to pdfHTML to PDF Converter | Convert from html to pdf online, using SelectPdf web to pdf convert

    ReplyDelete
  4. html to pdf converterHTML to PDF Converter for .NET | Select.Pdf offers a powerful html to pdf converter that can be used in any .NET application to convert any web page or raw html string to pdf

    ReplyDelete
  5. pdf libraryCreate high quality PDFs with SelectPdf Html To Pdf Converter from the best PDF library. HTML to PDF API also available

    ReplyDelete
  6. free html to pdf converter

    HTML to PDF Converter | Convert from html to pdf online, using SelectPdf web to pdf convert

    https://selectpdf.com/category/html-to-pdf/

    ReplyDelete
  7. Html to pdf converter

    SelectPdf Free Html To Pdf Converter Samples for C# / ASP.NET. Pdf Library for .NET with full sample code in C# and VB.NET.
    to get more - https://selectpdf.com/html-to-pdf/demo/

    ReplyDelete