C
C#5w ago
jooeyvh

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
Sehra
Sehra5w ago
var stream = new MemoryStream();
using (var writer = new PdfWriter(stream))
{
writer.SetCloseStream(false);
using var pdf = new PdfDocument(writer);
using var doc = new Document(pdf);
// create pdf
}
stream.Position = 0;
// upload
var stream = new MemoryStream();
using (var writer = new PdfWriter(stream))
{
writer.SetCloseStream(false);
using var pdf = new PdfDocument(writer);
using var doc = new Document(pdf);
// create pdf
}
stream.Position = 0;
// upload
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?
jooeyvh
jooeyvhOP5w ago
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
Sehra
Sehra5w ago
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
jooeyvh
jooeyvhOP5w ago
Right, my UploadToBlobStorage method looks like this, usually works fine
public async Task UploadToBlobStorage(MemoryStream pdfStream, string blobName)
{
pdfStream.Position = 0;
BlobServiceClient blobServiceClient = new BlobServiceClient(Environment.GetEnvironmentVariable("connectionString"));
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(Environment.GetEnvironmentVariable("containerName"));
await containerClient.CreateIfNotExistsAsync();
BlobClient blobClient = containerClient.GetBlobClient(blobName);

await blobClient.UploadAsync(pdfStream);
}
public async Task UploadToBlobStorage(MemoryStream pdfStream, string blobName)
{
pdfStream.Position = 0;
BlobServiceClient blobServiceClient = new BlobServiceClient(Environment.GetEnvironmentVariable("connectionString"));
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(Environment.GetEnvironmentVariable("containerName"));
await containerClient.CreateIfNotExistsAsync();
BlobClient blobClient = containerClient.GetBlobClient(blobName);

await blobClient.UploadAsync(pdfStream);
}
Sehra
Sehra5w ago
but are you awaiting UploadToBlobStorage? firing off a request without await and then disposing the memory stream will not go well
jooeyvh
jooeyvhOP5w ago
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
wasabi
wasabi5w ago
Just FYI because many people don't notice it. iText is AGPL or commercial. Be sure that works for you.
jooeyvh
jooeyvhOP5w ago
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?
glhays
glhays4w ago
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...
jooeyvh
jooeyvhOP4w ago
Nice I will explore QuestPDF deeper. Thank you👍

Did you find this page helpful?