Renderización de PDF que contienen imágenes almacenadas en Azure Blob Storage
Azure Blob Storage es un servicio de almacenamiento en la nube proporcionado por Microsoft Azure. Está diseñado para almacenar grandes cantidades de datos no estructurados, como texto o datos binarios, a los que se puede acceder a través de HTTP o HTTPS.
Algunos desarrolladores desean utilizar imágenes almacenadas en Azure Blob Storage. Esto plantea un problema porque los datos de la imagen se almacenan en formato binario y no como un archivo, que podría ser fácilmente referenciado por HTML. La solución consiste en convertir las imágenes a una cadena base64 y añadirlas a una etiqueta img.
Comience con IronPDF
Comience a usar IronPDF en su proyecto hoy con una prueba gratuita.
Cómo renderizar imágenes almacenadas en Azure Blob Storage
- Descargar IronPDF para renderizar imágenes almacenadas en Azure Blob
- Manejar el proceso de recuperación de la mancha
- Utilice el método ToBase64String para convertir bytes a base64
- Incluir la información base64 en la etiqueta img
- Convertir HTML en PDF
Convertir Azure Blob a HTML
Asumiendo que ya has configurado una cuenta Azure Storage y tienes un contenedor con blobs, también necesitas manejar la autenticación y conexión a tu Azure Storage en el proyecto C#. Después puedes usar el método DownloadToStreamAsync
para descargar la imagen como un stream. A continuación, la información del flujo puede convertirse a Base64 e incrustarse en la etiqueta img de HTML. Por último, la variable imageTag puede integrarse en un documento 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/>"
Convertir HTML a PDF
A partir de la imageTag, se puede convertir a PDF utilizando el método RenderHtmlAsPdf
de ChromePdfRenderer.
: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")