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
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

C# NuGet Library for PDF

Install with NuGet

Install-Package IronPdf
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer

Check out IronPDF on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming PDF with C#.

C# NuGet Library for PDF nuget.org/packages/IronPdf/
Install-Package IronPdf

Consider installing the IronPDF DLL directly. Download and manually install it for your project or GAC form: IronPdf.zip

Manually install into your project

Download DLL

Convert Azure Blob to HTML

Assuming you have already set up an Azure Storage account and have a container with blobs, you also need to handle authentication and connection to your Azure Storage in the C# project. Afterward you can use the DownloadToStreamAsync method to download the image as a stream. The stream information can then be converted to Base64 and embedded in the img tag of HTML. Finally, the imageTag variable can be merged into an HTML document.

// Define your connection string and container name
string connectionString = "your_connection_string";
string containerName = "your_container_name";

// Initialize BlobServiceClient with the connection string
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

// Get the BlobContainerClient for the specified container
BlobContainerClient blobContainer = blobServiceClient.GetBlobContainerClient(containerName);

// 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/>";
// Define your connection string and container name
string connectionString = "your_connection_string";
string containerName = "your_container_name";

// Initialize BlobServiceClient with the connection string
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

// Get the BlobContainerClient for the specified container
BlobContainerClient blobContainer = blobServiceClient.GetBlobContainerClient(containerName);

// 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/>";
' Define your connection string and container name
Dim connectionString As String = "your_connection_string"
Dim containerName As String = "your_container_name"

' Initialize BlobServiceClient with the connection string
Dim blobServiceClient As New BlobServiceClient(connectionString)

' Get the BlobContainerClient for the specified container
Dim blobContainer As BlobContainerClient = blobServiceClient.GetBlobContainerClient(containerName)

' 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#

Convert HTML to PDF

Continuing from the imageTag, it can then be converted to PDF by using the RenderHtmlAsPdf method of ChromePdfRenderer.

:path=/static-assets/pdf/content-code-examples/how-to/images-azure-blob-storage-html-to-pdf.cs
using IronPdf;

// Instantiate Renderer
var renderer = new ChromePdfRenderer();

// Create a PDF from a HTML string using C#
var pdf = renderer.RenderHtmlAsPdf(imageTag);

// Export to a file
pdf.SaveAs("imageToPdf.pdf");
Imports IronPdf

' Instantiate Renderer
Private renderer = New ChromePdfRenderer()

' Create a PDF from a HTML string using C#
Private pdf = renderer.RenderHtmlAsPdf(imageTag)

' Export to a file
pdf.SaveAs("imageToPdf.pdf")
VB   C#