Start Conversion from JavaScript |
The converter exposes a global JavaScript object called ExpertPdfJSObj in the page it converts. With it a script can tell whether it runs inside the converter (check that typeof ExpertPdfJSObj == "object") and read the version of the converter with ExpertPdfJSObj.getAppVersion().
Its main use is to start the conversion from the page itself, when the page knows better than a fixed delay when it is ready - after its data has arrived and its charts are drawn, for example. To do that:
Set the StartupMode property of the PdfConverter object to Manual.
Call ExpertPdfJSObj.startConversion() from the page's JavaScript when the page is ready.
Important:
If StartupMode is set to Manual and the page never calls ExpertPdfJSObj.startConversion(), the conversion fails with a timeout after NavigationTimeout seconds.
With the default value, Automatic, the conversion starts when the page has loaded (and ConversionDelay has elapsed), without waiting for any JavaScript call.
The image converter and the HTML elements of a Document have the same StartupMode property.
Below is the HTML of a test page that counts three seconds after it loads and then starts the conversion. Converted in Manual mode the PDF shows the timer at 3; converted in Automatic mode it shows the timer at 1, because the conversion does not wait.
<!DOCTYPE html> <html> <head> <title>Start the conversion from JavaScript</title> </head> <body> <h1>Start the conversion from JavaScript</h1> <p> The page increments a timer every second until it reaches 3, then starts the conversion. </p> <p> Converter version: <span id="version"></span><br /> Timer: <span id="timer"></span> </p> <script> var timer = 0; var inConverter = typeof ExpertPdfJSObj == "object"; document.getElementById("version").textContent = inConverter ? ExpertPdfJSObj.getAppVersion() : "Not in the converter."; function incrementTimer() { timer = timer + 1; document.getElementById("timer").textContent = timer; if (timer == 3) { // 3 seconds elapsed - start the conversion if (inConverter) { ExpertPdfJSObj.startConversion(); } } else { // wait another second setTimeout(incrementTimer, 1000); } } incrementTimer(); </script> </body> </html>
The code that converts the page above in manual mode. The url variable is the address of the page.
// instantiate a html to pdf converter object PdfConverter converter = new PdfConverter(); // wait for the page to call ExpertPdfJSObj.startConversion() converter.StartupMode = StartupMode.Manual; // fail if the call does not come within 10 seconds converter.NavigationTimeout = 10; // create a new pdf document converting an url using (Document doc = converter.GetPdfDocumentObjectFromUrl(url)) { // save pdf document doc.Save("Sample.pdf"); }