I have followed the answer in this question and tried to output a pdf from an MVC view with iTextSharp to test the rendering fidelity.
I have the following code:
var ms = new MemoryStream();
var document = new Document(PageSize.A4, 10, 10, 10, 10);
var writer = PdfWriter.GetInstance(document, ms);
document.Open();
var html = this.RenderView(GetViewName(), reportVM);
var css = System.IO.File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "\\Content\\site.css"));
var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(css));
var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html));
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, msHtml, msCss);
document.Close();
return File(ms, "application/pdf");
Unfortunately it returns a window in the browser with the message:
Failed to load PDF
What are the main causes of this behavior?
Note: I have produced a pdf from the same html with PdfSharp which is based on iTextSharp too, so I guess I am not using iTextSharp properly.
EDIT:
I have followed Bruno's suggestion in the comments, so I changed the return to :
bytes = ms.ToArray();
return File(new MemoryStream(bytes), "application/pdf");
And now the result is a 2 pages empty pdf, so it is better, but is it possible to make it more accurate, since the content of the pdf should have some text inside?