function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jason.bradleyjason.bradley 

Print dynamically created sheet to printer on commandButton click?

Hello,

 

I have been looking around for a way to print, well anything really, to a printer when a commandButton is pressed, without bringing up a dialogue in which the user can manipulate settings and then tell the document to print. This application is only going to be used on one computer at a front desk in order to print badges anyways, so there is no need to change the print settings before printing the document. All of the solutions I have found so far to print things in general involve the javascript function "print()" being called from the window or whatever object needs to be printed but as I have come to understand, this brings up the system's print dialogue.

If there is not a way to do this from the VisualForce page itself due to security issues, would I be able to send information that is entered into the VisualForce page itself to a small java app or applet that would print the information without a dialogue?

 

I would greatly appreciate anyone's input on this.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You would indeed need a local client in order to achieve this effect. Browsers in general are designed with security in mind, so it's not reasonable to expect that you'll be able to circumvent this feature as a matter of convenience. Internet Explorer is probably the exception: http://stackoverflow.com/questions/1096862/print-directly-from-browser-without-print-popup-window ... so if you're okay with that limitation, go for the solution posted at the link. Depending on the complexity of the request, I would envision a cross-browser solution would look like this:

 

1) Have the target of the commandButton or commandLink (probably a link) that points to something like "http://localhost:29995/print?src=<urlencoded-target>". I chose the port arbitrarily, you should avoid IANA assigned ports, but otherwise you can pretty much do whatever.

2) Choose your favorite development language (Java, C++, .Net, doesn't really matter) that runs as a taskbar icon or service that listens on that port for requests. Once it receives a request, it should engage the printer.

 

As an alternative, you could conceptually write a Java Applet, sign it, and verify the signature on the browser. This will cause Java to not run in "sandbox mode," allowing access to the entire system, including the ability to queue print jobs. This has a benefit of not requiring a standalone app running in the background pointlessly consuming resources. The applet could be loaded when the button is clicked, perform its action, then quit.

All Answers

sfdcfoxsfdcfox

You would indeed need a local client in order to achieve this effect. Browsers in general are designed with security in mind, so it's not reasonable to expect that you'll be able to circumvent this feature as a matter of convenience. Internet Explorer is probably the exception: http://stackoverflow.com/questions/1096862/print-directly-from-browser-without-print-popup-window ... so if you're okay with that limitation, go for the solution posted at the link. Depending on the complexity of the request, I would envision a cross-browser solution would look like this:

 

1) Have the target of the commandButton or commandLink (probably a link) that points to something like "http://localhost:29995/print?src=<urlencoded-target>". I chose the port arbitrarily, you should avoid IANA assigned ports, but otherwise you can pretty much do whatever.

2) Choose your favorite development language (Java, C++, .Net, doesn't really matter) that runs as a taskbar icon or service that listens on that port for requests. Once it receives a request, it should engage the printer.

 

As an alternative, you could conceptually write a Java Applet, sign it, and verify the signature on the browser. This will cause Java to not run in "sandbox mode," allowing access to the entire system, including the ability to queue print jobs. This has a benefit of not requiring a standalone app running in the background pointlessly consuming resources. The applet could be loaded when the button is clicked, perform its action, then quit.

This was selected as the best answer
jason.bradleyjason.bradley

I just finished writing a java application that allows printing to the default printer without a printer dialogue. I haven't begun to look at making it an applet instead, but is there a way, through javascript perhaps, to start that application if it was already installed on the client's computer and interact with the methods contained within it?

jason.bradleyjason.bradley

I actually just went ahead and converted it to an applet and signed it and it at least works locally now with no problems. Now I just have to integrate it with the VF page and I should be good to go. Thanks!

sfdcfoxsfdcfox

JavaScript probably wouldn't have access to the local system (browser security), but you could also have created a service with the Tanuki Java Service Wrapper, but that may have been overkill in your case.