Convertir HTML a PDF en .NET

Resumen

Este artículo explica cómo convertir HTML a PDF usando C#. Cubre los siguientes temas.

El siguiente fragmento de código también funciona con la biblioteca Aspose.PDF.Drawing.

Formato: HTML

Formato: MHTML

Formato: WebPage

Conversión de C# HTML a PDF

Aspose.PDF for .NET es una API de manipulación de PDF que te permite convertir cualquier documento HTML existente a PDF sin problemas. El proceso de conversión de HTML a PDF se puede personalizar de manera flexible.

Convertir HTML a PDF

El siguiente ejemplo de código C# muestra cómo convertir un documento HTML a un PDF.

Pasos: Convertir HTML a PDF en C#

  1. Crea una instancia de la clase HtmlLoadOptions.
  2. Inicializa el objeto Document.
  3. Guarda el documento PDF de salida llamando al método Document.Save().
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertHTMLtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Load the HTML file into a document using HtmlLoadOptions
    var options = new Aspose.Pdf.HtmlLoadOptions();

    // Open HTML document
    using (var document = new Aspose.Pdf.Document(dataDir + "test.html", options))
    {
        // Save PDF document
        document.Save(dataDir + "ConvertHTMLtoPDF_out.pdf");
    }
}

Conversión avanzada de HTML a PDF

El motor de conversión HTML tiene varias opciones que nos permiten controlar el proceso de conversión.

Soporte para consultas de medios

Las consultas de medios son una técnica popular para entregar una hoja de estilo adaptada a diferentes dispositivos. Podemos establecer el tipo de dispositivo utilizando la propiedad HtmlMediaType.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertHTMLtoPDFAdvancedMediaType()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Load the HTML file into a document using HtmlLoadOptions with Print media type
    var options = new HtmlLoadOptions
    {
        // Set Print or Screen mode
        HtmlMediaType = Aspose.Pdf.HtmlMediaType.Print
    };

    // Open HTML document
    using (var document = new Aspose.Pdf.Document(dataDir + "test.html", options))
    {
        // Save PDF document
        document.Save(dataDir + "ConvertHTMLtoPDFAdvancedMediaType_out.pdf");
    }
}

Habilitar (deshabilitar) la incrustación de fuentes

Las páginas HTML a menudo utilizan fuentes (por ejemplo, fuentes de la carpeta local, Google Fonts, etc.). También podemos controlar la incrustación de fuentes en un documento utilizando la propiedad IsEmbedFonts.

 // For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
 private static void ConvertHTMLtoPDFAdvancedEmbedFonts()
 {
     // The path to the documents directory
     var dataDir = RunExamples.GetDataDir_AsposePdf();

     // Load the HTML file into a document using HtmlLoadOptions with the font embedding option set
     var options = new Aspose.Pdf.HtmlLoadOptions
     {
         // Disable font embedding
         IsEmbedFonts = false
     };

     // Open HTML document
     using (var document = new Aspose.Pdf.Document(dataDir + "test_fonts.html", options))
     {
         // Save PDF document
         document.Save(dataDir + "ConvertHTMLtoPDFAdvanced_EmbedFonts_out.pdf");
     }
 }

Gestionar la carga de recursos externos

El motor de conversión proporciona un mecanismo que te permite controlar la carga de ciertos recursos asociados con el documento HTML. La clase HtmlLoadOptions tiene la propiedad CustomLoaderOfExternalResources con la que podemos definir el comportamiento del cargador de recursos. Supongamos que necesitamos reemplazar todas las imágenes PNG con una sola imagen test.jpg y reemplazar la URL externa por interna para otros recursos. Para hacer esto, podemos definir un cargador personalizado SamePictureLoader y apuntar CustomLoaderOfExternalResources a este nombre.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertHTMLtoPDFAdvanced_DummyImage()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Load the HTML file into a document with a custom resource loader for external images
    var options = new Aspose.Pdf.HtmlLoadOptions
    {
        CustomLoaderOfExternalResources = SamePictureLoader
    };

    // Open HTML document
    using (var document = new Aspose.Pdf.Document(dataDir + "test.html", options))
    {
        // Save PDF document
        document.Save(dataDir + "html_test.pdf");
    }
}

