0
public IHttpActionResult DownloadPDF()
{
    var stream = CreatePdf();

    return ResponseMessage(new HttpResponseMessage
    {
        Content = new StreamContent(stream)
        {
            Headers =
            {
                ContentType = new MediaTypeHeaderValue("application/pdf"),
                ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "myfile.pdf"
                }
            }
        },
        StatusCode = HttpStatusCode.OK
    });
}
Here is the CreatePdf method:

private Stream CreatePdf()
{
    using (var document = new Document(PageSize.A4, 50, 50, 25, 25))
    {
        var output = new MemoryStream();

        var writer = PdfWriter.GetInstance(document, output);
        writer.CloseStream = false;

        document.Open();
        document.Add(new Paragraph("Hello World"));
        document.Close();

        output.Seek(0, SeekOrigin.Begin);

        return output;
    }
}

I can able to download the PDF but the context is empty. Here I am using memory stream and I also tried with file stream its downloading in the respective folder but if I tried to open the downloaded file then also the content is empty. Can anyone help me what I'm missing here?

Sabarish
  • 59
  • 1
  • 6

1 Answers1

0

Here is an approach that usually works for me when using Web API

private byte[] CreatePdf() {
    var buffer = new byte[0];
    //stream to hold output data
    var output = new MemoryStream();
    //creation of a document-object
    using (var document = new Document(PageSize.A4, 50, 50, 25, 25)) {
        //create a writer that listens to the document
        // and directs a PDF-stream to output stream
        var writer = PdfWriter.GetInstance(document, output);

        //open the document
        document.Open();

        // Create a page in the document
        document.NewPage();

        // Get the top layer to write some text
        var pdfContentBytes = writer.DirectContent;
        pdfContentBytes.BeginText();

        //add content to page
        document.Add(new Paragraph("Hello World"));

        //done writing text
        pdfContentBytes.EndText();

        // make sure any data in the buffer is written to the output stream
        writer.Flush();

        document.Close();
    }    
    buffer = output.GetBuffer();
    return buffer;
}

And then in the action

public IHttpActionResult DownloadPDF() {
    var buffer = CreatePdf();

    return ResponseMessage(new HttpResponseMessage {
        Content = new StreamContent(new MemoryStream(buffer)) {
            Headers = {
                ContentType = new MediaTypeHeaderValue("application/pdf"),
                ContentDisposition = new ContentDispositionHeaderValue("attachment") {
                    FileName = "myfile.pdf"
                }
            }
        },
        StatusCode = HttpStatusCode.OK
    });
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Thanks for your comments. But now, I can't even open the file the following Error "Failed to load PDF document." i'm getting. – Sabarish Dec 08 '17 at 12:59
  • Now, again the previous issue, I don't see the content of the PDF. it's empty here. – Sabarish Dec 08 '17 at 14:10