Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ITextSharp HTML to PDF?

I'd like to know if ITextSharp has the capability of converting HTML to PDF. Everything I will convert will just be plain text but unfortunately there is very little to no documentation on ITextSharp so I can't determine if that will be a viable solution for me.

If it can't do it, can someone point me to some good, free .net libraries that can take a simple plain text HTML document and convert it to a pdf?

tia.

like image 496
Kyle Avatar asked May 12 '10 21:05

Kyle


People also ask

Is Itextsharp free for commercial use?

This license is a commercial license. You have to pay for it. To answer your question: iText can be used for free in situations where you also distribute your software for free. As soon as you want to use iText in a closed source, proprietary environment, you have to pay for your use of iText.


1 Answers

I came across the same question a few weeks ago and this is the result from what I found. This method does a quick dump of HTML to a PDF. The document will most likely need some format tweaking.

private MemoryStream createPDF(string html) {     MemoryStream msOutput = new MemoryStream();     TextReader reader = new StringReader(html);      // step 1: creation of a document-object     Document document = new Document(PageSize.A4, 30, 30, 30, 30);      // step 2:     // we create a writer that listens to the document     // and directs a XML-stream to a file     PdfWriter writer = PdfWriter.GetInstance(document, msOutput);      // step 3: we create a worker parse the document     HTMLWorker worker = new HTMLWorker(document);      // step 4: we open document and start the worker on the document     document.Open();     worker.StartDocument();      // step 5: parse the html into the document     worker.Parse(reader);      // step 6: close the document and the worker     worker.EndDocument();     worker.Close();     document.Close();      return msOutput; } 
like image 115
Jonathan Avatar answered Oct 08 '22 21:10

Jonathan