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();
}
|