Azure Blob Storageに保存されている画像を含むPDFのレンダリング
Azure Blob Storageは、Microsoft Azureによって提供されるクラウドベースのストレージサービスです。 これは、HTTPまたはHTTPSを通じてアクセス可能なテキストやバイナリデータなどの大量の非構造化データを保存するように設計されています。
一部の開発者は、Azure Blob Storageに保存されている画像を使用したいと考えています。 これは問題を引き起こします。なぜなら、画像データがファイルとしてではなくバイナリとして保存されているため、HTMLで簡単に参照することができないからです。 回避策として、画像をbase64文字列に変換し、imgタグに追加します。
IronPDFを始めましょう
今日から無料トライアルでIronPDFをあなたのプロジェクトで使い始めましょう。
Azure Blob Storageに保存されている画像をレンダリングする方法
- Azure Blobに保存された画像をレンダリングするIronPDFをダウンロードする
- BLOBの取得プロセスを処理する
- ToBase64String メソッドを使用してバイトを base64 に変換する
- img タグに base64 情報を含める
- HTMLをPDFにレンダリング
Azure Blob を HTML に変換
Azure Storage アカウントをすでにセットアップし、ブロブを含むコンテナーがあると仮定すると、Azure Storage への認証と接続も C# プロジェクトで処理する必要があります。 その後、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")