EVO HTML to PDF Converter

Select Conversion Triggering Mode

EVO PDF Client for .NET Documentation

The HTML conversion to PDF happens in two main stages. In the first stage the HTML document is loaded in the internal HTML viewer. When the load is complete the second stage starts and the HTML content is rendered to PDF document or into an image. The moment when the HTML load is complete is not well defined for all HTML documents. There might be for example scripts running in page which continuously update the HTML document. The triggering modes help the converter to decide when the HTML page load should be considered completed and when the actual rendering to PDF can start. The triggering mode is given by the HtmlToPdfConverterTriggeringMode property. There are three possible triggering modes:

  • Auto Tiriggering Mode - this is the default triggering mode and the rendering to PDF starts immediately after the HTML page is loaded in converter which means the images, script files, CSS files are loaded and the scripts found in page are executed. The scripts can actually schedule asynchronous operations to be executed later, but the converter will not wait for those operations to finish. You can use the following two triggering modes to handle those asynchronous operations. The code sample for settings this triggering mode is below.

    C#
    // Create the PDF converter
    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
    // Set the triggering mode
    htmlToPdfConverter.TriggeringMode = TriggeringMode.Auto;
  • Delayed Tiriggering Mode - When this triggering mode is set the converter will wait an additional given time period for the asynchronous scripts to be executed before starting the rendering to PDF or to image. The code sample below will configure the converter to start rendering at 3 seconds after the HTML page was loaded which means the asynchronous scripts may run for 3 more seconds before the actual rendering to PDF or image starts. This type of triggering mode is good when you know that all the asynchronous scripts will finish execution in a given period of time.

    C#
    // Create the PDF converter
    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
    // Set the triggering mode
    htmlToPdfConverter.ConversionDelay = 3;
  • Manual Tiriggering Mode - When this triggering mode is set the converter will wait until the evoPdfConverter.startConversion() method is called from HTML page JavaScript. The code sample below will configure the converter for manual rendering mode.

    C#
    // Create the PDF converter
    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
    // Set the triggering mode
    htmlToPdfConverter.TriggeringMode = TriggeringMode.Manual;
Code Sample - Select Conversion Triggering Mode

C#
protected void convertToPdf()
{
    // Get the server IP and port
    String serverIP = textBoxServerIP.Text;
    uint serverPort = uint.Parse(textBoxServerPort.Text);

    // Create a HTML to PDF converter object
    HtmlToPdfConverter htmlToPdfConverter = null;
    if (radioButtonUseTcpService.Checked)
        htmlToPdfConverter = new HtmlToPdfConverter(serverIP, serverPort);
    else
        htmlToPdfConverter = new HtmlToPdfConverter(true, textBoxWebServiceUrl.Text);

    // Set optional service password
    if (textBoxServicePassword.Text.Length > 0)
        htmlToPdfConverter.ServicePassword = textBoxServicePassword.Text;

    // Set license key received after purchase to use the converter in licensed mode
    // Leave it not set to use the converter in demo mode
    htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";

    // Set the conversion triggering mode
    if (autoRadioButton.Checked)
    {
        // Set Auto triggering mode
        htmlToPdfConverter.TriggeringMode = TriggeringMode.Auto;
    }
    else if (delayedRadioButton.Checked)
    {
        // Set delayed triggering moe
        htmlToPdfConverter.ConversionDelay = int.Parse(conversionDelayTextBox.Text);
    }
    else if (manualRadioButton.Checked)
    {
        // Set manual triggering mode
        // The conversion starts when the evoPdfConverter.startConversion() is called 
        // in JavaScript code of the converted HTML page
        htmlToPdfConverter.TriggeringMode = TriggeringMode.Manual;
    }

    byte[] outPdfBuffer = null;

    if (convertHtmlRadioButton.Checked)
    {
        string htmlWithForm = htmlStringTextBox.Text;
        string baseUrl = baseUrlTextBox.Text;

        // Convert the HTML string with page-break-inside:avoid styles to a PDF document in a memory buffer
        outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlWithForm, baseUrl);
    }
    else
    {
        string url = urlTextBox.Text;

        // Convert the HTML page with page-break-inside:avoid styles to a PDF document in a memory buffer
        outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);
    }
}
HTML Code with Manual Triggering

XML
<!DOCTYPE html>
<html>
<head>
    <title>Conversion Triggering Modes</title>
    <script type="text/javascript">
        var counter = 0;
        function count() {
            // Display the current counter value
            document.getElementById("counterDiv").innerHTML = counter;

            if (counter == 5) {
                // Trigger conversion when counter reached a value
                counterDiv.style.color = "green";
                if (typeof evoPdfConverter != "undefined") {
                    evoPdfConverter.startConversion();
                }
            }
            else {
                // Increment counter
                counter++;

                // After 1 second and call count() again
                setTimeout("count()", 1000);
            }
        }
    </script>
</head>
<body onload="count()" style="font-family: 'Times New Roman'; font-size: 14px">
    The <span style="color: green"><b>count()</b></span> function is called immediately after the HTML page was loaded as a handler of the <span style="color: blue"><b>onload event</b></span> of the body element. The 
    <span style="color: green"><b>count()</b></span> function increments the counter and schedules a new call to the same function to occur after 1 second if the counter did not reach the value 5 yet.<br />
    <ul>
        <li>If the selected triggering mode is <span style="background-color: aliceblue"><b>Auto</b></span> then the execution of the initial call to <span style="color: green">count()</span> will end and the conversion to PDF will start immediately
        </li>
        <li>If the selected triggering mode is <span style="background-color: aliceblue"><b>Delayed</b></span> then the <span style="color: green">count()</span> will be invoked once a second during the delay interval and after that the conversion to PDF will start
        </li>
        <li>If the selected triggering mode is <span style="background-color: aliceblue"><b>Manual</b></span> then the <span style="color: green">count()</span> will be invoked once a second until the counter reached the value 5. When the counter reached this value the 
    <span style="background-color: whitesmoke"><b>evoPdfConverter.startConversion()</b></span> function is called to trigger the conversion to PDF
        </li>
    </ul>
    <br />
    <b style="font-size: 20px">Counter value:</b><br />
    <div style="font-size: 30px; color: red; float: left" id="counterDiv">0 </div>
    <span style="font-size: 30px; color: black; float: left">&nbsp; of</span>
    <span style="font-size: 30px; color: green; float: left">&nbsp; 5</span>
    <br />
    <br />
    <br />
    <span style="color: navy">
        <script type="text/javascript">
            if (typeof evoPdfInfo != "undefined") {
                document.write("Created by " + evoPdfInfo.getVersion());
            }
        </script>
    </span>
</body>
</html>