Some test text!

menu
search

VB HTML to PDF converter

Sample VB code for using PDFTron SDK to directly convert HTML pages to PDF by using 'pdftron.PDF.HTML2PDF'. The HTML2PDF converter supports conversion from a string or URL and offers many options to control page size and formatting. Learn more about our PDF Conversion Library.

Get StartedSamplesDownload

To run this sample, get started with a free trial of PDFTron SDK.

'
' Copyright (c) 2001-2019 by PDFTron Systems 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 http://www.pdftron.com/pdfnet/downloads.html.
'
' 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 = "http://www.gutenberg.org/"
        Dim page0 As String = "wiki/Main_Page"
        Dim page1 As String = "catalog/"
        Dim page2 As String = "browse/recent/last1"
        Dim page3 As String = "wiki/Gutenberg:The_Sheet_Music_Project"

        ' 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()

        ' 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")

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

        Try
            Dim doc As PDFDoc = New PDFDoc()
            If (HTML2PDF.Convert(doc, host + page0)) Then
                doc.Save(output_path + "_01.pdf", SDFDoc.SaveOptions.e_linearized)
            Else
                Console.WriteLine("Conversion failed.")
            End If
        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.SetImageQuality(25)
            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
            If (converter.Convert(doc)) Then
                doc.Save(output_path + "_02.pdf", SDFDoc.SaveOptions.e_linearized)
            Else
                Console.WriteLine("Conversion failed. HTTP Code: {0}" & vbCrLf & "{1}", converter.GetHTTPErrorCode(), converter.GetLog())
            End If
        Catch ex As PDFNetException
            Console.WriteLine(ex.Message)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        '--------------------------------------------------------------------------------
        ' 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. 

        Try
            Dim doc As PDFDoc = New PDFDoc()
            Dim converter As HTML2PDF = New HTML2PDF()

            ' Add a cover page, which is excluded from the outline, and ignore any errors
            Dim cover As HTML2PDF.WebPageSettings = New HTML2PDF.WebPageSettings()
            cover.SetLoadErrorHandling(HTML2PDF.WebPageSettings.ErrorHandling.e_ignore)
            cover.SetIncludeInOutline(False)
            converter.InsertFromURL(host + page3, cover)

            ' Add a table of contents settings (modifying the settings is optional)
            Dim toc As HTML2PDF.TOCSettings = New HTML2PDF.TOCSettings()
            toc.SetDottedLines(False)
            converter.InsertTOC(toc)

            ' Now add the rest of the web pages, disabling external links and 
            ' skipping any web pages that fail to load.
            '
            ' Note that the order of insertion matters, so these will appear
            ' after the cover and table of contents, in the order below.
            Dim settings As HTML2PDF.WebPageSettings = New HTML2PDF.WebPageSettings()
            settings.SetLoadErrorHandling(HTML2PDF.WebPageSettings.ErrorHandling.e_skip)
            settings.SetExternalLinks(False)
            converter.InsertFromURL(host + page0, settings)
            converter.InsertFromURL(host + page1, settings)
            converter.InsertFromURL(host + page2, settings)

            If (converter.Convert(doc)) Then
                doc.Save(output_path + "_03.pdf", SDFDoc.SaveOptions.e_linearized)
            Else
                Console.WriteLine("Conversion failed. HTTP Code: {0}" & vbCrLf & "{1}", converter.GetHTTPErrorCode(), converter.GetLog())
            End If
        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.

            If (converter.Convert(doc)) Then
                doc.Save(output_path + "_04.pdf", SDFDoc.SaveOptions.e_linearized)
            Else
                Console.WriteLine("Conversion failed. HTTP Code: {0}" & vbCrLf & "{1}", converter.GetHTTPErrorCode(), converter.GetLog())
            End If
        Catch ex As PDFNetException
            Console.WriteLine(ex.Message)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

End Module
close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn More
close