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.
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.
Manual Tiriggering Mode - When this triggering mode is set the converter will wait until the wnvPdfConverter.startConversion() method is called from HTML page JavaScript. The code sample below will configure the converter for manual rendering mode.
protected void convertToPdfButton_Click(object sender, EventArgs e) { // Create a HTML to PDF converter object with default settings HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(); // 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 = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og="; // 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 wnvPdfConverter.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); } // Send the PDF as response to browser // Set response content type Response.AddHeader("Content-Type", "application/pdf"); // Instruct the browser to open the PDF file as an attachment or inline Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Conversion_Triggering_Modes.pdf; size={0}", outPdfBuffer.Length.ToString())); // Write the PDF document buffer to HTTP response Response.BinaryWrite(outPdfBuffer); // End the HTTP response and stop the current page processing Response.End(); }
<!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 wnvPdfConverter != "undefined") { wnvPdfConverter.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>wnvPdfConverter.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"> of</span> <span style="font-size: 30px; color: green; float: left"> 5</span> <br /> <br /> <br /> <span style="color: navy"> <script type="text/javascript"> if (typeof wnvPdfInfo != "undefined") { document.write("Created by " + wnvPdfInfo.getVersion()); } </script> </span> </body> </html>