Print Templates, Part II: TemplatePrinter: Printing the Document - Doc JavaScript | WebReference

Print Templates, Part II: TemplatePrinter: Printing the Document - Doc JavaScript


Print Templates, Part II: TemplatePrinter

Printing the Document

Before sending the document to printing, we call PrintPrep(). The purpose of this function is to make sure the pages are ready for printing. We sample page1 only:

  if (layoutrect1.contentDocument.readyState == "complete") {
    PrintNow();
  }

When the page is not ready, we install a new event handler that will fire off when the page is ready:

  layoutrect1.contentDocument.onreadystatechange =
  PrintWhenContentDocComplete;

Here is the full PrintPrep() function :

function PrintPrep() {
  if (layoutrect1.contentDocument.readyState == "complete") {
    PrintNow();
  }
  else {
    layoutrect1.contentDocument.onreadystatechange =
     PrintWhenContentDocComplete;
  }
}

The function PrintWhenContentDocComplete() checks again the readyState of page1, and only then calls PrintNow(). We also need to set the readyState property to null, to make sure the onreadystatechange won't fire off again and again. Here is the whole PrintWhenContentDocComplete() function:

function PrintWhenContentDocComplete() {
  if (layoutrect1.contentDocument.readyState == "complete")
    {
    layoutrect1.contentDocument.onreadystatechange = null;
    PrintNow();
  }
}

The function PrintNow() is straightforward. You need to start with the startDoc() method, giving it a string for annotating the print job with. You end up the print job with the stopDoc() method. In between, you call printPage() with every page of the document. Here is the full listing of the printNow() function:

function PrintNow() {
  printer.startDoc("Printing from template2.htm");
  printer.printPage(page1);
  printer.printPage(page2);
  printer.stopDoc();
}

Next: Code listing of the print template

http://www.internet.com


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: August 27, 2001
Revised: August 27, 2001

URL: http://www.webreference.com/js/column91/5.html