HiQPdf Documentation

Set Conversion Triggering Mode

Quickly Create High Quality PDFs

Normally the conversion to PDF starts immediately after the HTML document was loaded in converter and this default behavior is suitable for converting most of the HTML documents. However, there are situations when some JavaScript scripts continue execution even after the document was loaded. In this case it is necessary to configure the converter to wait a predefined interval before starting the rendering to PDF or to configure the converter to wait for the hiqPdfConverter.startConversion() method to be manually called from JavaScript.

Below there is an example of HTML document which requires manual triggering of the conversion. In the sample script we provide, a ticks counter is incremented each 30 ms after the document was loaded. When the ticks count reached 100 in about 3 seconds the startConversion() method is called.

In the HTML script below you can also notice a call to the hiqPdfInfo.getVersion() JavaScript method. This method is exposed by the converter to the JavaScript code in the HTML document being converted to offer information about the HiQPdf library version. The existence of the hiqPdfInfo object in JavaScript can be used to determine whether the document is currently loaded in HiQPdf converter. For example, when the HTML document is loaded in converter you can run a script to change the styles in the document.

HTML Script to Manually Trigger Conversion

<html>
<head>
    <title>Conversion Triggering Mode</title>
</head>
<body>
    <span style="font-family: Times New Roman; font-size: 10pt">When the triggering mode
        is 'Manual' the conversion is triggered by the call to <b>hiqPdfConverter.startConversion()</b>
        from JavaScript.<br />
        In this example document the startConversion() method is called when the ticks count
        reached 100 which happens in about 3 seconds.</span>
    <br />
    <br />
    <b>Ticks Count:</b> <span style="color: Red" id="ticks">0</span>
    <br />
    <br />
    <!-- display HiQPdf HTML converter version if the document is loaded in converter-->
    <span style="font-family: Times New Roman; font-size: 10pt">HiQPdf Info:
        <script type="text/javascript">
            // check if the document is loaded in HiQPdf HTML to PDF Converter
            if (typeof hiqPdfInfo == "undefined") {
                // hiqPdfInfo object is not defined and the document is loaded in a browser
                document.write("Not in HiQPdf");
            }
            else {
                // hiqPdfInfo object is defined and the document is loaded in converter
                document.write(hiqPdfInfo.getVersion());
            }
        </script>
    </span>
    <br />
    <script type="text/javascript">
        var ticks = 0;
        function tick() {
            // increment ticks count
            ticks++;

            var ticksElement = document.getElementById("ticks");
            // set ticks count
            ticksElement.innerHTML = ticks;
            if (ticks == 100) {
                // trigger conversion
                ticksElement.style.color = "green";
                hiqPdfConverter.startConversion();
            }
            else {
                // wait one more tick
                setTimeout("tick()", 30);
            }
        }

        tick();
    </script>
</body>
</html>
Triggering Modes Demo

In this demo you learn how to configure various triggering modes. There are three conversion triggering modes: Auto, WaitTime and Manual. In the sample script above, a ticks counter is incremented each 30 ms after the document was loaded. When the ticks count reached 100 in about 3 seconds the startConversion() is called.

  • When the triggering mode is Manual the call to startConversion() will trigger the conversion.

  • When the triggering mode is WaitTime a wait time of 5 seconds is sufficient to allow the ticks count reach 100.

  • When the triggering mode is Auto the conversion will start before the counter reached 100.

Demo Source Code

C#
private void buttonCreatePdf_Click(object sender, EventArgs e)
{
    // create the HTML to PDF converter
    HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

    Cursor = Cursors.WaitCursor;

    string pdfFile = Application.StartupPath + @"\DemoOutput\TriggeringMode.pdf";
    try
    {
        // set triggering mode; for WaitTime mode set the wait time before convert
        switch (comboBoxTriggeringMode.SelectedItem.ToString())
        {
            case "Auto":
                htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Auto;
                break;
            case "WaitTime":
                htmlToPdfConverter.TriggerMode = ConversionTriggerMode.WaitTime;
                htmlToPdfConverter.WaitBeforeConvert = int.Parse(textBoxWaitTime.Text);
                break;
            case "Manual":
                htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Manual;
                break;
            default:
                htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Auto;
                break;
        }

        // convert the URL to PDF
        htmlToPdfConverter.ConvertHtmlToFile(textBoxHtmlCode.Text, null, pdfFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("Conversion failed. {0}", ex.Message));
        return;
    }
    finally
    {
        Cursor = Cursors.Arrow;
    }

    // open the PDF document
    try
    {
        System.Diagnostics.Process.Start(pdfFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("Conversion succeeded but cannot open '{0}'. {1}", pdfFile, ex.Message));
    }
}
See Also

Other Resources