Problem storing iText7 pdf in a memoryStream
Hey,
I am having a problem that I hope someone can help me sort out. I'm working on a project in C#, my solution will create a pdf file and I'm using iText7 for that purpose. It works fine storing the pdf locally but the next step is to store it in a memoryStream instead of a local file for later upload to Azure. However, something is weird and I just can't figure it out.
Hopefully someone here will be able to help me or can give me a hint in right direction.
These errors occur in the "memoryStream.Position" part:
ReadTimeout = 'memoryStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'
WriteTimeout = 'memoryStream.WriteTimeout' threw an exception of type 'System.InvalidOperationException'
My code:
MemoryStream memoryStream = new MemoryStream();
PdfWriter writer = new PdfWriter(filePath);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
{
// Code for constructing and mapping information to pdf
}
memoryStream.Position = 0;
Utilities.UploadToBlobStorage(memoryStream, connectionString, containerName, blobName);
memoryStream.Dispose();
10 Replies
give this a try, guessing you need to properly close the writer while keeping the stream open
also MemoryStream always throw on accessing ReadTimeout or WriteTimeout. you sure it's not UploadToBlobStorage that tries to read them without checking CanTimeout first?
Thanks a lot for this, it won't upload anything to the blob storage, however, it doesn't throw any weird errors except for those two you mentioned it always will throw. I will keep investigating why nothing shows up on the blob storage
just setting position would not try to access the timeout properties, i'd look at UploadToBlobStorage to see what it does. and if it's also async you are missing an await
Right, my UploadToBlobStorage method looks like this, usually works fine
but are you awaiting UploadToBlobStorage? firing off a request without await and then disposing the memory stream will not go well
oh yeah of course 👍 that was the reason why it didn't work, I made some adjustments and added await to the UploadToBlobStorage method call. Thank you so much for all the help
Just FYI because many people don't notice it. iText is AGPL or commercial. Be sure that works for you.
thanks for that information I didn't know. In my particular case it won't be an issue but good to know for the future definitely. Have you used anything similar to iText that you could recommend?
GitHub
GitHub - QuestPDF/QuestPDF: QuestPDF is a modern open-source .NET l...
QuestPDF is a modern open-source .NET library for PDF document generation. Offering comprehensive layout engine powered by concise and discoverable C# Fluent API. Easily generate PDF reports, invoi...
Nice I will explore QuestPDF deeper. Thank you👍