1

I have this HTTP api

http://localhost:1111/myApi/Test 

and that supports JSON and xml as outout format. How can I get pdf as output?

I am using itext and the following code. It does gives me pdf back but stores in root directory of the application. How can I get the pdf open in new window and ask for Open or save dialogue. I am using Chrome Rest Client to test.

     public void Generatepdf (string content)
        {

           Document document = new Document();
            PdfWriter.GetInstance(document, new FileStream("pdfResponses.pdf",                                                     FileMode.Create));

 HttpContext.Current.Response.ContentType = "application/pdf";
WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-disposition", "attachment; filename=someReport.pdf");

        document.Open();        
        document.Add(new Paragraph(content));
        document.Close();
}
Will Hartung
  • 115,893
  • 19
  • 128
  • 203
  • @Will: this is not Servlet code. Look like sort of .NET. user1224171: please insert appropriate language tags, not just "rest" and "api". – BalusC Feb 21 '12 at 19:38
  • This is C# code and I meant for RESTFUL api. I was wondering how that got changed. – user1224171 Feb 21 '12 at 19:38
  • Will Hartung made a wrong edit. I've rectified it. But in the future you really need to be more specific about the language you're using. This site is not for only C#. In any way, to fix this "problem" you should just replace `new FileStream()` by the output stream of the HTTP response. – BalusC Feb 21 '12 at 19:40
  • @BalusC: replacing new FileStream() by the output stream of the HTTP response gives me some messy binary stuff in my output window. – user1224171 Feb 21 '12 at 19:55
  • He mentioned iText, I didn't know there was a C# implementation of iText. This has nothing to do with the REST architecture, it's simply an HTTP request. Not all HTTP requests are REST requests, and, either way, the REST architecture is not germane to this question about how to serve up a PDF over a C# HTTP response. Perhaps this is WCF question or something of that nature, I'm not that familiar with .NET. – Will Hartung Feb 21 '12 at 19:58
  • @user1224171: that's exactly how a PDF file look like when treated as character data instead of binary data :) Perhaps you've passed a stream which is supposed to only take character data, or the content type header isn't properly been set. Well, C# is completely beyond me. Good luck. – BalusC Feb 21 '12 at 20:03
  • There is this iTextSharp for .net – user1224171 Feb 21 '12 at 20:24

0 Answers0