1. Home
  2. /
  3. GroupDocs.Conversion.LowCode
  4. /
  5. Developer Guide
  6. /
  7. Using HTML to PDF Converter

Using HTML to PDF Converter

This topic shows how to use the HtmlToPdfConverter with examples that demonstrate common use cases for converting HTML files to PDF.

Prerequisites

Refer to the Developer Guide to learn how to set up your environment and run code examples in this section.

You can also check two related topics on Loading Source Documents and Saving Converted Documents to learn how to specify input and output documents.

Basic Example

The following example shows the most common use case for converting HTML document to PDF. The source HTML file is loaded from a current folder. The converted file is saved to the same folder.

using System;
using GroupDocs.Conversion.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply the license
License.Set(publicKey, privateKey);

// Create a converter for the HTML file
var converter = new HtmlToPdfConverter("sample.html");

// Convert HTML to PDF
converter.Convert("sample.pdf");
Imports System
Imports GroupDocs.Conversion.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply the license
        License.Set(publicKey, privateKey)

        ' Create a converter from file path
        Dim converter As New HtmlToPdfConverter("sample.html")

        ' Convert HTML to PDF
        converter.Convert("sample.pdf")
    End Sub
End Module
open System
open GroupDocs.Conversion.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

    // Apply the license
    License.Set(publicKey, privateKey)

    // Create a converter from file path
    let converter = new HtmlToPdfConverter("sample.html")

    // Convert HTML to PDF
    converter.Convert("sample.pdf")
    0

Sample input file: Download sample.html

Expected output: Download sample.pdf

Web Load Options

This section covers only the main scenarios. You can also refer to the API references for the WebLoadOptions for a complete list of options that you can specify.

Convert HTML with Custom CSS Styling

The following example shows how to apply custom CSS styling when converting HTML to PDF using the CustomCssStyle property.

using System;
using GroupDocs.Conversion.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply license
License.Set(publicKey, privateKey);

// Apply custom CSS styling through load options
var converter = new HtmlToPdfConverter("sample.html", options =>
{
    options.CustomCssStyle = "body { color: coral !important; }";
});

// Convert HTML to PDF
converter.Convert("styled-sample.pdf");
Imports GroupDocs.Conversion.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply license
        License.Set(publicKey, privateKey)

        ' Apply custom CSS styling through load options
        Dim converter As New HtmlToPdfConverter("sample.html", Sub(options)
            options.CustomCssStyle = "body { font-family: Arial, sans-serif; font-size: 14px; color: #333; }"
        End Sub)

        ' Convert HTML to PDF
        converter.Convert("styled-sample.pdf")
    End Sub
End Module
open System
open GroupDocs.Conversion.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

    // Apply license
    License.Set(publicKey, privateKey)

    // Apply custom CSS styling through load options
    let converter = new HtmlToPdfConverter("sample.html", fun options ->
        options.CustomCssStyle <- "body { font-family: Arial, sans-serif; font-size: 14px; color: #333; }"
    )
    
    // Convert HTML to PDF
    converter.Convert("styled-sample.pdf")
    0

Sample input: Download sample.html

Convert HTML with Zoom Level

The following example shows how to set a custom zoom level when converting HTML to PDF using the Zoom property.

Note: When using zoom levels greater than 100%, the content may not fit into standard page sizes. Consider using PageLayoutOptions to fit page by width or height if needed.

using System;
using GroupDocs.Conversion.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply license
License.Set(publicKey, privateKey);

// Set zoom level to 150% through load options
var converter = new HtmlToPdfConverter("sample.html", options =>
{
    options.Zoom = 150; // 150% zoom level
});

// Convert HTML to PDF
converter.Convert("zoomed-sample.pdf");
Imports GroupDocs.Conversion.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply license
        License.Set(publicKey, privateKey)

        ' Set zoom level to 150% through load options
        Dim converter As New HtmlToPdfConverter("sample.html", Sub(options)
            options.Zoom = 150 ' 150% zoom level
        End Sub)

        ' Convert HTML to PDF
        converter.Convert("zoomed-sample.pdf")
    End Sub
End Module
open System
open GroupDocs.Conversion.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")
    
    // Apply license
    License.Set(publicKey, privateKey)

    // Set zoom level to 150% through load options
    let converter = new HtmlToPdfConverter("sample.html", fun options ->
        options.Zoom <- 150 // 150% zoom level
    )
    
    // Convert HTML to PDF
    converter.Convert("zoomed-sample.pdf")
    0