private static Aspose.Pdf.LoadOptions.ResourceLoadingResult SamePictureLoader(string resourceURI)
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();
    Aspose.Pdf.LoadOptions.ResourceLoadingResult result;

    if (resourceURI.EndsWith(".png"))
    {
        byte[] resultBytes = File.ReadAllBytes(dataDir + "test.jpg");
        result = new Aspose.Pdf.LoadOptions.ResourceLoadingResult(resultBytes)
        {
            // Set MIME Type
            MIMETypeIfKnown = "image/jpeg"
        };
    }
    else
    {
        result = new Aspose.Pdf.LoadOptions.ResourceLoadingResult(GetContentFromUrl(resourceURI));
    }
    return result;
}

private static byte[] GetContentFromUrl(string url)
{
    var httpClient = new System.Net.Http.HttpClient();
    return httpClient.GetByteArrayAsync(url).GetAwaiter().GetResult();
}

Convertir página web a PDF

Convertir una página web es ligeramente diferente a convertir un documento HTML local. Para convertir el contenido de una página web a formato PDF, primero podemos obtener el contenido de la página HTML utilizando una instancia de HttpClient, crear un objeto Stream, pasar el contenido al objeto Document y renderizar la salida en formato PDF.

Al convertir una página web alojada en un servidor web a PDF:

Pasos: Convertir WebPage a PDF en C#

  1. Lee el contenido de la página utilizando un objeto HttpClient.
  2. Instancia el objeto HtmlLoadOptions y establece la URL base.
  3. Inicializa un objeto Document pasando el objeto stream.
  4. Opcionalmente, establece el tamaño de la página y/o la orientación.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertHTMLtoPDFAdvanced_WebPage()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    const string url = "https://en.wikipedia.org/wiki/Aspose_API";

    // Set page size A3 and Landscape orientation;   
    var options = new Aspose.Pdf.HtmlLoadOptions(url)
    {
        PageInfo =
        {
            Width = 842,
            Height = 1191,
            IsLandscape = true
        }
    };

    // Load the web page content as a stream and create a PDF document
    using (var document = new Aspose.Pdf.Document(GetContentFromUrlAsStream(url), options))
    {
        // Save PDF document
        document.Save(dataDir + "html_test.pdf");
    }
}

private static Stream GetContentFromUrlAsStream(string url, System.Net.ICredentials credentials = null)
{
    using (var handler = new System.Net.Http.HttpClientHandler { Credentials = credentials })
    using (var httpClient = new System.Net.Http.HttpClient(handler))
    {
        return httpClient.GetStreamAsync(url).GetAwaiter().GetResult();
    }
}

Proporcionar credenciales para la conversión de página web a PDF

A veces necesitamos realizar la conversión de archivos HTML que requieren autenticación y privilegios de acceso, de modo que solo los usuarios auténticos puedan obtener el contenido de la página. También incluye el escenario en el que algunos recursos/datos referenciados dentro de HTML se obtienen de algún servidor externo que requiere autenticación y, para atender este requisito, se agregó la propiedad ExternalResourcesCredentials a la clase HtmlLoadOptions. El siguiente fragmento de código muestra los pasos para pasar credenciales para solicitar HTML y sus respectivos recursos mientras se convierte un archivo HTML a PDF.

 // For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
 private static void ConvertHTMLtoPDFAdvancedAuthorized()
 {
     // The path to the documents directory
     var dataDir = RunExamples.GetDataDir_AsposePdf();

     const string url = "http://httpbin.org/basic-auth/user1/password1";
     var credentials = new System.Net.NetworkCredential("user1", "password1");

     var options = new Aspose.Pdf.HtmlLoadOptions(url)
     {
         ExternalResourcesCredentials = credentials
     };

     using (var document = new Aspose.Pdf.Document(GetContentFromUrlAsStream(url, credentials), options))
     {
         // Save PDF document
         document.Save(dataDir + "HtmlTest_out.pdf");
     }
 }

