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.
Get started with IronPDF
Start using IronPDF in your project today with a free trial.
How to Render Images Stored in Azure Blob Storage
- Download IronPDF for rendering 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
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
}
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
// Import the IronPdf library to work with PDF rendering.
using IronPdf;
// Create a new instance of the ChromePdfRenderer.
// This object will allow us to render HTML content into a PDF format.
var renderer = new ChromePdfRenderer();
// Define the HTML string that we want to convert to PDF.
// This is the HTML content you want to render as PDF.
var htmlContent = "<html><body><h1>Hello, World!</h1><p>This is a sample HTML to PDF conversion.</p></body></html>";
// Use the renderer to convert the HTML string to a PDF document.
// RenderHtmlAsPdf takes the HTML string as input and returns a PDFDocument object.
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Save the resulting PDF document to a file named "imageToPdf.pdf".
// The PDF file will be saved in the current working directory of the application.
pdfDocument.SaveAs("imageToPdf.pdf");
Make sure to adjust the "htmlContent"
variable to include your actual HTML content where the imageTag
is used.
Frequently Asked Questions
What is Azure Blob Storage?
Azure Blob Storage is a cloud-based storage service provided by Microsoft Azure, designed to store large amounts of unstructured data that can be accessed via HTTP or HTTPS.
How can I render images stored in Azure Blob Storage as part of a PDF?
To render images stored in Azure Blob Storage in a PDF, you need to convert the image data to a base64 string, embed it in an HTML img tag, and then render the HTML to PDF using IronPDF.
What are the steps to convert an Azure blob to HTML?
First, set up an Azure Storage account and container. Then, use the `DownloadToStreamAsync` method to download the blob as a stream, convert it to a base64 string, and embed it in an HTML img tag.
How do I convert HTML to PDF using IronPDF?
You can convert HTML to PDF using IronPDF by utilizing the `RenderHtmlAsPdf` method of the `ChromePdfRenderer` class, which takes HTML content and renders it as a PDF document.
What is the purpose of the base64 conversion when working with Azure Blob Storage?
The base64 conversion allows you to embed binary image data directly into an HTML document as a data URL, which can then be rendered correctly in a PDF.
Can I use IronPDF to handle images directly from Azure Blob Storage?
Yes, IronPDF can be used to handle images stored in Azure Blob Storage by converting them to base64 and embedding them in HTML before rendering to PDF.
Do I need to set up authentication for accessing Azure Blob Storage?
Yes, you need to handle authentication and establish a connection to your Azure Storage account in your C# project to access blobs.
What is IronPDF?
IronPDF is a .NET library that allows developers to render HTML into PDF documents, including the ability to include images and other media.
How do I download IronPDF?
You can download IronPDF from the NuGet package manager by searching for 'IronPdf' or visiting the NuGet website.
What programming language is used for the examples in the guide?
The examples in the guide are written in C#.