Start Conversion from Javascript |
Select.Pdf exposes a global Javascript variable called selectpdf 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(selectpdf) == "object"). The converter version can also be checked using selectpdf.version.
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:
Set the StartupMode property of the HtmlToPdfOptions object to Manual.
Call selectpdf.start() from javascript.
Important:
If StartupMode is set to Manual and selectpdf.start() 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.
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.
<!DOCTYPE html> <html> <head> <title>Test document for Select.Pdf Sample</title> </head> <body> <h1>Select.Pdf 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 Select.Pdf Library 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 (selectpdf) == "object") { $("*#idVersion").html(selectpdf.version); } else { $("*#idVersion").html("Not in converter."); } $("*#idTimer").text(timer); if (timer == 3) { // 3 seconds elapsed - start conversion if (typeof (selectpdf) == "object") { selectpdf.start(); } } 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 Select.Pdf Library for .NET using a Javascript call. The url parameter points to the page above.
// instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // set startup mode converter.Options.StartupMode = HtmlToPdfStartupMode.Manual; // set timeout converter.Options.MaxPageLoadTime = 10; // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(url); // save pdf document doc.Save(Response, false, "Sample.pdf"); // close pdf document doc.Close();