在Azure Blob存储中存储的图像渲染PDFs

This article was translated from English: Does it need improvement?
Translated
View the article in English

Azure Blob存储是Microsoft Azure提供的基于云的存储服务。 它旨在存储大量非结构化数据,例如文本或二进制数据,这些数据可以通过HTTP或HTTPS访问。

有些开发人员希望使用存储在Azure Blob Storage中的图片。 这引发了一个问题,因为图像数据是以二进制形式存储的,而不是作为文件存储,后者可以被HTML轻松引用。 解决方法是将图像转换为 base64 字符串,并将它们添加到 img 标签中。

开始使用IronPDF

立即在您的项目中开始使用IronPDF,并享受免费试用。

第一步:
green arrow pointer



将 Azure Blob 转换为 HTML

假设您已经设置了一个Azure存储账户,并且有一个包含blob的容器,您还需要在C#项目中处理认证和连接到您的Azure存储。 之后您可以使用 DownloadToStreamAsync 方法将图像下载为流。 流信息然后可以转换为Base64并嵌入到HTML的img标签中。 最后,imageTag变量可以合并到HTML文档中。

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

将 HTML 转换为 PDF

继续从 imageTag 开始,可以通过使用 ChromePdfRendererRenderHtmlAsPdf 方法将其转换为 PDF。

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