Sample input: Download sample.html

Skip External Resources Referenced in HTML

The following example shows how to skip external resources when converting HTML to PDF using the SkipExternalResources property.

using System;
using GroupDocs.Conversion.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply license
License.Set(publicKey, privateKey);

// Skip external resources through load options
var converter = new HtmlToPdfConverter("with-image.html", options =>
{
    options.SkipExternalResources = true;
});

// Convert HTML to PDF
converter.Convert("without-image.pdf");
Imports GroupDocs.Conversion.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply license
        License.Set(publicKey, privateKey)

        ' Skip external resources through load options
        Dim converter As New HtmlToPdfConverter("with-image.html", Sub(options)
            options.SkipExternalResources = True
        End Sub)

        ' Convert HTML to PDF
        converter.Convert("without-image.pdf")
    End Sub
End Module
open System
open GroupDocs.Conversion.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")
    
    // Apply license
    License.Set(publicKey, privateKey)

    // Skip external resources through load options
    let converter = new HtmlToPdfConverter("with-image.html", fun options ->
        options.SkipExternalResources <- true
    )
    
    // Convert HTML to PDF
    converter.Convert("without-image.pdf")
    0

Expected output: Download without-image.pdf (External image skipped)

PDF Convert Options

The examples in this section shows how you can adjust the output using PdfConvertOptions.

Convert HTML to PDF with Specific PDF Format

You can specify the PDF format for the output file using the PdfFormat property in PdfOptions class. This allows you to create PDF files that conform to specific standards like PDF/A for archiving or PDF/X for print production.

The following example shows how to convert an HTML file to PDF/A-1b format, which is commonly used for long-term archiving:

using System;
using GroupDocs.Conversion.LowCode;
using GroupDocs.Conversion.Options.Convert;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply license
License.Set(publicKey, privateKey);

// Create the converter
var converter = new HtmlToPdfConverter("sample.html");

// Convert to PDF/A-1b format for archiving
converter.Convert("archived-sample.pdf", convertOptions =>
{
    convertOptions.PdfOptions.PdfFormat = PdfFormats.PdfA_1B;
});
Imports GroupDocs.Conversion.LowCode
Imports GroupDocs.Conversion.Options.Convert

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey As String = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey As String = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply license
        License.Set(publicKey, privateKey)

        ' Create the converter
        Dim converter As New HtmlToPdfConverter("sample.html")

        ' Convert to PDF/A-1b format for archiving
        converter.Convert("archived-sample.pdf", Sub(convertOptions)
            convertOptions.PdfOptions.PdfFormat = PdfFormats.PdfA_1B
        End Sub)
    End Sub
End Module
open System
open GroupDocs.Conversion.LowCode
open GroupDocs.Conversion.Options.Convert

[<EntryPoint>]
let main argv =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

    // Apply license
    License.Set(publicKey, privateKey)

    // Create the converter
    let converter = new HtmlToPdfConverter("sample.html")

    // Convert to PDF/A-1b format for archiving
    converter.Convert("archived-sample.pdf", fun convertOptions ->
        convertOptions.PdfOptions.PdfFormat <- PdfFormats.PdfA_1B
    )

    0 // return exit code

Sample input: Download sample.html

Expected output: Download archived-sample.pdf (PDF/A-1b format)

Convert HTML to Password-Protected PDF

You can protect the output PDF with a password by setting the Password property in PdfConvertOptions class.

using System;
using GroupDocs.Conversion.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply license
License.Set(publicKey, privateKey);

// Create the converter
var converter = new HtmlToPdfConverter("sample.html");

// Convert to password-protected PDF
converter.Convert("protected.pdf", convertOptions =>
{
    convertOptions.Password = "12345";
});
Imports GroupDocs.Conversion.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply license
        License.Set(publicKey, privateKey)

        ' Create the converter
        Dim converter As New HtmlToPdfConverter("sample.html")

        ' Convert to password-protected PDF
        converter.Convert("protected.pdf", Sub(convertOptions)
            convertOptions.Password = "12345"
        End Sub)
    End Sub
End Module
open System
open GroupDocs.Conversion.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

    // Apply license
    License.Set(publicKey, privateKey)

    // Create the converter
    let converter = new HtmlToPdfConverter("sample.html")

    // Convert to password-protected PDF
    converter.Convert("protected.pdf", fun convertOptions ->
        convertOptions.Password <- "12345"
    )

    0

Sample input: Download sample.html

Expected output: Download protected.pdf (Password: 12345)