Azure Blob Storageに保存されている画像を含む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 Storageに保存されている画像を使用したいと考えています。 これは問題を引き起こします。なぜなら、画像データがファイルとしてではなくバイナリとして保存されているため、HTMLで簡単に参照することができないからです。 回避策として、画像をbase64文字列に変換し、imgタグに追加します。


PDF 用 C# NuGet ライブラリ

でインストール NuGet

Install-Package IronPdf
または
Java PDF JAR(ジャバPDF JAR)

ダウンロード DLL (ディーエルエル)

DLLをダウンロード

プロジェクトに手動でインストールする

PDF 用 C# NuGet ライブラリ

でインストール NuGet

Install-Package IronPdf
または
Java PDF JAR(ジャバPDF JAR)

ダウンロード DLL (ディーエルエル)

DLLをダウンロード

プロジェクトに手動でインストールする

今日からプロジェクトでIronPDFを使い始めましょう。無料のトライアルをお試しください。

最初のステップ:
green arrow pointer

チェックアウト IronPDF オン Nuget 迅速なインストールと展開のために。8百万以上のダウンロード数により、PDFをC#で変革しています。

PDF 用 C# NuGet ライブラリ nuget.org/packages/IronPdf/
Install-Package IronPdf

インストールを検討してください IronPDF DLL 直接。ダウンロードして、プロジェクトまたはGACの形式で手動でインストールしてください。 IronPdf.zip

プロジェクトに手動でインストールする

DLLをダウンロード

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