Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Direct Print to a Printer

 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've used the following code to imitate a direct print to a printer using javascript. The terminology may not be sound but the code works. Hopefully this will help someone out there looking to print to a printer "directly". If you have a better alternative approach, what keep you from getting back sooner? :
================================================
// Set the frames source, basically your HTML
// page holding the javascript will be in one
// frame while this one will hold the data to
// print. Set the javascript page to be 100%
// width and the data page frame to be 0%
//==============================================
var purchasetxt = print_items.arguments[0];
window.parent.frames['display'].src = purchasetxt;
//=== Obtain the data frame object
upper_object = window.parent.frames['display'];
//=== Create a Web Browser Object
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';

//=== Insert the Web Browser Object into
//=== the second frame's window upper_object.document.body.insertAdjacentHTML('beforeend', WebBrowser);
//=== Set focus to the second frame (i.e. data
//=== frame)
upper_object.focus();
//=== Execute the Web Browser Object upper_object.document.WebBrowser1.ExecWB(6,-1);
//=== Get rid of the Embedded Web Browser Object upper_object.document.WebBrowser1.outerHTML = "";
=================================================
 
ric tan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi..

do you have a web page with this function works ?

thanks
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're lucky I noticed this cause I had another question I was looking for. Yeah, I do have a few applications that has this working on them. Mind you, the people using this normally run only IE but I do believe it has been tested on Netscape as well.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic