渲染包含 Azure Blob 存储中存储的图像的 PDF 文件
Azure Blob Storage 是 Microsoft Azure 提供的基于云的存储服务。它设计用于存储大量非结构化数据,如文本或二进制数据,可通过 HTTP 或 HTTPS 访问。
有些开发人员希望使用 Azure Blob Storage 中存储的图像。这就产生了一个问题,因为图像数据是以二进制形式存储的,而不是以文件形式存储,很容易被 HTML 引用。解决方法是将图像转换为 base64 字符串,然后将其添加到 img 标签中。
如何渲染 Azure Blob 存储中存储的图像
- 下载 C# 库以渲染存储在 Azure Blob 中的图像
- 处理检索 Blob 的过程
- 使用 ToBase64String 方法将字节转换为 base64
- 在 img 标记中包含 base64 信息
- 将 HTML 转换为 PDF
开始在您的项目中使用IronPDF,并立即获取免费试用。
查看 IronPDF 上 Nuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变PDF。
Install-Package IronPdf
考虑安装 IronPDF DLL 直接。下载并手动安装到您的项目或GAC表单中: IronPdf.zip
手动安装到你的项目中
下载DLL将 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/>"
将 HTML 转换为 PDF
继续使用 imageTag,然后使用 ChromePdfRenderer 的 RenderHtmlAsPdf
方法将其转换为 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")