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.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet

    PM > Install-Package IronPdf

  2. Copy the code

    var blobBase64 = Convert.ToBase64String(new BlobContainerClient("conn","cont").GetBlobClient("img.jpg").DownloadContent().Value.Content.ToArray());
    new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf($"<img src=\"data:image/jpeg;base64,{blobBase64}\" />").SaveAs("blobImage.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

Get started with IronPDF

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

First Step:
green arrow pointer



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.

using Azure.Storage.Blobs;
using System;
using System.IO;
using System.Threading.Tasks;

public async Task ConvertBlobToHtmlAsync()
{
    // 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 the reference to the blob and initialize a stream
    BlobClient blobClient = blobContainer.GetBlobClient("867.jpg");
    using var stream = new MemoryStream();

    // Download the blob data to the stream
    await blobClient.DownloadToAsync(stream);
    stream.Position = 0; // Reset stream position

    // Convert the stream to a byte array
    byte[] array = stream.ToArray();

    // Convert bytes to base64
    var base64 = Convert.ToBase64String(array);

    // Create an img tag with the base64-encoded string
    var imageTag = $"<img src=\"data:image/jpeg;base64,{base64}\"/><br/>";

    // Use the imageTag in your HTML document as needed
}
using Azure.Storage.Blobs;
using System;
using System.IO;
using System.Threading.Tasks;

public async Task ConvertBlobToHtmlAsync()
{
    // 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 the reference to the blob and initialize a stream
    BlobClient blobClient = blobContainer.GetBlobClient("867.jpg");
    using var stream = new MemoryStream();

    // Download the blob data to the stream
    await blobClient.DownloadToAsync(stream);
    stream.Position = 0; // Reset stream position

    // Convert the stream to a byte array
    byte[] array = stream.ToArray();

    // Convert bytes to base64
    var base64 = Convert.ToBase64String(array);

    // Create an img tag with the base64-encoded string
    var imageTag = $"<img src=\"data:image/jpeg;base64,{base64}\"/><br/>";

    // Use the imageTag in your HTML document as needed
}
Imports Azure.Storage.Blobs
Imports System
Imports System.IO
Imports System.Threading.Tasks

Public Async Function ConvertBlobToHtmlAsync() As Task
	' 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 the reference to the blob and initialize a stream
	Dim blobClient As BlobClient = blobContainer.GetBlobClient("867.jpg")
	Dim stream = New MemoryStream()

	' Download the blob data to the stream
	Await blobClient.DownloadToAsync(stream)
	stream.Position = 0 ' Reset stream position

	' Convert the stream to a byte array
	Dim array() As Byte = stream.ToArray()

	' Convert bytes to base64
	Dim base64 = Convert.ToBase64String(array)

	' Create an img tag with the base64-encoded string
	Dim imageTag = $"<img src=""data:image/jpeg;base64,{base64}""/><br/>"

	' Use the imageTag in your HTML document as needed
End Function
$vbLabelText   $csharpLabel

Convert HTML to PDF

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

Below is a simple example of how you might call RenderHtmlAsPdf:

: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")
$vbLabelText   $csharpLabel

Make sure to adjust the "htmlContent" variable to include your actual HTML content where the imageTag is used.

Frequently Asked Questions

How can I render a PDF with images from Azure Blob Storage?

To render a PDF with images from Azure Blob Storage, you can use IronPDF to convert the image data to a base64 string, embed it in an HTML img tag, and then render the HTML as a PDF using the `RenderHtmlAsPdf` method.

What steps are needed to convert an image from Azure Blob Storage to a base64 string?

First, download the image blob using the `DownloadToStreamAsync` method. Then, convert the image stream to a byte array and use `Convert.ToBase64String` to transform it into a base64 string.

How do I include images from Azure Blob Storage in an HTML document?

Download the image as a stream from Azure Blob Storage, convert it to a base64 string, and embed it in an HTML img tag. This allows you to include the image directly in the HTML document.

What is the role of Azure Blob Storage in handling images for PDFs?

Azure Blob Storage is used to store unstructured image data. Converting this data to base64 allows it to be embedded in HTML, facilitating its inclusion in PDFs using IronPDF.

How can I convert HTML content with embedded images to a PDF in C#?

You can convert HTML content, including embedded base64 images, to a PDF using IronPDF's `RenderHtmlAsPdf` method. This method processes the HTML and renders it as a PDF document.

Do I need to authenticate to access blobs from Azure Blob Storage?

Yes, authentication is necessary to establish a connection to your Azure Storage account in your C# project to access and manage blobs securely.

What is the significance of converting images to base64 for PDF rendering?

Converting images to base64 enables embedding binary image data directly into HTML documents as data URLs, which can be accurately rendered into PDFs using IronPDF.

How can I start using IronPDF to render PDFs?

You can download IronPDF from the NuGet package manager. Once installed, you can use its methods to render HTML content, including images, into PDF documents.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.