Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning memorystream - gives corrupt PDF file or "cannot accessed a closed stream"

I have a web service, which calls the following method. I want to return a memorystream, which is a PDF file.

Now, the problem is the PDF file is corrupt with the following code. I think it's because the files are not being closed. However, if I close them, I get the classic error "Cannot access a closed stream".

When I previously saved it through a filestream, the PDF file wasn't corrupt.

So my humble question is: How to solve it and get back a non-corrupt PDF file? :-)

My code:

public Stream Generate(GiftModel model)
{
    var template = HttpContext.Current.Server.MapPath(TemplatePath);

    // Magic code which creates a new PDF file from the stream of the other
    PdfReader reader = new PdfReader(template);
    Rectangle size = reader.GetPageSizeWithRotation(1);
    Document document = new Document(size);

    MemoryStream fs = new MemoryStream();
    PdfWriter writer = PdfWriter.GetInstance(document, fs);
    document.Open();

    // Two products on every page
    int bookNumber = 0;
    int pagesWeNeed = (int)Math.Ceiling(((double)model.Books.Count / (double)2));
    for (var i = 0; i < pagesWeNeed; i++)
    {
        PdfContentByte cb = writer.DirectContent;

        // Creates a new page
        PdfImportedPage page = writer.GetImportedPage(reader, 1);
        cb.AddTemplate(page, 0, 0);

        // Add text strings
        DrawGreetingMessages(model.FromName, model.ReceiverName, model.GiftMessage, cb);

        // Draw the books
        DrawBooksOnPage(model.Books.Skip(bookNumber).Take(2).ToList(), cb);

        // Draw boring shit
        DrawFormalities(true, model.GiftLink, cb);

        bookNumber += 2;
    }

    // Close off our streams because we can
    //document.Close();
    //writer.Close();
    reader.Close();

    fs.Position = 0;
    return fs;
}
like image 550
Lars Holdgaard Avatar asked Dec 16 '22 03:12

Lars Holdgaard


1 Answers

Reuse of streams can be problematic, especially if you are using an abstraction and you don't quite know what it is doing to your stream. Because of this I generally recommend never passing streams themselves around. If you can by with it, try just passing the raw underlying byte array itself. But if passing streams is a requirement then I recommend still doing the raw byte array at the end and then wrapping that in a new second stream. Try the below code to see if it works.

public Stream Generate(GiftModel model)
{
    //We'll dump our PDF into these when done
    Byte[] bytes;

    using (var ms = new MemoryStream())
    {
        using (var doc = new Document())
        {
            using (var writer = PdfWriter.GetInstance(doc, ms))
            {
                doc.Open();
                doc.Add(new Paragraph("Hello"));
                doc.Close();
            }
        }
        //Finalize the contents of the stream into an array
        bytes = ms.ToArray();
    }
    //Return a new stream wrapped around our array
    return new MemoryStream(bytes);
}
like image 112
Chris Haas Avatar answered Jan 25 '23 08:01

Chris Haas