Click or drag to resize

Start Conversion from Javascript

ExpertPdf exposes a global Javascript variable called ExpertPdfJSObj in the page that is being converted. Using this variable you can check if the javascript code is executed inside the converter (check if typeof(ExpertPdfJSObj) == "object"). The converter version can also be checked using ExpertPdfJSObj.getAppVersion().

The most important feature of the Javascript interface is the possibility to manually start the page conversion to pdf with a javascript call. To do that, you must:

  1. Set the StartupMode property of the PdfConverter object to Manual.

  2. Call ExpertPdfJSObj.startConversion() from javascript.

Important:

  • If StartupMode is set to Manual and ExpertPdfJSObj.startConversion() is not called from javascript, the conversion will timeout.

  • If StartupMode is set to the default value Automatic, the conversion will start without waiting for any javascript calls.

Sample Code

Below it's the html of a test page that waits for 3 seconds after it loads and then calls the javascript conversion method. Converting it in Manual mode will display the time elapsed. Converting it in Automatic mode will not show that 3 seconds time interval.

XML
<!DOCTYPE html>
<html>
    <head>
        <title>Test document for ExpertPdf Sample</title>
    </head>
    <body>
        <h1>ExpertPdf for .NET - Start Conversion from Javascript</h1>
        <p>
            This is a sample page that will demonstrate how a html to pdf conversion can be triggered from a javascript call using ExpertPdf Html To Pdf Converter for .NET.
            <br />
            The page increments a timer until it reaches 3 and then it calls the conversion from javascript. 
            If the conversion is manually started from javascript, the timer below should be 3 in pdf. If the conversion is automatically started when the page loads, the timer below should be 1 in pdf.
        </p>
        <p>
            Library version: <span id="idVersion"></span><br />
            Timer: <span id="idTimer"></span><br />
        </p>

        <script type="text/javascript" src="jquery.js"> </script>
        <script type="text/javascript">
            var timer = 0;

            function incrementTimer() {
                timer = timer + 1;

                if (typeof (ExpertPdfJSObj) == "object") {
                    $("*#idVersion").html(ExpertPdfJSObj.getAppVersion());
                }
                else {
                    $("*#idVersion").html("Not in converter.");
                }
                $("*#idTimer").text(timer);

                if (timer == 3) {
                    // 3 seconds elapsed - start conversion
                    if (typeof (ExpertPdfJSObj) == "object") {
                        ExpertPdfJSObj.startConversion();
                    }
                }
                else {
                    // wait another second
                    setTimeout("incrementTimer()", 1000);
                }

            }

            $(document).ready(function () {
                incrementTimer();
            });
        </script>

    </body>
</html>

This sample code shows how to manually start the html to pdf conversion of the ExpertPdf Library for .NET using a Javascript call. The url parameter points to the page above.

// instantiate a html to pdf converter object
PdfConverter converter = new PdfConverter();

// set startup mode
converter.StartupMode = StartupMode.Manual;

// set timeout
converter.NavigationTimeout = 10;

// create a new pdf document converting an url
Document doc = converter.GetPdfDocumentObjectFromUrl(url);

// save pdf document
doc.Save(Response, false, "Sample.pdf");

// close pdf document
doc.Close();
See Also