渲染包含儲存在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 Storage帳戶並且有一個包含blob的容器,您還需要在C#項目中處理認證和連接到您的Azure Storage。 之後您可以使用 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#