Printing a webhtmlviewer

I have a web page

On that web page there is an HTMLviewer

The HTMLviewer is displaying a PDF that was server up by my web server

I wish to add a button to print the pdf document only.

Any ideas?

Any Javascript gods out there? I found some javascipt I think will do the job, but I haven’t a clue where to put it/ how to implement

[code]printPdf = function (url) {
var iframe = this._printIframe;
if (!this._printIframe) {
iframe = this._printIframe = document.createElement(‘iframe’);
document.body.appendChild(iframe);

iframe.style.display = 'none';
iframe.onload = function() {
  setTimeout(function() {
    iframe.focus();
    iframe.contentWindow.print();
  }, 1);
};

}

iframe.src = url;
}[/code]

Add this method to a module. It extends WebHtmlViewer, so call myViewer.PrintContentTD from any Xojo control event.

Sub PrintContentTD(Extends objHtml As WebHTMLViewer)
  'NOTE: this ONLY works for content served from the same domain as your web app.
  'Trying to print content from another domain triggers a JavaScript permissions error.
  'Cross site scripting is forbidden and the print command is off the iframe content.
  '
  'You could possibly work around this by having your web app download the content and serve it
  'via HandleSpecialURL so that, from the client's point of view, it does come from your domain.
  
  'Preflight
  If Session = Nil Then Return
  
  'Setup js.
  Dim js As String
  
  If Session.Browser = WebSession.BrowserType.InternetExplorer Then
    js = "var objFrame = document.querySelector('#"+objHtml.ControlID+" iframe'); objFrame.contentWindow.document.execCommand('print', false, null);"
  Else
    js = "var objFrame = document.querySelector('#"+objHtml.ControlID+" iframe'); objFrame.focus(); objFrame.contentWindow.print();"
  End If
  
  'Run it.
  objHtml.ExecuteJavaScript(js)
  
End Sub

Hmmm…testing something else I just realized that the docs are wrong about WebHtmlViewer.Print. FireFox does not print the contents of the entire page. It only prints the contents of the viewer.

The code above is still useful for Internet Explorer which does print the web app page when using WebHtmlViewer.Print.