渲染包含存儲在 Azure Blob 存儲中的圖片的 PDF

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

Azure Blob Storage 是由 Microsoft Azure 提供的基於雲端的儲存服務。它的設計目的是存放大量的非結構化數據,例如文本或二進位數據,並可以通過 HTTP 或 HTTPS 訪問。

一些開發人員希望使用存儲在 Azure Blob 存儲中的圖片。這會帶來一些問題,因為圖片數據是以二進位形式存儲的,而不是作為文件,這樣能夠被 HTML 輕易引用。解決辦法是將圖片轉換成 base64 字串,然後將它們添加到 img 標籤中。


C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

立即開始在您的專案中使用IronPDF,並享受免費試用。

第一步:
green arrow pointer

查看 IronPDFNuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變PDF。

C# NuGet 程式庫用于 PDF nuget.org/packages/IronPdf/
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/>"
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#