iTextSharp crea PDF con páginas en blanco
Acabo de agregar el paquete nuget iTextSharp XMLWorker (y sus dependencias) a mi proyecto y estoy tratando de convertir el HTML de una cadena en un archivo PDF, aunque no se lanzan excepciones, el archivo PDF se genera con Dos páginas en blanco. ¿Por qué?
La versión anterior del código estaba usando solo iTextSharp 5.5.8.0 con HTMLWorker y el método ParseList, luego cambié a
Aquí está el código que estoy usando:
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();
}
Si colocodocument.Add(new Paragraph("Just a test"));
justo antes dedocument.Close();
el párrafo se representa en la segunda página, pero el resto del documento todavía está en blanco.
He cambiado el HTML en elhtmlString
variable a solo unDIV
y unTABLE
Y funcionó. Entonces, ahora la pregunta es: ¿cómo sé qué parte del HTML está causando algún error en el XMLWorker?