Convert HTML through a Proxy Server

The HiQPdf HTML to PDF Converter can load the HTML page and its resources through a HTTP or a SOCKS5 proxy server. The proxy is configured with the NetworkProxy object exposed by the HtmlToPdfProxy property. By default no proxy is set and the network settings of the system are used.

The same options are exposed by the HtmlToImageProxy property of the HTML to Image converter. The HTML headers, footers and stamps rendered during a conversion are loaded through the proxy of the converter.

Proxy Options

  • Protocol. The Protocol property selects a ProxyProtocolHttp or a ProxyProtocolSocks5 proxy. The default value ProxyProtocolNone means that no proxy is used.

  • Address. The Host property is the host name or the IP address of the proxy server and Port is its port. The proxy is used only when the protocol is not None and a host name is set.

  • Credentials. The Username and Password properties are sent when a HTTP proxy requires authentication.

  • Excluded Hosts. The ExcludedHosts array lists the hosts accessed directly, without the proxy. An entry can be a host name, an IP address, a domain with its subdomains ("*.example.com") or a CIDR range ("10.0.0.0/8"). The local machine is always accessed directly.

Code Sample - Convert HTML through a Proxy Server

C#
using HiQPdf.Next;

public byte[] ConvertHtmlThroughProxy(string url, string proxyHostName, int proxyPortNumber,
    string proxyUsername, string proxyPassword, string[] excludedHosts)
{
    // Set the serial number received after purchase to use the library in licensed mode; leave it commented for demo mode
    // Licensing.SerialNumber = "your-serial-number";

    // Create an HTML to PDF converter object with default settings
    HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

    // The protocol of the proxy used to load the HTML page and its resources. With the None protocol
    // the network settings of the system are used, as when no proxy is configured
    htmlToPdfConverter.Proxy.Protocol = ProxyProtocol.Http;

    // The address of the proxy server
    htmlToPdfConverter.Proxy.Host = proxyHostName;
    htmlToPdfConverter.Proxy.Port = proxyPortNumber;

    // The credentials for a proxy which requires authentication
    if (!string.IsNullOrEmpty(proxyUsername))
    {
        htmlToPdfConverter.Proxy.Username = proxyUsername;
        htmlToPdfConverter.Proxy.Password = proxyPassword;
    }

    // The hosts accessed directly, without the proxy: host names, IP addresses, domains with
    // their subdomains (*.example.com) or CIDR ranges (10.0.0.0/8). The local machine is
    // always accessed directly
    if (excludedHosts != null)
        htmlToPdfConverter.Proxy.ExcludedHosts = excludedHosts;

    // The same proxy options are available in the HTML to image converter

    // Convert the HTML page to a PDF document in a memory buffer
    byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);

    return outPdfBuffer;
}

See Also