'can BR tag work inside TD with XmlWorker?
<td>text text text</br>text text<td>
is legitimate html - but it throws an error with XmlWorker 5.5.5 and iText 5.5.5
com.itextpdf.tool.xml.exceptions.RuntimeWorkerException: Invalid nested tag br found, expected closing tag td.
if you remove the 'br' then the code works, only of course you do not get the multiline row
this is also not fixed by using white-space:pre in the td css, and converting the 'br' to a carriage return, as the new line is effectively ignored by iText
Is this a feature/ issue/ never been asked for before thing? Or am I missing something not in the examples?
html file... link
Solution 1:[1]
This is invalid XHTML:
<td>text text text</br>text text<td>
This is valid XHTML:
<td>text text text<br />text text<td>
Please change </br> into <br />. because when an XML parser encounters the closing tag </br> without having encountered the opening tag <br> first, it will throw an exception because your XML is invalid.
Note that <br /> is shorthand for <br></br> (an opening tag immediately followed by a closing tag).
Solution 2:[2]
[HttpPost]
[ValidateInput(false)]
public FileResult Export(string HtmlData, string header, string footer)
{
var finalHtmlData = HtmlData.Replace("<br>", "<br/>"); //Replace <br> to <br/>, otherwise it will throw exception
MemoryStream stream = new System.IO.MemoryStream();
StringReader sr = new StringReader(finalHtmlData);
Document doc = new Document(PageSize.A4, 40, 40, 40, 40);
PdfWriter writer = PdfWriter.GetInstance(doc, stream);
doc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, sr);
doc.Close();
byte[] bytes = stream.ToArray();
stream.Dispose();
Font blackFont = FontFactory.GetFont("Arial", 13, Font.NORMAL, BaseColor.BLACK);
Font blackFont1 = FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK);
PdfReader reader = new PdfReader(bytes);
stream = new MemoryStream();
using (PdfStamper stamper = new PdfStamper(reader, stream))
{
int pages = reader.NumberOfPages;
for (int i = 1; i <= pages; i++)
{
ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_CENTER, new Phrase(header, blackFont1), 300f, 810f, 0);
ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_CENTER, new Phrase(footer, blackFont1), 300f, 15f, 0);
ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, new Phrase("Page " + i.ToString() + " of " + pages, blackFont1), 568f, 15f, 0);
}
}
bytes = stream.ToArray();
return File(bytes, "application/pdf", "Test.pdf");
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bruno Lowagie |
| Solution 2 | Md Shahriar |
