I am trying to print using the DotNetBrowser using code like this:
public void PrintUrl(string url)
{
using (var browser = BrowserFactory.Create())
{
browser.PrintHandler = this;
_waitEvent = new ManualResetEvent(false);
browser.FinishLoadingFrameEvent += (sender, e) =>
{
if (e.IsMainFrame)
{
browser.Print();
}
};
browser.LoadURL(url);
_waitEvent.WaitOne();
}
}
public PrintStatus OnPrint(PrintJob printJob)
{
printJob.PrintJobEvent += (sender, args) => _waitEvent.Set();
return PrintStatus.CONTINUE;
}
The page I am trying to print is rendered with Knockout. The HTML elements are bound to the Javascript Knockout model. The model is initialised when the Javascript is executed and the HTML updated.
I get only a blank page.
What I assume is happening is that the FinishLoadingFrameEvent of the main frame is triggered before the Javascript is executed --or-- the event is triggered after the Javascript has executed but before the bound HTML elements are updated.
I can get printing to work by adding the following code right before the print call:
browser.FinishLoadingFrameEvent += (sender, e) =>
{
if (e.IsMainFrame)
{
// executing javascript seems to update the DOM
browser.ExecuteJavaScriptAndReturnValue("window");
browser.Print();
}
};
Calling ExecuteJavaScriptAndReturnValue seems to work but I don't know if that is reliable. Is there a sure fire way to get the DOM to update based on the underlying KO model?
The page itself contains a script block that calls ko.applyBindings() and if I execute that instead of "window" it also seems to work.
Is this reliable though? Feels like a hack.