In This Topic
Programming / HTML / Converting an HTML page to PDF

Converting an HTML page to PDF

In This Topic

Prerequesties

A Google Chrome or Edge browser is required to support HTML content as input into the whole toolkit scope.

If Chrome or Edge is already installed on your machine, no action on your part is necessary.

If it’s not the case, you have two options to make it work with Chrome:

- 1. Install Google Chrome Portable in the installation folder. You can download Google Chrome Portable here.

- 2. Set the path of the chrome.exe portable version with the SetWebBrowserPath() method.

 

Converting an HTML page, from an URI, to a PDF document.

 

Copy Code
'We assume that GdPicture has been correctly installed And unlocked.
'Check whether a Chrome environment Is already set up.
If GdPictureDocumentUtilities.IsWebBrowserAvailable() = False Then
    'Set the custom path of the Chrome Portable version.
    GdPictureDocumentUtilities.SetWebBrowserPath("D:\chrome-portable\chrome.exe")
End If
'Set the browser pool size (Not mandatory)
GdPictureDocumentUtilities.SetWebBrowserPoolSize(8)
'Convert And save a web page to a PDF file with custom parameters.
Dim oDocumentConverter As New GdPictureDocumentConverter
'Load the HTML document from the Uri.
Dim status As GdPictureStatus = oDocumentConverter.LoadFromHttp(New Uri("https://www.docuvieware.com/"), GdPicture14.DocumentFormat.DocumentFormatHTML)
'Set the parameters of the conversion to PDF.
If status = GdPictureStatus.OK Then
    oDocumentConverter.HtmlPreferCSSPageSize = False
    oDocumentConverter.HtmlPageHeight = 1028
    oDocumentConverter.HtmlPageMarginBottom = 15
    oDocumentConverter.HtmlPageMarginLeft = 15
    oDocumentConverter.HtmlPageMarginRight = 15
    oDocumentConverter.HtmlPageMarginTop = 15
    oDocumentConverter.HtmlPageWidth = 768
    'Save the HTML file as PDF.
    status = oDocumentConverter.SaveAsPDF("D:\myPdf.pdf", PdfConformance.PDF1_7)
    If status = GdPictureStatus.OK Then
        MessageBox.Show("The document has been saved successfully!")
    Else
        MessageBox.Show("Error: " + status.ToString())
    End If
Else
    MessageBox.Show("Error: " + status.ToString())
End If
oDocumentConverter.Dispose()
Copy Code
//We assume that GdPicture has been correctly installed and unlocked.
//Check whether a Chrome environment is already set up.
if (!GdPictureDocumentUtilities.IsWebBrowserAvailable())
{
    //Set the custom path of the Chrome Portable version.
    GdPictureDocumentUtilities.SetWebBrowserPath(@"D:\chrome-portable\chrome.exe");
}
//Set the browser pool size (not mandatory)
GdPictureDocumentUtilities.SetWebBrowserPoolSize(8);
//Convert and save a web page to a PDF file with custom parameters.
GdPictureDocumentConverter oDocumentConverter = new GdPictureDocumentConverter();
//Load the HTML document from the Uri.
GdPictureStatus status = oDocumentConverter.LoadFromHttp(new Uri("https://www.docuvieware.com/"), GdPicture14.DocumentFormat.DocumentFormatHTML);
//Set the parameters of the conversion to PDF.
if (status == GdPictureStatus.OK)
    {
        oDocumentConverter.HtmlPreferCSSPageSize = false;
        oDocumentConverter.HtmlPageHeight = 1028;
        oDocumentConverter.HtmlPageMarginBottom = 15;
        oDocumentConverter.HtmlPageMarginLeft = 15;
        oDocumentConverter.HtmlPageMarginRight = 15;
        oDocumentConverter.HtmlPageMarginTop = 15;
        oDocumentConverter.HtmlPageWidth = 768;
        //Save the HTML file as PDF.
        status = oDocumentConverter.SaveAsPDF(@"D:\myPdf.pdf", PdfConformance.PDF1_7);
        if (status == GdPictureStatus.OK)
        {
            MessageBox.Show("The document has been saved successfully!");
        }
        else
        {
            MessageBox.Show("Error: " + status.ToString());
        }
    }
else
{
    MessageBox.Show("Error: " + status.ToString());
}
oDocumentConverter.Dispose();

 

The ClearWebBrowserCache method must be called before the application exits to clear temporary data if browser pooling is enabled. 

Copy Code
GdPictureDocumentUtilities.ClearWebBrowserCache()
Copy Code
GdPictureDocumentUtilities.ClearWebBrowserCache();