Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am developing an invoice manager where i would like to save the billing details in pdf. I am using iTextsharp but the table format and the css dont get included.


If I get the code to convert the asp.net web page div containing datacontrols into html format. It will also be helpful.
Please help me with proper solution. Thanks in advance.

What I have tried:

I have already tried
iTextsharp , Select.htmlToPdf
Posted

1 solution

You can iTextSharp HTMLWorker class to convert HTML to PDF:
C#
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
var example_html = @"<p>This is some sample  text<span style="">!!!</span></p>";
StringReader sr = new StringReader(example_html);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();

Response.Write(pdfDoc);
Response.End();

Everything will work fine but HTMLWorker is obsolete. So instead you can use XMLWorkerHelper to get more advantage:

Firstly install class in Nuget console: Install-Package itextsharp.xmlworker
Then add namespace: using iTextSharp.tool.xml;
C#
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
var example_html = @"<p>This is some sample  text<span style="">!!!</span></p>";
StringReader sr = new StringReader(example_html);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
var writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();

Response.Write(pdfDoc);
Response.End();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900