Rendering PDFs Containing Images Stored in Azure Blob Storage
Azure Blob Storage is a cloud-based storage service provided by Microsoft Azure. It's designed to store large amounts of unstructured data, such as text or binary data, that can be accessed via HTTP or HTTPS.
Some developers would like to use images stored in Azure Blob Storage. This presents an issue because the image data is stored as binary rather than as a file, which could easily be referenced by HTML. The workaround is to convert images to a base64 string and add them to an img tag.
How to Render Images Stored in Azure Blob Storage
- Download the C# library to render images stored in Azure Blob
- Handle the process of retrieving the blob
- Use the ToBase64String method to convert bytes to base64
- Include the base64 information in the img tag
- Render the HTML to PDF
Install with NuGet
Install-Package IronPdf
Download DLL
Manually install into your project
// Get Blob
var blob = blobContainer.GetBlobReference("867.jpg");
var stream = new MemoryStream();
await blob.DownloadToStreamAsync(stream);
var array = new byte[blob.Properties.Length];
await blob.DownloadToByteArrayAsync(target: array, 0);
// Convert bytes to base64
var base64 = Convert.ToBase64String(array);
var ImageTag = $"<img src=\"data:image/jpeg;base64, {base64}\"/><br/>";
// Get Blob
var blob = blobContainer.GetBlobReference("867.jpg");
var stream = new MemoryStream();
await blob.DownloadToStreamAsync(stream);
var array = new byte[blob.Properties.Length];
await blob.DownloadToByteArrayAsync(target: array, 0);
// Convert bytes to base64
var base64 = Convert.ToBase64String(array);
var ImageTag = $"<img src=\"data:image/jpeg;base64, {base64}\"/><br/>";
' Get Blob
Dim blob = blobContainer.GetBlobReference("867.jpg")
Dim stream = New MemoryStream()
Await blob.DownloadToStreamAsync(stream)
Dim array = New Byte(blob.Properties.Length - 1){}
Await blob.DownloadToByteArrayAsync(target:= array, 0)
' Convert bytes to base64
Dim base64 = Convert.ToBase64String(array)
Dim ImageTag = $"<img src=""data:image/jpeg;base64, {base64}""/><br/>"
The ImageTag variable can then be merged into the main HTML document and rendered as a PDF.