0

I've tried a number of different variations to output a very basic PDF from memory but all seem to return the same result, which is to say it doesn't actually return anything. The code compiles and runs without error but when VS finishes processing the code nothing happens.

I'm using VS2008 and iTextSharp v5.1.1

Does anyone have any suggestions please?

Here is my code in its current state:

MemoryStream ms = new MemoryStream();           
Document doc = new Document();        
PdfWriter writer = PdfWriter.GetInstance(doc, ms);
writer.CloseStream = false;

doc.Open();
doc.Add(new Paragraph("Test Content"));
doc.Add(new Paragraph(DateTime.Now.ToString()));
doc.Close();

Response.ContentType = "application/pdf";
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();            
Response.OutputStream.Close();
ms.Close();
arserbin3
  • 6,010
  • 8
  • 36
  • 52
Mattplus
  • 1
  • 1
  • 5
  • There's a good working version here: http://bit.ly/kEWMWn. Might be worth your while to compare your attempt with it to see if you're missing anything. I can't remember if iTextSharp writes to an error log - might be worth checking that too if so. – immutabl Jun 23 '11 at 09:08
  • Hi, thanks very much for your suggestion. There were some differences, unfortunately none of which made any difference to the result I'm receiving. Could the issue perhaps have anything to do with the browser I'm using? I'm currently running IE9, but am unable to test in other browsers as the website has been written to be browser specific to IE only. – Mattplus Jun 23 '11 at 09:25
  • I think it might be worth testing with other browsers if you can, but I would have thought its more likely to be a serverside issue. Does your code throw any exceptions? Can you step through it in the debugger? And is there an error log you can look at? – immutabl Jun 23 '11 at 09:44
  • Ok, the code does not throw any exceptions and I have stepped through it multiple times (everytime I change something infact), which is why it's quite confusing. There don't appear to be any logs, none of which I can see anyway. I am using third party logging but as VS isn't throwing any errors those logs are clear. – Mattplus Jun 23 '11 at 09:49
  • Edit: I have managed to test in Chrome, which results in the exact same issue. – Mattplus Jun 23 '11 at 09:55
  • Tried same code and same dll with framework 3.5 and 4.0... it works properly in chrome, firefox and ie! – danyolgiax Jun 23 '11 at 11:15
  • Hmmm, I can't understand it then. Is there anything else I could be doing wrong? Or anything else that could be set up incorrectly, e.g. IIS or something like that? Many thanks again. – Mattplus Jun 23 '11 at 12:31
  • Have you tried doing `Response.Clear()` before you write the pdf to the response? You may be getting some page markup at the beginning of the stream which is confusing the browser. – Brook Jun 23 '11 at 12:45
  • Yes, I've already tried completely clearing the response, content and headers. Have just tried on a colleagues machine using a blank website which worked exactly as it should. This leads me to believe it has something to do with IIS. I've also tried giving temporary full access to all users to exclude the possibility of it being a permissions related issue. – Mattplus Jun 23 '11 at 13:16

2 Answers2

0

One thing that I learned early on, don't use GetBuffer(), use ToArray(). See this post:

iTextSharp-generated PDFs now cause Save dialog in Adobe Reader X

Community
  • 1
  • 1
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • Thank you for your reply, I've just tried this and didn't make any difference. I'm sure this is IIS related and not code related, as all variations of code seem to end in the same result. I've already checked mimetypes and allowed filenameextensions but there muast be something else. – Mattplus Jun 23 '11 at 13:23
  • There's been a development... I've just created a brand new website on my local machine to test the code, which works! But for some reason it just will not work in my actual project. – Mattplus Jun 23 '11 at 13:44
  • Then this isn't an iText problem, it's a `Response` problem. I suggest you retag appropriately. – Mark Storer Jun 23 '11 at 20:54
0

I found what was causing my issue, the code was in a button_click event where the button control was inside an ajax update panel, as soon as i moved the button outside of the update panel it worked perfectly. Not sure if this is a fundamental mistake on my part or a bug with update panels so I'm off to have a read about them.

@Mark Storer, I appreciate now that this wasn't an ITextSharp problem, however when I wrote this I believed it to be, apologies to all for the mistake.

Mattplus
  • 1
  • 1
  • 5