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.


C# NuGet Library for PDF

Install with NuGet

Install-Package IronPdf
or
C# PDF DLL

Download DLL

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/>"
VB   C#

The ImageTag variable can then be merged into the main HTML document and rendered as a PDF.