views:

1419

answers:

4

I know you can use window.print() to print the current page... but what I want to know is can I build a document using javascript in order to populate it with data and print it off?

Just like you can have html/xml as a javascript object, can you do something similar to this:

var name = "Matt";
var htmlDocumentToPrint = "<html><body><div style='width:300px; height:20px; background-color:#000; text-align:center;'>My name is " + name + "</div></body></html>";

htmlDocumentToPrint.print();

I don't really care about adding colors all that much-- just want to format a document, populate it with data, and print it off. Is this possible?

A: 

If you are doing this while the document is being loaded, you can use document.write to write the current document, then print it.

If the page has finished loading, you can use functions to manipulate the DOM, or preferably use a library such as jQuery or Prototype, then print the current document.

Sinan Taifour
A: 

print() essentially just calls up the native print dialog for a given window.

But as you're are thinking, it will work on any window or (i)frame.

thus if you write content to a frame, you can then call this to print it.

window.frameName.print();

note the only drawback (and its a big one), is that this calls up the print dialog... not the print preview window... thus the user doesn't really get a chance to see what they are printing and/or scale it to fit their printer/paper.

I personally wish that all browsers would implement the following to handle the above issue. ;-)

window.printPreview();
scunliffe
+1  A: 

Print() is a method on the window object. If you create a document in a window using javascript, then call print on that window object, it should work.

<script type="text/javascript">
    var myWindow=window.open('','','width=200,height=100')
    myWindow.document.write("This is 'myWindow'")
    myWindow.print();
</script>

Example modified from w3schools.com window open example.

tvanfosson
You can also use a hidden iframe to print hidden documents!
J-P
This doesn't seem to work in Chrome or Opera. Both browsers seems to print the parent window when self.print() or window.print() are called on a hidden iFrame.
Jack
In fact, it doesn't seem to work with an iframe with height and width of 0px either...
Jack
+1  A: 

My first thought:

You could create an iframe programmatically, assign the HTML to be printed, call the print() function on the context of the iframe.contentWindow, and then remove the iframe from the DOM:

function printHTML(input){
  var iframe = document.createElement("iframe"); // create the element
  document.body.appendChild(iframe);  // insert the element to the DOM 

  iframe.contentWindow.document.write(input); // write the HTML to be printed
  iframe.contentWindow.print();  // print it
  document.body.removeChild(iframe); // remove the iframe from the DOM
}

printHTML('<h1>Test!</h1>');

You can test the above snippet here.

CMS
Doesn't work in IE. If I remember correctly you need to focus() the window before printing it, or something similar.
J-P
This crashes Google Chrome
David Kemp