Empty content on the downloaded PDF using itextsharp in WebAPI 2 response

1.2k Views Asked by At
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?

1

There are 1 best solutions below

2
On

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
    });
}