private static Stream GetContentFromUrlAsStream(string url, System.Net.ICredentials credentials = null)
{
    using (var handler = new System.Net.Http.HttpClientHandler { Credentials = credentials })
    using (var httpClient = new System.Net.Http.HttpClient(handler))
    {
        return httpClient.GetStreamAsync(url).GetAwaiter().GetResult();
    }
}

Renderizar todo el contenido HTML en una sola página

Aspose.PDF for .NET proporciona la capacidad de renderizar todo el contenido en una sola página mientras se convierte un archivo HTML a formato PDF. Por ejemplo, si tienes algún contenido HTML cuyo tamaño de salida es mayor que una página, puedes usar la opción para renderizar los datos de salida en una sola página PDF. Para usar esta opción, la clase HtmlLoadOptions se amplió con la bandera IsRenderToSinglePage. El siguiente fragmento de código muestra cómo usar esta funcionalidad.

 // For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
 private static void ConvertHTMLtoPDFAdvancedSinglePageRendering()
 {
     // The path to the documents directory
     var dataDir = RunExamples.GetDataDir_AsposePdf();

     // Initialize HtmlLoadOptions
     var options = new Aspose.Pdf.HtmlLoadOptions
     {
         // Set Render to single page property
         IsRenderToSinglePage = true
     };

     // Open PDF document
     using (var document = new Aspose.Pdf.Document(dataDir + "HTMLToPDF.html", options))
     {
         // Save PDF document
         document.Save(dataDir + "RenderContentToSamePage_out.pdf");
     }
 }

Renderizar HTML con datos SVG

Aspose.PDF for .NET proporciona la capacidad de convertir una página HTML a un documento PDF. Dado que HTML permite agregar elementos gráficos SVG como una etiqueta en la página, Aspose.PDF también admite la conversión de dichos datos en el archivo PDF resultante. El siguiente fragmento de código muestra cómo convertir archivos HTML con etiquetas gráficas SVG a documentos PDF etiquetados.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertHTMLtoPDFWithSVG()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Initialize HtmlLoadOptions
    var options = new Aspose.Pdf.HtmlLoadOptions(Path.GetDirectoryName(dataDir + "HTMLSVG.html"));

    // Initialize Document object
    using (var document = new Aspose.Pdf.Document(dataDir + "HTMLSVG.html", options))
    {
        // Save PDF document
        document.Save(dataDir + "RenderHTMLwithSVGData_out.pdf");
    }
}

Convertir MHTML a PDF

MHTML, abreviatura de MIME HTML, es un formato de archivo de archivo de página web utilizado para combinar recursos que normalmente están representados por enlaces externos (como imágenes, animaciones Flash, applets de Java y archivos de audio) con código HTML en un solo archivo. El contenido de un archivo MHTML está codificado como si fuera un mensaje de correo electrónico HTML, utilizando el tipo MIME multipart/related. Aspose.PDF for .NET puede convertir archivos HTML a formato PDF y con el lanzamiento de Aspose.PDF for .NET 9.0.0, hemos introducido una nueva función que te permite convertir archivos MHTML a formato PDF. El siguiente fragmento de código muestra cómo convertir archivos MHTML a formato PDF con C#:

Pasos: Convertir MHTML a PDF en C#

  1. Crea una instancia de la clase MhtLoadOptions.
  2. Inicializa el objeto Document.
  3. Guarda el documento PDF de salida llamando al método Document.Save().
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertMHTtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Initialize MhtLoadOptions with page setup
    var options = new Aspose.Pdf.MhtLoadOptions()
    {
        PageInfo = { Width = 842, Height = 1191, IsLandscape = true }
    };

    // Initialize Document object using the MHT file and options
    using (var document = new Aspose.Pdf.Document(dataDir + "fileformatinfo.mht", options))
    {
        // Save PDF document
        document.Save(dataDir + "MhtmlTest_out.pdf");
    }
}

Ver También

Este artículo también cubre estos temas. Los códigos son los mismos que los anteriores.

Formato: HTML

Formato: MHTML

Formato: WebPage