ITextSharp creates pdf with blank pages

I just added the nuget XMLWorker iTextSharp package (and its dependencies) to my project, and I'm trying to convert HTML from a string to a PDF file, even though no exceptions are thrown, the PDF file is generated by two blank pages. Why?

In the previous version of the code, only iTextSharp 5.5.8.0 was used using the HTMLWorker and ParseList methods, then I switched to

Here is the code I'm using:

public void ExportToPdf() { string htmlString = ""; Document document = new Document(PageSize.A4, 40, 40, 40, 40); var memoryStream = new MemoryStream(); PdfWriter writer = PdfWriter.GetInstance(document, memoryStream); document.Open(); htmlString = sbBodyMail.ToString(); XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, new StringReader(htmlString)); document.Close(); DownloadFile(memoryStream); } public void DownloadFile(MemoryStream memoryStream) { //Clears all content output from Buffer Stream Response.ClearContent(); //Clears all headers from Buffer Stream Response.ClearHeaders(); //Adds an HTTP header to the output stream Response.AddHeader("Content-Disposition", "attachment;filename=Report_Diagnosis.pdf"); //Gets or Sets the HTTP MIME type of the output stream Response.ContentType = "application/pdf"; //Writes the content of the specified file directory to an HTTP response output stream as a file block Response.BinaryWrite(memoryStream.ToArray()); //Response.Write(doc); //sends all currently buffered output to the client Response.Flush(); //Clears all content output from Buffer Stream Response.Clear(); } 

If I put document.Add(new Paragraph("Just a test")); right before document.Close(); , the paragraph appears on the second page, but the rest of the document is still empty.

UPDATE

I changed the HTML in the htmlString variable htmlString only DIV and TABLE , and it worked. So now the question is: how do I know which part of the HTML is causing some error in XMLWorker?

+6
source share
1 answer

I found out that XMLWorkerHelper was having problems with the DIV width attribute (even the style attribute set) and, unfortunately, it does not throw any exceptions to help you with this.

I found this answer from the developer iTextSharp, which says that centering the table is not yet supported, so I assume this is not supported either.

+4
source

All Articles