I have a requirement to write pdfdocument using html contents in an ASP.NET MVC Project. I am writing the HTML code inside a string variable and then I am giving that string for rendering it as pdf bytes using the below code. The returned bytes I am saving
it as pdf on the server. However I am getting too many formatting issues in the pdf generated.
When I saved the html contents which I passed to the string, formattings are good but somehow when I am trying to create a pdf out of those contents I am getting issues. Any help will be greatly appreciated.
public byte[] Render(string htmlText)
{
byte[] renderedBuffer;
using (var outputMemoryStream = new MemoryStream())
{
using (var pdfDocument = new Document())
{
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);
pdfWriter.CloseStream = false;
//pdfWriter.PageEvent = new PrintHeaderFooter { Title = pageTitle };
pdfDocument.Open();
using (var htmlViewReader = new StringReader(htmlText))
{
using (var htmlWorker = new HTMLWorker(pdfDocument))
{
htmlWorker.Parse(htmlViewReader);
}
}
}
The format of the HTML contents are not getting rendered properly in pdf. I should be able to see the pdf exactly how that html file is looking and thats my problem
You need to provide in line style for the HTML tags before sending the html string to pdf & then try again to create the pdf. Hopefully it will reslove your issue of formating
Don't forget to click "Mark as Answer" on the post that helped you.
Your suggestion seems to be a solution but I am stuck here because there are lot many styles been used from an external css file. So inorder for my pdf to get generated everything should work properly with almost no changes on the htm file as the same html
file is been used for all other requirements as well.
Then your html file will not use any css classs becasue the formatting will not applied, so you need to make that page to use inline style instead of css classes. And it will reslove your problem.
Don't forget to click "Mark as Answer" on the post that helped you.
krishnamohan...
Member
9 Points
31 Posts
formatting issue with iTextSharp
Feb 26, 2013 05:12 AM|LINK
Hello
I have a requirement to write pdfdocument using html contents in an ASP.NET MVC Project. I am writing the HTML code inside a string variable and then I am giving that string for rendering it as pdf bytes using the below code. The returned bytes I am saving it as pdf on the server. However I am getting too many formatting issues in the pdf generated.
When I saved the html contents which I passed to the string, formattings are good but somehow when I am trying to create a pdf out of those contents I am getting issues. Any help will be greatly appreciated.
public byte[] Render(string htmlText)
{
byte[] renderedBuffer;
using (var outputMemoryStream = new MemoryStream())
{
using (var pdfDocument = new Document())
{
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);
pdfWriter.CloseStream = false;
//pdfWriter.PageEvent = new PrintHeaderFooter { Title = pageTitle };
pdfDocument.Open();
using (var htmlViewReader = new StringReader(htmlText))
{
using (var htmlWorker = new HTMLWorker(pdfDocument))
{
htmlWorker.Parse(htmlViewReader);
}
}
}
renderedBuffer = new byte[outputMemoryStream.Position];
outputMemoryStream.Position = 0;
outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length);
}
return renderedBuffer;
}
ignatandrei
All-Star
135174 Points
21682 Posts
Moderator
MVP
Re: formatting issue with iTextSharp
Feb 26, 2013 05:56 AM|LINK
What issues?
krishnamohan...
Member
9 Points
31 Posts
Re: formatting issue with iTextSharp
Feb 26, 2013 06:36 AM|LINK
The format of the HTML contents are not getting rendered properly in pdf. I should be able to see the pdf exactly how that html file is looking and thats my problem
ahmad_iam
Member
622 Points
167 Posts
Re: formatting issue with iTextSharp
Feb 26, 2013 07:10 AM|LINK
You need to provide in line style for the HTML tags before sending the html string to pdf & then try again to create the pdf. Hopefully it will reslove your issue of formating
krishnamohan...
Member
9 Points
31 Posts
Re: formatting issue with iTextSharp
Mar 01, 2013 04:07 AM|LINK
Your suggestion seems to be a solution but I am stuck here because there are lot many styles been used from an external css file. So inorder for my pdf to get generated everything should work properly with almost no changes on the htm file as the same html file is been used for all other requirements as well.
ahmad_iam
Member
622 Points
167 Posts
Re: formatting issue with iTextSharp
Mar 02, 2013 01:47 AM|LINK
Then your html file will not use any css classs becasue the formatting will not applied, so you need to make that page to use inline style instead of css classes. And it will reslove your problem.