Victor Barcellos Victor Barcellos - 5 months ago 44x
HTML Question

PDF Not Rendering Html Css ASP.NET

Hello guys I am generating a Payment Invoice order in PDF from my html content and sending it by e-mail with the following code:

***//Generates PDF Payment Invoice***
StringBuilder sb = new StringBuilder();
sb.Append(@"<meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"">");

StringReader sr = new StringReader(sb.ToString());

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
byte[] bytes;

memoryStream = new MemoryStream();

PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);

iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
ST.LoadTagStyle("body", "encoding", "Identity-H");

bytes = memoryStream.ToArray();
memoryStream = new MemoryStream(bytes);

***//Sends E-mail with PDF PAYMENT INVOICE Attached***
MailAddress de = new MailAddress(enderecoOrigem, HttpUtility.HtmlDecode(nomeOrigem));
MailAddress para = new MailAddress(enderecoDestino, HttpUtility.HtmlDecode(nomeDestino));

MailMessage mensagem = new MailMessage(de, para);
NetworkCredential credential = new NetworkCredential(usuarioConta, senhaConta);
SmtpClient smtp = new SmtpClient();
smtp.Host = servidorSMTP;
smtp.Port = Convert.ToInt32(porta);
MailAddressCollection comCopia;

Attachment att = new Attachment(memoryStream, "Boleto.pdf", MediaTypeNames.Application.Pdf);

mensagem.Subject = "Payment Invoice";
mensagem.Body = String.Format("Your payment invoice is available.");
mensagem.IsBodyHtml = true;

smtp.UseDefaultCredentials = true;
smtp.EnableSsl = false;


The problem is that the PDF attached to the email does not render correctly the HTML so it stills unformmated. Otherwise when i create a blank file and put the entire HTML and open it using Chrome it's pretty well formated.

I need to get this PDF correctly attached to the e-mail.

Could somebody help me?Here You can see the Rendering Problem


Finally I've found the solution to my problem!

It was necessary to use itextSharp.xmlWorker library and do some changes in the code-behind. The reason is HTMLWorker really does not resolve CSS, so I had to use XMLWorker instead and do like following:

    //Geração de PDF
    StringBuilder sb = new StringBuilder();      
    StringReader sr;
    Document pdfDoc;
    PdfWriter writer;
    byte[] bytes;

    sr = new StringReader(sb.ToString().Replace("<br />","<b></b>").Replace("<br>","<br></br>"));

    pdfDoc = new Document(PageSize.A4, 30, 30, 30, 30);

    writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);

    ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);

    IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, writer)));

    XMLWorker worker = new XMLWorker(pipeline, true);
    XMLParser xmlParser = new XMLParser(worker);




    bytes = memoryStream.ToArray();

    return new MemoryStream(bytes);

Thanks you all btw!