> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/core/get-started/samples/html2pdftest.md).

# Convert HTML to PDF with Apryse Server SDK: Sample Code in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go, and VB

Sample code for using Apryse Server SDK to directly convert HTML pages to PDF using 'pdftron.PDF.HTML2PDF'. The HTML2PDF converter supports conversion from a string or URL and offers many options to c

Sample code for using Apryse SDK to directly convert HTML pages to PDF by using 'pdftron.PDF.HTML2PDF', provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB. The HTML2PDF converter supports conversion from a string or URL and offers many options to control page size and formatting.

To use this code, you'll need to

1. [Download and get started with Server SDK](/core/get-started/get-started.md)
2. [Install the HTML2PDF Module](/core/learn-more/modules.md#html2pdf-module)

Learn more about our [Server SDK](/core/get-started/get-started.md) and [PDF Conversion Library](/core/conversion/conversion.md).

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

using System;
using System.IO;

using pdftron;
using pdftron.Common;
using pdftron.SDF;
using pdftron.PDF;

namespace HTML2PDFTestCS
{
	//---------------------------------------------------------------------------------------
	// The following sample illustrates how to convert HTML pages to PDF format using
	// the HTML2PDF class.
	// 
	// 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be 
	// used to convert HTML web pages into PDF documents by using an external module (html2pdf).
	//
	// html2pdf modules can be downloaded from https://docs.apryse.com/core/guides/info/modules#html2pdf-module.
	//
	// Users can convert HTML pages to PDF using the following operations:
	// - Simple one line static method to convert a single web page to PDF. 
	// - Convert HTML pages from URL or string, plus optional table of contents, in user defined order. 
	// - Optionally configure settings for proxy, images, java script, and more for each HTML page. 
	// - Optionally configure the PDF output, including page size, margins, orientation, and more. 
	// - Optionally add table of contents, including setting the depth and appearance.
	//---------------------------------------------------------------------------------------
	class HTML2PDFSample
	{
		private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
		static HTML2PDFSample() {}
		
		static void Main(string[] args)
		{
			string output_path = "../../../../TestFiles/Output/html2pdf_example";
			string host = "https://docs.apryse.com";
			string page0 = "/";
			string page1 = "/all-products/";
			string page2 = "/web/faq";

			// The first step in every application using PDFNet is to initialize the 
			// library and set the path to common PDF resources. The library is usually 
			// initialized only once, but calling Initialize() multiple times is also fine.
			PDFNet.Initialize(PDFTronLicense.Key);
			// For HTML2PDF we need to locate the html2pdf module. If placed with the 
			// PDFNet library, or in the current working directory, it will be loaded
			// automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath().
			HTML2PDF.SetModulePath("../../../../../Lib");
			if (!HTML2PDF.IsModuleAvailable())
			{
				Console.WriteLine();
				Console.WriteLine("Unable to run HTML2PDFTest: Apryse SDK HTML2PDF module not available.");
				Console.WriteLine("---------------------------------------------------------------");
				Console.WriteLine("The HTML2PDF module is an optional add-on, available for download");
				Console.WriteLine("at https://docs.apryse.com/core/guides/info/modules . If you have already downloaded this");
				Console.WriteLine("module, ensure that the SDK is able to find the required files");
				Console.WriteLine("using the HTML2PDF.SetModulePath() function.");
				Console.WriteLine();
				return;
			}
			
			//--------------------------------------------------------------------------------
			// Example 1) Simple conversion of a web page to a PDF doc. 

			try
			{
				using (PDFDoc doc = new PDFDoc())
				{

					// now convert a web page, sending generated PDF pages to doc
					HTML2PDF.Convert(doc, host + page0);
					doc.Save(output_path + "_01.pdf", SDFDoc.SaveOptions.e_linearized);
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
			}

			//--------------------------------------------------------------------------------
			// Example 2) Modify the settings of the generated PDF pages and attach to an
			// existing PDF document. 

			try
			{
				// open the existing PDF, and initialize the security handler
				using (PDFDoc doc = new PDFDoc("../../../../TestFiles/numbered.pdf"))
				{
					doc.InitSecurityHandler();

					// create the HTML2PDF converter object and modify the output of the PDF pages
					HTML2PDF converter = new HTML2PDF();
					converter.SetPaperSize(PrinterMode.PaperSize.e_11x17);

					// insert the web page to convert
					converter.InsertFromURL(host + page0);

					// convert the web page, appending generated PDF pages to doc
					converter.Convert(doc);
					doc.Save(output_path + "_02.pdf", SDFDoc.SaveOptions.e_linearized);
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
			}

			//--------------------------------------------------------------------------------
			// Example 3) Convert multiple web pages

			try
			{
				using (PDFDoc doc = new PDFDoc())
				{
					// convert page 0 into pdf
					HTML2PDF converter = new HTML2PDF();
					string header = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>";
					string footer = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>";
					converter.SetHeader(header);
					converter.SetFooter(footer);
					converter.SetMargins("1cm", "2cm", ".5cm", "1.5cm");

					HTML2PDF.WebPageSettings settings = new HTML2PDF.WebPageSettings();
					settings.SetZoom(0.5);
					converter.InsertFromURL(host + page0, settings);
					converter.Convert(doc);

					// convert page 1 with the same settings, appending generated PDF pages to doc
					converter.InsertFromURL(host + page1, settings);
					converter.Convert(doc);

					// convert page 2 with different settings, appending generated PDF pages to doc
					HTML2PDF another_converter = new HTML2PDF();
					another_converter.SetLandscape(true);
					HTML2PDF.WebPageSettings another_settings = new HTML2PDF.WebPageSettings();
					another_settings.SetPrintBackground(false);
					another_converter.InsertFromURL(host + page2, another_settings);
					another_converter.Convert(doc);

					doc.Save(output_path + "_03.pdf", SDFDoc.SaveOptions.e_linearized);
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
			}

			//--------------------------------------------------------------------------------
			// Example 4) Convert HTML string to PDF. 

			try
			{
				using (PDFDoc doc = new PDFDoc())
				{

					HTML2PDF converter = new HTML2PDF();
				
					// Our HTML data
					string html = "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>";
					
					// Add html data
					converter.InsertFromHtmlString(html);
					// Note, InsertFromHtmlString can be mixed with the other Insert methods.

					converter.Convert(doc);
					doc.Save(output_path + "_04.pdf", SDFDoc.SaveOptions.e_linearized);
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
			}

			//--------------------------------------------------------------------------------
			// Example 5) Set the location of the log file to be used during conversion.

			try
			{
				using (PDFDoc doc = new PDFDoc())
				{
					HTML2PDF converter = new HTML2PDF();
					converter.SetLogFilePath("../../../../TestFiles/Output/html2pdf.log");
					converter.InsertFromURL(host + page0);
					converter.Convert(doc);
					doc.Save(output_path + "_05.pdf", SDFDoc.SaveOptions.e_linearized);
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
			}

			PDFNet.Terminate();
		}
	}
}
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

#include <iostream>
#include <PDF/PDFNet.h>
#include <PDF/PDFDoc.h>
#include <PDF/HTML2PDF.h>
#include "../../LicenseKey/CPP/LicenseKey.h"

using namespace std;
using namespace pdftron;
using namespace Common;
using namespace SDF;
using namespace PDF;

//---------------------------------------------------------------------------------------
// The following sample illustrates how to convert HTML pages to PDF format using
// the HTML2PDF class.
// 
// 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be 
// used to convert HTML web pages into PDF documents by using an external module (html2pdf).
//
// html2pdf modules can be downloaded from https://docs.apryse.com/core/guides/info/modules#html2pdf-module.
//
// Users can convert HTML pages to PDF using the following operations:
// - Simple one line static method to convert a single web page to PDF. 
// - Convert HTML pages from URL or string, plus optional table of contents, in user defined order. 
// - Optionally configure settings for proxy, images, java script, and more for each HTML page. 
// - Optionally configure the PDF output, including page size, margins, orientation, and more. 
// - Optionally add table of contents, including setting the depth and appearance.
//---------------------------------------------------------------------------------------

int main(int argc, char * argv[])
{
	int ret = 0;

	std::string output_path = "../../TestFiles/Output/html2pdf_example";
	std::string host = "https://docs.apryse.com";
	std::string page0 = "/";
	std::string page1 = "/all-products/";
	std::string page2 = "/web/faq";
	
	
	// The first step in every application using PDFNet is to initialize the 
	// library and set the path to common PDF resources. The library is usually 
	// initialized only once, but calling Initialize() multiple times is also fine.
	PDFNet::Initialize(LicenseKey);

	// For HTML2PDF we need to locate the html2pdf module. If placed with the 
	// PDFNet library, or in the current working directory, it will be loaded
	// automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath().
	HTML2PDF::SetModulePath("../../../Lib");
	if (!HTML2PDF::IsModuleAvailable())
	{
		cout << endl;
		cout << "Unable to run HTMLPDFTest: Apryse SDK HTML2PDF module not available." << endl;
		cout << "---------------------------------------------------------------" << endl;
		cout << "The HTML2PDF module is an optional add-on, available for download" << endl;
		cout << "at https://docs.apryse.com/core/guides/info/modules. If you have already downloaded this" << endl;
		cout << "module, ensure that the SDK is able to find the required files" << endl;
		cout << "using the HTML2PDF::SetModulePath() function." << endl << endl;
		return 1;
	}

	//--------------------------------------------------------------------------------
	// Example 1) Simple conversion of a web page to a PDF doc. 
	try
	{
		PDFDoc doc;

		// now convert a web page, sending generated PDF pages to doc
		HTML2PDF::Convert(doc, host + page0);
		doc.Save(output_path + "_01.pdf", SDFDoc::e_linearized, NULL);
	}
	catch (Common::Exception& e)
	{
		cout << e << endl;
		ret = 1;
	}
	catch (...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}

	//--------------------------------------------------------------------------------
	// Example 2) Modify the settings of the generated PDF pages and attach to an
	// existing PDF document. 

	try
	{
		// open the existing PDF, and initialize the security handler
		PDFDoc doc("../../TestFiles/numbered.pdf");
		doc.InitSecurityHandler();

		// create the HTML2PDF converter object and modify the output of the PDF pages
		HTML2PDF converter;
		converter.SetPaperSize(PrinterMode::e_11x17);

		// insert the web page to convert
		converter.InsertFromURL(host + page0);

		// convert the web page, appending generated PDF pages to doc
		converter.Convert(doc);
		doc.Save(output_path + "_02.pdf", SDFDoc::e_linearized, NULL);
	}
	catch (Common::Exception& e)
	{
		cout << e << endl;
		ret = 1;
	}
	catch (...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}


	//--------------------------------------------------------------------------------
	// Example 3) Convert multiple web pages
	try
	{
		// convert page 0 into pdf
		PDFDoc doc;
		HTML2PDF converter;
		UString header("<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>");
		UString footer("<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>");
		converter.SetHeader(header);
		converter.SetFooter(footer);
		converter.SetMargins("1cm", "2cm", ".5cm", "1.5cm");
		HTML2PDF::WebPageSettings settings;
		settings.SetZoom(0.5);
		converter.InsertFromURL(host + page0, settings);
		converter.Convert(doc);

		// convert page 1 with the same settings, appending generated PDF pages to doc
		converter.InsertFromURL(host + page1, settings);
		converter.Convert(doc);
	
		// convert page 2 with different settings, appending generated PDF pages to doc
		HTML2PDF another_converter;
		another_converter.SetLandscape(true);
		HTML2PDF::WebPageSettings another_settings;
		another_settings.SetPrintBackground(false);
		another_converter.InsertFromURL(host + page2, another_settings);
		another_converter.Convert(doc);

		doc.Save(output_path + "_03.pdf", SDFDoc::e_linearized, NULL);
	}
	catch (Common::Exception& e)
	{
		std::cout << e << endl;
		ret = 1;
	}
	catch (...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}

	//--------------------------------------------------------------------------------
	// Example 4) Convert HTML string to PDF. 

	try
	{
		PDFDoc doc;

		HTML2PDF converter;
	
		// Our HTML data
		UString html("<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>");
		
		// Add html data
		converter.InsertFromHtmlString(html);
		// Note, InsertFromHtmlString can be mixed with the other Insert methods.
		
		converter.Convert(doc);
		doc.Save(output_path + "_04.pdf", SDFDoc::e_linearized, NULL);
	}
	catch (Common::Exception& e)
	{
		std::cout << e << endl;
		ret = 1;		
	}
	catch (...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}

	//--------------------------------------------------------------------------------
	// Example 5) Set the location of the log file to be used during conversion. 
	try
	{
		PDFDoc doc;
		HTML2PDF converter;
		converter.SetLogFilePath("../../TestFiles/Output/html2pdf.log");
		converter.InsertFromURL(host + page0);
		converter.Convert(doc);
		doc.Save(output_path + "_05.pdf", SDFDoc::e_linearized, NULL);
	}
	catch (Common::Exception& e)
	{
		cout << e << endl;
		ret = 1;
	}
	catch (...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}

	PDFNet::Terminate();
	return ret;

}
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
// Consult LICENSE.txt regarding license information.
//---------------------------------------------------------------------------------------

package main
import (
	"fmt"
	"strconv"
	. "pdftron"
)

import  "pdftron/Samples/LicenseKey/GO"

//---------------------------------------------------------------------------------------
// The following sample illustrates how to convert HTML pages to PDF format using
// the HTML2PDF class.
// 
// 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be 
// used to convert HTML web pages into PDF documents by using an external module (html2pdf).
//
// html2pdf modules can be downloaded from https://docs.apryse.com/core/guides/info/modules#html2pdf-module.
//
// Users can convert HTML pages to PDF using the following operations:
// - Simple one line static method to convert a single web page to PDF. 
// - Convert HTML pages from URL or string, plus optional table of contents, in user defined order. 
// - Optionally configure settings for proxy, images, java script, and more for each HTML page. 
// - Optionally configure the PDF output, including page size, margins, orientation, and more. 
// - Optionally add table of contents, including setting the depth and appearance.
//---------------------------------------------------------------------------------------

func main(){
    outputPath := "../../TestFiles/Output/html2pdf_example"
    host := "https://www.pdftron.com"
    page0 := "/"
    page1 := "/support"
    page2 := "/blog"
 
    // The first step in every application using PDFNet is to initialize the 
    // library and set the path to common PDF resources. The library is usually 
    // initialized only once, but calling Initialize() multiple times is also fine.
    PDFNetInitialize(PDFTronLicense.Key)
    
    // For HTML2PDF we need to locate the html2pdf module. If placed with the 
    // PDFNet library, or in the current working directory, it will be loaded
    // automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath.
    HTML2PDFSetModulePath("../../../PDFNetC/Lib/")
    if ! HTML2PDFIsModuleAvailable(){
        fmt.Println("Unable to run HTML2PDFTest: PDFTron SDK HTML2PDF module not available.\n" +
        "---------------------------------------------------------------\n" +
        "The HTML2PDF module is an optional add-on, available for download\n" +
        "at https://docs.apryse.com/core/guides/info/modules. If you have already downloaded this\n" +
        "module, ensure that the SDK is able to find the required files\n" +
        "using the HTML2PDF::SetModulePath() function.")
        return
    }
    
    //--------------------------------------------------------------------------------
    // Example 1) Simple conversion of a web page to a PDF doc. 

    doc := NewPDFDoc()
    // now convert a web page, sending generated PDF pages to doc
    converter := NewHTML2PDF()
    converter.InsertFromURL(host + page0)
    if converter.Convert(doc){
        doc.Save(outputPath + "_01.pdf", uint(SDFDocE_linearized))
	}else{
        fmt.Println("Conversion failed.")
    }

    //--------------------------------------------------------------------------------
    // Example 2) Modify the settings of the generated PDF pages and attach to an
    // existing PDF document. 
    
    // open the existing PDF, and initialize the security handler
    doc = NewPDFDoc("../../TestFiles/numbered.pdf")
    doc.InitSecurityHandler()
    
    // create the HTML2PDF converter object and modify the output of the PDF pages
    converter = NewHTML2PDF()
    converter.SetPaperSize(PrinterModeE_11x17)
    
    // insert the web page to convert
    converter.InsertFromURL(host + page0)
    
    // convert the web page, appending generated PDF pages to doc
    if converter.Convert(doc){
        doc.Save(outputPath + "_02.pdf", uint(SDFDocE_linearized))
    }else{
        fmt.Println("Conversion failed. HTTP Code: " + strconv.Itoa(converter.GetHTTPErrorCode()) + "\n" + converter.GetLog())
	}
    //--------------------------------------------------------------------------------
    // Example 3) Convert multiple web pages, adding a table of contents, and setting
    // the first page as a cover page, not to be included with the table of contents outline.
    
    doc = NewPDFDoc()
    converter = NewHTML2PDF()

    header := "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
    footer := "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
    converter.SetHeader(header)
    converter.SetFooter(footer)
    converter.SetMargins("1cm", "2cm", ".5cm", "1.5cm")
    settings := NewWebPageSettings()
    settings.SetZoom(0.5)
    converter.InsertFromURL(host + page0, settings)
    is_conversion_0_successful := converter.Convert(doc)

    // convert page 1 with the same settings, appending generated PDF pages to doc
    converter.InsertFromURL(host + page1, settings)
    is_conversion_1_successful := converter.Convert(doc)

    // convert page 2 with different settings, appending generated PDF pages to doc
    another_converter := NewHTML2PDF()
    another_converter.SetLandscape(true)
    another_settings := NewWebPageSettings()
    another_settings.SetPrintBackground(false)
    another_converter.InsertFromURL(host + page2, another_settings)
    is_conversion_2_successful := another_converter.Convert(doc);

    if(is_conversion_0_successful && is_conversion_1_successful && is_conversion_2_successful){
        doc.Save(outputPath + "_03.pdf", uint(SDFDocE_linearized))
    }else{
        fmt.Println("Conversion failed. HTTP Code: " + strconv.Itoa(converter.GetHTTPErrorCode()) + "\n" + converter.GetLog())
    }

    //--------------------------------------------------------------------------------
    // Example 4) Convert HTML string to PDF. 
    
    doc = NewPDFDoc()
    converter = NewHTML2PDF()
    
    // Our HTML data
    html := "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>"
    
    // Add html data
    converter.InsertFromHtmlString(html)
    // Note, InsertFromHtmlString can be mixed with the other Insert methods.
    
    if converter.Convert(doc){
        doc.Save(outputPath + "_04.pdf", uint(SDFDocE_linearized))
    }else{
        fmt.Println("Conversion failed. HTTP Code: " + strconv.Itoa(converter.GetHTTPErrorCode()) + "\n" + converter.GetLog())
	}
    PDFNetTerminate()
}
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import com.pdftron.pdf.*;
import com.pdftron.sdf.*;

public class HTML2PDFTest {
    //---------------------------------------------------------------------------------------
    // The following sample illustrates how to convert HTML pages to PDF format using
    // the HTML2PDF class.
    //
    // 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be
    // used to convert HTML web pages into PDF documents by using an external module (html2pdf).
    //
    // html2pdf modules can be downloaded from https://docs.apryse.com/core/guides/info/modules#html2pdf-module.
    //
    // Users can convert HTML pages to PDF using the following operations:
    // - Simple one line static method to convert a single web page to PDF.
    // - Convert HTML pages from URL or string, plus optional table of contents, in user defined order.
    // - Optionally configure settings for proxy, images, java script, and more for each HTML page.
    // - Optionally configure the PDF output, including page size, margins, orientation, and more.
    // - Optionally add table of contents, including setting the depth and appearance.
    //---------------------------------------------------------------------------------------

    public static void main(String[] args) {
        String output_path = "../../TestFiles/Output/html2pdf_example";
        String host = "https://docs.apryse.com";
        String page0 = "/";
        String page1 = "/all-products/";
        String page2 = "/web/faq";
        // The first step in every application using PDFNet is to initialize the
        // library and set the path to common PDF resources. The library is usually
        // initialized only once, but calling initialize() multiple times is also fine.
        PDFNet.initialize(PDFTronLicense.Key());
        // For HTML2PDF we need to locate the html2pdf module. If placed with the
        // PDFNet library, or in the current working directory, it will be loaded
        // automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath().
        try {
                HTML2PDF.setModulePath("../../../Lib");
                if(!HTML2PDF.isModuleAvailable())
                {
                    System.out.println();
                    System.out.println("Unable to run HTML2PDFTest: Apryse SDK HTML2PDF module not available.");
                    System.out.println("---------------------------------------------------------------");
                    System.out.println("The HTML2PDF module is an optional add-on, available for download");
                    System.out.println("at https://docs.apryse.com/core/guides/info/modules. If you have already downloaded this");
                    System.out.println("module, ensure that the SDK is able to find the required files");
                    System.out.println("using the HTML2PDF.setModulePath() function." );
                    System.out.println();
                    return;
                }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        //--------------------------------------------------------------------------------
        // Example 1) Simple conversion of a web page to a PDF doc.

        try (PDFDoc doc = new PDFDoc()) {
            // now convert a web page, sending generated PDF pages to doc
            HTML2PDF.convert(doc, host + page0);
            doc.save(output_path + "_01.pdf", SDFDoc.SaveMode.LINEARIZED, null);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        //--------------------------------------------------------------------------------
        // Example 2) Modify the settings of the generated PDF pages and attach to an
        // existing PDF document.

        try (PDFDoc doc = new PDFDoc("../../TestFiles/numbered.pdf")) {
            // open the existing PDF, and initialize the security handler
            doc.initSecurityHandler();

            // create the HTML2PDF converter object and modify the output of the PDF pages
            HTML2PDF converter = new HTML2PDF();
            converter.setPaperSize(PrinterMode.e_11x17);

            // insert the web page to convert
            converter.insertFromURL(host + page0);

            // convert the web page, appending generated PDF pages to doc
            converter.convert(doc);
            doc.save(output_path + "_02.pdf", SDFDoc.SaveMode.LINEARIZED, null);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        //--------------------------------------------------------------------------------
        // Example 3) Convert multiple web pages

        try (PDFDoc doc = new PDFDoc()) {
            // convert page 0 into pdf

            HTML2PDF converter = new HTML2PDF();

            String header = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>";
            String footer = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>";
            converter.setHeader(header);
            converter.setFooter(footer);
            converter.setMargins("1cm", "2cm", ".5cm", "1.5cm");
            HTML2PDF.WebPageSettings settings = new HTML2PDF.WebPageSettings();
            settings.setZoom(0.5);
            converter.insertFromURL(host + page0, settings);
            converter.convert(doc);

            // convert page 1 with the same settings, appending generated PDF pages to doc
            converter.insertFromURL(host + page1, settings);
            converter.convert(doc);

            // convert page 2 with different settings, appending generated PDF pages to doc
            HTML2PDF another_converter = new HTML2PDF();;
            another_converter.setLandscape(true);
            HTML2PDF.WebPageSettings another_settings = new HTML2PDF.WebPageSettings();
            another_settings.setPrintBackground(false);
            another_converter.insertFromURL(host + page2, another_settings);
            another_converter.convert(doc);

            doc.save(output_path + "_03.pdf", SDFDoc.SaveMode.LINEARIZED, null);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        //--------------------------------------------------------------------------------
        // Example 4) Convert HTML string to PDF.

        try (PDFDoc doc = new PDFDoc()) {
            HTML2PDF converter = new HTML2PDF();

            // Our HTML data
            String html = "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>";

            // Add html data
            converter.insertFromHtmlString(html);
            // Note, InsertFromHtmlString can be mixed with the other Insert methods.

            converter.convert(doc);
            doc.save(output_path + "_04.pdf", SDFDoc.SaveMode.LINEARIZED, null);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        //--------------------------------------------------------------------------------
        // Example 5) Set the location of the log file to be used during conversion.

        try (PDFDoc doc = new PDFDoc()) {
            HTML2PDF converter = new HTML2PDF();
            converter.setLogFilePath("../../TestFiles/Output/html2pdf.log");
            converter.insertFromURL(host + page0);
            converter.convert(doc);
            doc.save(output_path + "_05.pdf", SDFDoc.SaveMode.LINEARIZED, null);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        PDFNet.terminate();
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------
// The following sample illustrates how to convert HTML pages to PDF format using
// the HTML2PDF class.
// 
// 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be 
// used to convert HTML web pages into PDF documents by using an external module (html2pdf).
//
// html2pdf modules can be downloaded from https://docs.apryse.com/core/guides/info/modules#html2pdf-module.
//
// Users can convert HTML pages to PDF using the following operations:
// - Simple one line static method to convert a single web page to PDF. 
// - Convert HTML pages from URL or string, plus optional table of contents, in user defined order. 
// - Optionally configure settings for proxy, images, java script, and more for each HTML page. 
// - Optionally configure the PDF output, including page size, margins, orientation, and more. 
// - Optionally add table of contents, including setting the depth and appearance.
//---------------------------------------------------------------------------------------

const { PDFNet } = require('@pdftron/pdfnet-node');
const PDFTronLicense = require('../LicenseKey/LicenseKey');

((exports) => {
  'use strict';

  exports.runHTML2PDFTest = () => {
    const main = async () => {
      const outputPath = '../TestFiles/Output/html2pdf_example';
      const host = 'https://docs.apryse.com';
      const page0 = '/';
      const page1 = '/all-products/';
      const page2 = '/web/faq';

      // For HTML2PDF we need to locate the html2pdf module. If placed with the 
      // PDFNet library, or in the current working directory, it will be loaded
      // automatically. Otherwise, it must be set manually using HTML2PDF.setModulePath.
      await PDFNet.HTML2PDF.setModulePath('../../lib/');

      if(!(await PDFNet.HTML2PDF.isModuleAvailable())) {
        console.log('Unable to run HTML2PDFTest: Apryse SDK HTML2PDF module not available.');
        console.log('---------------------------------------------------------------');
        console.log('The HTML2PDF module is an optional add-on, available for download');
        console.log('at https://docs.apryse.com/core/guides/info/modules. If you have already downloaded this');
        console.log('module, ensure that the SDK is able to find the required files');
        console.log('using the HTML2PDF.setModulePath() function.');

        return;
      }

      //--------------------------------------------------------------------------------
      // Example 1) Simple conversion of a web page to a PDF doc. 

      try {
        const html2pdf = await PDFNet.HTML2PDF.create();
        const doc = await PDFNet.PDFDoc.create();

        html2pdf.insertFromUrl(host.concat(page0));
        // now convert a web page, sending generated PDF pages to doc
        await html2pdf.convert(doc);
        doc.save(outputPath.concat('_01.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
      } catch (err) {
        console.log(err);
      }

      //--------------------------------------------------------------------------------
      // Example 2) Modify the settings of the generated PDF pages and attach to an
      // existing PDF document. 

      try {
        // open the existing PDF, and initialize the security handler
        const doc = await PDFNet.PDFDoc.createFromFilePath('../TestFiles/numbered.pdf');
        await doc.initSecurityHandler();

        // create the HTML2PDF converter object and modify the output of the PDF pages
        const html2pdf = await PDFNet.HTML2PDF.create();
        html2pdf.setPaperSize(PDFNet.PrinterMode.PaperSize.e_11x17);

        // insert the web page to convert
        html2pdf.insertFromUrl(host.concat(page0));

        // convert the web page, appending generated PDF pages to doc
        await html2pdf.convert(doc);
        doc.save(outputPath.concat('_02.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
      } catch (err) {
        console.log(err);
      }

      //--------------------------------------------------------------------------------
      // Example 3) Convert multiple web pages

      try {
        // convert page 0 into pdf
        const doc = await PDFNet.PDFDoc.create();

        const converter = await PDFNet.HTML2PDF.create();

        const header = '<div style=\'width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF\'><span class=\'date\'></span></div><div style=\'width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF\'><span>PDFTRON HEADER EXAMPLE</span></div><div style=\'width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF\'><span class=\'pageNumber\'></span> of <span class=\'totalPages\'></span></div>';
        const footer = '<div style=\'width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF\'><span class=\'date\'></span></div><div style=\'width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF\'><span>PDFTRON FOOTER EXAMPLE</span></div><div style=\'width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF\'><span class=\'pageNumber\'></span> of <span class=\'totalPages\'></span></div>';
        converter.setHeader(header);
        converter.setFooter(footer);
        converter.setMargins('1cm', '2cm', '.5cm', '1.5cm');
        const settings = await PDFNet.HTML2PDF.WebPageSettings.create();
        await settings.setZoom(0.5);
        converter.insertFromUrl2(host.concat(page0), settings);
        await converter.convert(doc);

        // convert page 1 with the same settings, appending generated PDF pages to doc
        converter.insertFromUrl2(host.concat(page1), settings);
        await converter.convert(doc);

        // convert page 2 with different settings, appending generated PDF pages to doc
        const another_converter = await PDFNet.HTML2PDF.create();
        another_converter.setLandscape(true);
        const another_settings =  await PDFNet.HTML2PDF.WebPageSettings.create();
        another_settings.setPrintBackground(false);
        another_converter.insertFromUrl2(host.concat(page2), another_settings);
        await another_converter.convert(doc);

        doc.save(outputPath.concat('_03.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
      } catch (err) {
        console.log(err);
      }

      //--------------------------------------------------------------------------------
      // Example 4) Convert HTML string to PDF. 

      try {
        const html2pdf = await PDFNet.HTML2PDF.create();
        const doc = await PDFNet.PDFDoc.create();
        const html = '<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>';

        html2pdf.insertFromHtmlString(html);
        await html2pdf.convert(doc);
        doc.save(outputPath.concat('_04.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
      } catch (err) {
        console.log(err);
      }

      //--------------------------------------------------------------------------------
      // Example 5) Set the location of the log file to be used during conversion. 

      try {
        const html2pdf = await PDFNet.HTML2PDF.create();
        const doc = await PDFNet.PDFDoc.create();
        html2pdf.setLogFilePath('../TestFiles/Output/html2pdf.log');
        html2pdf.insertFromUrl(host.concat(page0));
        await html2pdf.convert(doc);
        doc.save(outputPath.concat('_05.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
      } catch (err) {
        console.log(err);
      }

      console.log('Test Complete!');
    }
    PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error) {
      console.log('Error: ' + JSON.stringify(error));
    }).then(function(){ return PDFNet.shutdown(); });
  };
  exports.runHTML2PDFTest();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=HTML2PDFTest.js
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
<?php
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
// Consult LICENSE.txt regarding license information.
//---------------------------------------------------------------------------------------
if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
include("../../../PDFNetC/Lib/PDFNetPHP.php");
include("../../LicenseKey/PHP/LicenseKey.php");

// Relative path to the folder containing the test files.
$input_path = getcwd()."/../../TestFiles/";
$output_path = $input_path."Output/";

//---------------------------------------------------------------------------------------
// The following sample illustrates how to convert HTML pages to PDF format using
// the HTML2PDF class.
// 
// 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be 
// used to convert HTML web pages into PDF documents by using an external module (html2pdf).
//
// html2pdf modules can be downloaded from https://dev.apryse.com/.
//
// Users can convert HTML pages to PDF using the following operations:
// - Simple one line static method to convert a single web page to PDF. 
// - Convert HTML pages from URL or string, plus optional table of contents, in user defined order. 
// - Optionally configure settings for proxy, images, java script, and more for each HTML page. 
// - Optionally configure the PDF output, including page size, margins, orientation, and more. 
// - Optionally add table of contents, including setting the depth and appearance.
//---------------------------------------------------------------------------------------

	$output_path = "../../TestFiles/Output/html2pdf_example";
	$host = "https://docs.apryse.com";
	$page0 = "/";
	$page1 = "/all-products/";
	$page2 = "/web/faq";

	// The first step in every application using PDFNet is to initialize the 
	// library and set the path to common PDF resources. The library is usually 
	// initialized only once, but calling Initialize() multiple times is also fine.
	PDFNet::Initialize($LicenseKey);
	PDFNet::GetSystemFontList();    // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.

	// For HTML2PDF we need to locate the html2pdf module. If placed with the 
	// PDFNet library, or in the current working directory, it will be loaded
	// automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath.
	HTML2PDF::SetModulePath("./../../../PDFNetC/Lib");
	if(!HTML2PDF::IsModuleAvailable()) {
		echo "Unable to run HTML2PDFTest: PDFTron SDK HTML2PDF module not available.\n
			---------------------------------------------------------------\n
			The HTML2PDF module is an optional add-on, available for download\n
			at https://www.pdftron.com/. If you have already downloaded this\n
			module, ensure that the SDK is able to find the required files\n
			using the HTML2PDF::SetModulePath() function.\n";
		return;
	}

	//--------------------------------------------------------------------------------
	// Example 1) Simple conversion of a web page to a PDF doc. 

	$doc = new PDFDoc();

	// now convert a web page, sending generated PDF pages to doc
	$converter = new HTML2PDF();
	$converter->InsertFromURL($host.$page0);
	$converter->Convert($doc);
	$doc->Save($output_path."_01.pdf", SDFDoc::e_linearized);
	$doc->Close();

	//--------------------------------------------------------------------------------
	// Example 2) Modify the settings of the generated PDF pages and attach to an
	// existing PDF document. 

	// open the existing PDF, and initialize the security handler
	$doc = new PDFDoc("../../TestFiles/numbered.pdf");
	$doc->InitSecurityHandler();

	// create the HTML2PDF converter object and modify the output of the PDF pages
	$converter = new HTML2PDF();
	$converter->SetPaperSize(PrinterMode::e_11x17);

	// insert the web page to convert
	$converter->InsertFromURL($host.$page0);

	// convert the web page, appending generated PDF pages to doc
	$converter->Convert($doc);
	$doc->Save($output_path."_02.pdf", SDFDoc::e_linearized);
	$doc->Close();

	//--------------------------------------------------------------------------------
	// Example 3) Convert multiple web pages, adding a table of contents, and setting
	// the first page as a cover page, not to be included with the table of contents outline. 

	$doc = new PDFDoc();

	$converter = new HTML2PDF();
	$header = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>";
	$footer = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>";
	$converter->SetHeader($header);
	$converter->SetFooter($footer);
	$converter->SetMargins("1cm", "2cm", ".5cm", "1.5cm");
    
	$settings = new WebPageSettings();
	$settings->SetZoom(0.5);
	$converter->InsertFromURL($host.$page0, $settings);
	$converter->Convert($doc);

	//convert page 1 with the same settings, appending generated PDF pages to doc
	$converter->InsertFromURL($host.$page1, $settings);
	$converter->Convert($doc);
	
	//convert page 2 with different settings, appending generated PDF pages to doc
	$another_converter = new HTML2PDF();
	$another_converter->SetLandscape(True);
	$another_settings = new WebPageSettings();
	$another_settings->SetPrintBackground(False);
	$another_converter->InsertFromURL($host.$page2, $another_settings);
	$another_converter->Convert($doc);
    
	$doc->Save($output_path."_03.pdf", SDFDoc::e_linearized);
	$doc->Close();
	
	//--------------------------------------------------------------------------------
	// Example 4) Convert HTML string to PDF. 

	$doc = new PDFDoc();

	$converter = new HTML2PDF();
	
	// Our HTML data
	$html = "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>";
		
	// Add html data
	$converter->InsertFromHtmlString($html);
	// Note, InsertFromHtmlString can be mixed with the other Insert methods.
	
	$converter->Convert($doc);
	$doc->Save($output_path."_04.pdf", SDFDoc::e_linearized);
	$doc->Close();

	//--------------------------------------------------------------------------------
	// Example 5) Set the location of the log file to be used during conversion. 

	$doc = new PDFDoc();

	// now convert a web page, sending generated PDF pages to doc
	$converter = new HTML2PDF();
	$converter->SetLogFilePath("../../TestFiles/Output/html2pdf.log");
	$converter->InsertFromURL($host.$page0);
	$converter->Convert($doc);
	$doc->Save($output_path."_05.pdf", SDFDoc::e_linearized);
	$doc->Close();

	PDFNet::Terminate();
?>
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
#---------------------------------------------------------------------------------------
# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
# Consult LICENSE.txt regarding license information.
#---------------------------------------------------------------------------------------

import site
site.addsitedir("../../../PDFNetC/Lib")
import sys
from PDFNetPython import *

sys.path.append("../../LicenseKey/PYTHON")
from LicenseKey import *

#---------------------------------------------------------------------------------------
# The following sample illustrates how to convert HTML pages to PDF format using
# the HTML2PDF class.
# 
# 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be 
# used to convert HTML web pages into PDF documents by using an external module (html2pdf).
#
# html2pdf modules can be downloaded from https://dev.apryse.com/.
#
# Users can convert HTML pages to PDF using the following operations:
# - Simple one line static method to convert a single web page to PDF. 
# - Convert HTML pages from URL or string, plus optional table of contents, in user defined order. 
# - Optionally configure settings for proxy, images, java script, and more for each HTML page. 
# - Optionally configure the PDF output, including page size, margins, orientation, and more. 
# - Optionally add table of contents, including setting the depth and appearance.
#---------------------------------------------------------------------------------------

def main():
    output_path = "../../TestFiles/Output/html2pdf_example"
    host = "https://docs.apryse.com"
    page0 = "/"
    page1 = "/all-products/"
    page2 = "/web/faq"

    # The first step in every application using PDFNet is to initialize the 
    # library and set the path to common PDF resources. The library is usually 
    # initialized only once, but calling Initialize() multiple times is also fine.
    PDFNet.Initialize(LicenseKey)
    
    # For HTML2PDF we need to locate the html2pdf module. If placed with the 
    # PDFNet library, or in the current working directory, it will be loaded
    # automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath.
    HTML2PDF.SetModulePath("../../../PDFNetC/Lib/")
    if not HTML2PDF.IsModuleAvailable():
        print("""
        Unable to run HTML2PDFTest: PDFTron SDK HTML2PDF module not available.
        ---------------------------------------------------------------
        The HTML2PDF module is an optional add-on, available for download
        at https://www.pdftron.com/. If you have already downloaded this
        module, ensure that the SDK is able to find the required files
        using the HTML2PDF.SetModulePath() function.""")
        return

    #--------------------------------------------------------------------------------
    # Example 1) Simple conversion of a web page to a PDF doc. 

    doc = PDFDoc()
    # now convert a web page, sending generated PDF pages to doc
    converter = HTML2PDF()
    converter.InsertFromURL(host + page0)
    converter.Convert(doc)
    doc.Save(output_path + "_01.pdf", SDFDoc.e_linearized)

    #--------------------------------------------------------------------------------
    # Example 2) Modify the settings of the generated PDF pages and attach to an
    # existing PDF document. 
    
    # open the existing PDF, and initialize the security handler
    doc = PDFDoc("../../TestFiles/numbered.pdf")
    doc.InitSecurityHandler()
    
    # create the HTML2PDF converter object and modify the output of the PDF pages
    converter = HTML2PDF()
    converter.SetPaperSize(PrinterMode.e_11x17)
    
    # insert the web page to convert
    converter.InsertFromURL(host + page0)
    
    # convert the web page, appending generated PDF pages to doc
    converter.Convert(doc)
    doc.Save(output_path + "_02.pdf", SDFDoc.e_linearized)
    #--------------------------------------------------------------------------------
    # Example 3) Convert multiple web pages
    
    doc = PDFDoc()
    converter = HTML2PDF()
    
    header = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
    footer = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
    converter.SetHeader(header)
    converter.SetFooter(footer)
    converter.SetMargins("1cm", "2cm", ".5cm", "1.5cm")
    
    settings = WebPageSettings()
    settings.SetZoom(0.5)
    converter.InsertFromURL(host + page0)
    converter.Convert(doc)

    # convert page 1 with the same settings, appending generated PDF pages to doc
    converter.InsertFromURL(host + page1, settings)
    converter.Convert(doc)

    # convert page 2 with different settings, appending generated PDF pages to doc
    another_converter = HTML2PDF()
    another_converter.SetLandscape(True)
    another_settings = WebPageSettings()
    another_settings.SetPrintBackground(False)
    another_converter.InsertFromURL(host + page2, another_settings)
    another_converter.Convert(doc)
    
    doc.Save(output_path + "_03.pdf", SDFDoc.e_linearized)
    
    #--------------------------------------------------------------------------------
    # Example 4) Convert HTML string to PDF. 
    
    doc = PDFDoc()
    converter = HTML2PDF()
    
    # Our HTML data
    html = "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>"
    
    # Add html data
    converter.InsertFromHtmlString(html)
    # Note, InsertFromHtmlString can be mixed with the other Insert methods.
    
    converter.Convert(doc)
    doc.Save(output_path + "_04.pdf", SDFDoc.e_linearized)

    #--------------------------------------------------------------------------------
    # Example 5) Set the location of the log file to be used during conversion. 

    doc = PDFDoc()
    # now convert a web page, sending generated PDF pages to doc
    converter = HTML2PDF()
    converter.SetLogFilePath("../../TestFiles/Output/html2pdf.log")
    converter.InsertFromURL(host + page0)
    converter.Convert(doc)
    doc.Save(output_path + "_05.pdf", SDFDoc.e_linearized)

    PDFNet.Terminate()
        
if __name__ == '__main__':
    main()
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
#---------------------------------------------------------------------------------------
# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
# Consult LICENSE.txt regarding license information.
#---------------------------------------------------------------------------------------

require '../../../PDFNetC/Lib/PDFNetRuby'
include PDFNetRuby
require '../../LicenseKey/RUBY/LicenseKey'

$stdout.sync = true

#---------------------------------------------------------------------------------------
# The following sample illustrates how to convert HTML pages to PDF format using
# the HTML2PDF class.
# 
# 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be 
# used to convert HTML web pages into PDF documents by using an external module (html2pdf).
#
# html2pdf modules can be downloaded from http:https://docs.apryse.com/core/guides/info/modules#html2pdf-module.
#
# Users can convert HTML pages to PDF using the following operations:
# - Simple one line static method to convert a single web page to PDF. 
# - Convert HTML pages from URL or string, plus optional table of contents, in user defined order. 
# - Optionally configure settings for proxy, images, java script, and more for each HTML page. 
# - Optionally configure the PDF output, including page size, margins, orientation, and more. 
# - Optionally add table of contents, including setting the depth and appearance.
#---------------------------------------------------------------------------------------

	output_path = "../../TestFiles/Output/html2pdf_example"
	host = "https://docs.apryse.com"
	page0 = "/"
	page1 = "/all-products/"
	page2 = "/web/faq"
	
	# The first step in every application using PDFNet is to initialize the 
	# library and set the path to common PDF resources. The library is usually 
	# initialized only once, but calling Initialize() multiple times is also fine.
	PDFNet.Initialize(PDFTronLicense.Key)
	
	# For HTML2PDF we need to locate the html2pdf module. If placed with the 
	# PDFNet library, or in the current working directory, it will be loaded
	# automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath.
	HTML2PDF.SetModulePath("../../../PDFNetC/Lib/");
	if !HTML2PDF.IsModuleAvailable
		puts 'Unable to run HTML2PDFTest: PDFTron SDK HTML2PDF module not available.'
		puts '---------------------------------------------------------------'
		puts 'The HTML2PDF module is an optional add-on, available for download'
		puts 'at https://dev.apryse.com/. If you have already downloaded this'
		puts 'module, ensure that the SDK is able to find the required files'
		puts 'using the HTML2PDF.SetModulePath function.'
		return 
	end

	#--------------------------------------------------------------------------------
	# Example 1) Simple conversion of a web page to a PDF doc. 
	
	doc = PDFDoc.new()
	# now convert a web page, sending generated PDF pages to doc
	converter = HTML2PDF.new()
	converter.InsertFromURL(host + page0)
	converter.Convert(doc)
	doc.Save(output_path + "_01.pdf", SDFDoc::E_linearized)
	
	#--------------------------------------------------------------------------------
	# Example 2) Modify the settings of the generated PDF pages and attach to an
	# existing PDF document. 
	
	# open the existing PDF, and initialize the security handler
	doc = PDFDoc.new("../../TestFiles/numbered.pdf")
	doc.InitSecurityHandler()
	
	# create the HTML2PDF converter object and modify the output of the PDF pages
	converter = HTML2PDF.new()
	converter.SetPaperSize(PrinterMode::E_11x17)
	
	# insert the web page to convert
	converter.InsertFromURL(host + page0)
	
	# convert the web page, appending generated PDF pages to doc
	converter.Convert(doc)
	doc.Save(output_path + "_02.pdf", SDFDoc::E_linearized)
	
	#--------------------------------------------------------------------------------
	# Example 3) Convert multiple web pages
	
	doc = PDFDoc.new()
	converter = HTML2PDF.new()

	header = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
	footer = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
	converter.SetHeader(header)
	converter.SetFooter(footer)
	converter.SetMargins("1cm", "2cm", ".5cm", "1.5cm")
    
	settings = WebPageSettings.new()
	settings.SetZoom(0.5)
	converter.InsertFromURL(host + page0, settings)
	converter.Convert(doc)

	# convert page 1 with the same settings, appending generated PDF pages to doc
	converter.InsertFromURL(host + page1, settings)
	converter.Convert(doc)
	
	# convert page 2 with different settings, appending generated PDF pages to doc
	another_converter = HTML2PDF.new()
	another_converter.SetLandscape(true)
	another_settings = WebPageSettings.new()
	another_settings.SetPrintBackground(false)
	another_converter.InsertFromURL(host + page2, another_settings)
	another_converter.Convert(doc)
    
	doc.Save(output_path + "_03.pdf", SDFDoc::E_linearized)
		
	#--------------------------------------------------------------------------------
	# Example 4) Convert HTML string to PDF. 
	
	doc = PDFDoc.new()
	converter = HTML2PDF.new()
	
	# Our HTML data
	html = "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>"
	
	# Add html data
	converter.InsertFromHtmlString(html)
	# Note, InsertFromHtmlString can be mixed with the other Insert methods.
	
	converter.Convert(doc)
	doc.Save(output_path + "_04.pdf", SDFDoc::E_linearized)

	#--------------------------------------------------------------------------------
	# Example 5) Set the location of the log file to be used during conversion.
	
	doc = PDFDoc.new()
	converter = HTML2PDF.new()

	# specify the log file name
	converter.SetLogFilePath('../../TestFiles/Output/html2pdf.log')
	
	# insert the web page to convert
	converter.InsertFromURL(host + page0)
	
	# convert the web page
	converter.Convert(doc)
	doc.Save(output_path + "_05.pdf", SDFDoc::E_linearized)

	PDFNet.Terminate
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
'
' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
'

Imports System

Imports pdftron
Imports pdftron.Common
Imports pdftron.Filters
Imports pdftron.SDF
Imports pdftron.PDF
'---------------------------------------------------------------------------------------
' The following sample illustrates how to convert HTML pages to PDF format using
' the HTML2PDF class.
' 
' 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be 
' used to convert HTML web pages into PDF documents by using an external module (html2pdf).
'
' html2pdf modules can be downloaded from https://docs.apryse.com/core/guides/info/modules#html2pdf-module.
'
' Users can convert HTML pages to PDF using the following operations:
' - Simple one line static method to convert a single web page to PDF. 
' - Convert HTML pages from URL or string, plus optional table of contents, in user defined order. 
' - Optionally configure settings for proxy, images, java script, and more for each HTML page. 
' - Optionally configure the PDF output, including page size, margins, orientation, and more. 
' - Optionally add table of contents, including setting the depth and appearance.
'---------------------------------------------------------------------------------------
Module HTML2PDFTestVB
    Dim pdfNetLoader As PDFNetLoader
    Sub New()
        pdfNetLoader = pdftron.PDFNetLoader.Instance()
    End Sub

    Sub Main()
        Dim output_path As String = "../../../../TestFiles/Output/html2pdf_example"
        Dim host As String = "https://docs.apryse.com"
        Dim page0 As String = "/"
        Dim page1 As String = "/all-products/"
        Dim page2 As String =  "/web/faq"
        ' The first step in every application using PDFNet is to initialize the 
        ' library and set the path to common PDF resources. The library is usually 
        ' initialized only once, but calling Initialize() multiple times is also fine.

        PDFNet.Initialize(PDFTronLicense.Key)

        ' For HTML2PDF we need to locate the html2pdf module. If placed with the 
        ' PDFNet library, or in the current working directory, it will be loaded
        ' automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath().
        HTML2PDF.SetModulePath("../../../../../Lib")

        If Not HTML2PDF.IsModuleAvailable() Then
            Console.WriteLine()
            Console.WriteLine("Unable to run HTML2PDFTest: Apryse SDK CAD module not available.")
            Console.WriteLine("---------------------------------------------------------------")
            Console.WriteLine("The HTML2PDF module is an optional add-on, available for download")
            Console.WriteLine("at https://docs.apryse.com/core/guides/info/modules. If you have already downloaded this")
            Console.WriteLine("module, ensure that the SDK is able to find the required files")
            Console.WriteLine("using the HTML2PDF.SetModulePath() function.")
            Console.WriteLine()
        End If
        '--------------------------------------------------------------------------------
        ' Example 1) Simple conversion of a web page to a PDF doc. 

        Try
            Dim doc As PDFDoc = New PDFDoc()
            HTML2PDF.Convert(doc, host & page0)
            doc.Save(output_path + "_01.pdf", SDFDoc.SaveOptions.e_linearized)
        Catch ex As PDFNetException
            Console.WriteLine(ex.Message)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        '--------------------------------------------------------------------------------
        ' Example 2) Modify the settings of the generated PDF pages and attach to an
        ' existing PDF document. 

        Try
            ' open the existing PDF, and initialize the security handler
            Dim doc As PDFDoc = New PDFDoc("../../../../TestFiles/numbered.pdf")
            doc.InitSecurityHandler()

            ' create the HTML2PDF converter object and modify the output of the PDF pages
            Dim converter As HTML2PDF = New HTML2PDF()
            converter.SetPaperSize(PrinterMode.PaperSize.e_11x17)

            ' insert the web page to convert
            converter.InsertFromURL(host & page0)

            ' convert the web page, appending generated PDF pages to doc
            converter.Convert(doc)
            doc.Save(output_path + "_02.pdf", SDFDoc.SaveOptions.e_linearized)
        Catch ex As PDFNetException
            Console.WriteLine(ex.Message)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        '--------------------------------------------------------------------------------
        ' Example 3) Convert multiple web pages

        Try
            Dim doc As PDFDoc = New PDFDoc()
            ' convert page 0 into pdf
            Dim converter As HTML2PDF = New HTML2PDF()
            Dim header As String = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
            Dim footer As String = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
            converter.SetHeader(header)
            converter.SetFooter(footer)
            converter.SetMargins("1cm", "2cm", ".5cm", "1.5cm")
            Dim settings As HTML2PDF.WebPageSettings = New HTML2PDF.WebPageSettings()
            settings.SetZoom(0.5)
            converter.InsertFromURL(host & page0, settings)
            converter.Convert(doc)

            ' convert page 1 with the same settings, appending generated PDF pages to doc
            converter.InsertFromURL(host & page1, settings)
            converter.Convert(doc)

            ' convert page 2 with different settings, appending generated PDF pages to doc
            Dim another_converter As HTML2PDF = New HTML2PDF()
            another_converter.SetLandscape(True)
            Dim another_settings As HTML2PDF.WebPageSettings = New HTML2PDF.WebPageSettings()
            another_settings.SetPrintBackground(False)
            another_converter.InsertFromURL(host & page2, another_settings)
            another_converter.Convert(doc)

            doc.Save(output_path + "_03.pdf", SDFDoc.SaveOptions.e_linearized)
        Catch ex As PDFNetException
            Console.WriteLine(ex.Message)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        '--------------------------------------------------------------------------------
        ' Example 4) Convert HTML string to PDF. 

        Try
            Dim doc As PDFDoc = New PDFDoc()

            Dim converter As HTML2PDF = New HTML2PDF()

            ' Our HTML data
            Dim html As String = "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>"

            ' Add html data
            converter.InsertFromHtmlString(html)
            ' Note, InsertFromHtmlString can be mixed with the other Insert methods.

            converter.Convert(doc)
            doc.Save(output_path + "_04.pdf", SDFDoc.SaveOptions.e_linearized)
        Catch ex As PDFNetException
            Console.WriteLine(ex.Message)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        '--------------------------------------------------------------------------------
        ' Example 5) Set the location of the log file to be used during conversion. 

        Try
            Dim doc As PDFDoc = New PDFDoc()
            Dim converter As HTML2PDF = New HTML2PDF()
            converter.SetLogFilePath("../../../../TestFiles/Output/html2pdf.log")
            converter.InsertFromURL(host & page0)
            converter.Convert(doc)
            doc.Save(output_path + "_05.pdf", SDFDoc.SaveOptions.e_linearized)
        Catch ex As PDFNetException
            Console.WriteLine(ex.Message)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        PDFNet.Terminate()
    End Sub

End Module
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/core/get-started/samples/html2pdftest.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
