spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column91


Print Templates, Part II: TemplatePrinter

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


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