You write directly to the OutputStream , and then also click on the object a custom object. Apart from duplicating efforts, the object you put forward is not a PDF, as we think about it, it is an internal representation of one. I am going to suggest a couple of changes.
First, do not change the flow of HTTP responses until you are 100% sure that you have a valid PDF. Otherwise, error messages are sent with the type of PDF, and everything can get confused quickly. Secondly, similar to the first, do not associate your PDF writer with the HTTP response stream, this will facilitate debugging. Instead, write to a MemoryStream and take the bytes from this if / on success.
EDIT
If the above does not work, then you probably have a problem with these four lines:
StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView1.RenderControl(hw); StringReader sr = new StringReader(sw.ToString());
Try replacing the above four with one line below:
StringReader sr = new StringReader("<p>Hello</p>");
If this works, you need to find out what is wrong with your HTML. Drop all PDF concepts for validation and check sw.ToString() . Remember, iTextSharp has zero ASP.Net knowledge, it can only work with HTML.
If a broken PDF is still being created above, you need to play around with the Response object a bit. Simplify it by removing caching and headers. Do not open the PDF directly, but first download it to disk. Send a link to the PDF and we can help you.
In addition, HTMLWorker very old and is no longer supported. Instead, switch to XmlWorker .
Chris Haas May 16 '14 at 19:33 2014-05-16 19:33
source share