Re: [OT] Pre-selecting a printer?
Re: [OT] Pre-selecting a printer?
- Subject: Re: [OT] Pre-selecting a printer?
- From: Jean-Francois Veillette <email@hidden>
- Date: Thu, 29 Oct 2009 09:31:24 -0400
I am able to share some code, here is the component that does the
'print panel' of the app.
The code may not be functionnal, it was changed on the fly right here
while composing this mail (translation from french, simplified and
removed some class dependencies).
It is a good start that could be integrated in a generic framework
(wonder or else).
This class was written as a test, a proof of concept. It worked and
we never really got back to it to make it 'generic' and 'clean', so
there are lots of space for improvement.
Ramsey, you can see in that code how you can access the printer
capabilities as reported by cups (here it does the minimum). You
could check more printer attributes and have your own algoritm to
choose the best printer from your print job.
enjoy ;-)
- jfv
package a.b.c.webobjects;
import com.webobjects.appserver.*;
import com.webobjects.foundation.*;
import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import javax.print.CancelablePrintJob;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.SimpleDoc;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaTray;
import javax.print.attribute.standard.RequestingUserName;
import javax.print.attribute.standard.Sides;
import de.lohndirekt.print.IppPrintServiceLookup;
import de.lohndirekt.print.attribute.auth.RequestingUserPassword;
public class PrintPanel extends WOComponent {
public final static String PRINTER_COOKIE = "selectedPrinter";
/* @TypeInfo PrintService */
protected NSArray printerList;
public PrintService printerItem;
public PrintService selectedPrinter;
protected NSArray listPaperTray;
public MediaTray paperTrayItem;
public MediaTray selectedPaperTray;
public int copyNumber;
public PrintPanel(WOContext context) {
super(context);
copyNumber = 1;
// setting cups properties
String uri = System.getProperty("PrintPanel_CUPS_URI");
String usr = System.getProperty("PrintPanel_CUPS_USERNAME");
String pwd = System.getProperty("PrintPanel_CUPS_PASSWORD");
System.getProperties().setProperty(IppPrintServiceLookup.URI_KEY,
uri /* "http://127.0.0.1:631" */);
System.getProperties().setProperty(IppPrintServiceLookup.USERNAME_KEY,
usr /* "root" */);
System.getProperties().setProperty(IppPrintServiceLookup.PASSWORD_KEY,
pwd /* "test" */);
}
/**
* Take the 'appendToResponse' phase to deal with cookies, before
showing up the print panel.
*/
public void appendToResponse(WOResponse reponse, WOContext contexte) {
checkCookies();
super.appendToResponse(reponse, contexte);
}
/** Cookie management, enhance user experience */
void checkCookies() {
if(selectedPrinter == null) {
String nom = context().request().cookieValueForKey(PRINTER_COOKIE);
NSArray liste = printerList();
if(nom != null) {
java.util.Enumeration en = liste.objectEnumerator();
while (en.hasMoreElements()) {
PrintService printer = (PrintService)en.nextElement();
if(nom.equals(printer.getName())) {
selectedPrinter = printer;
}
}
} else {
selectedPrinter = (PrintService)liste.objectAtIndex(0);
}
}
WOCookie cookie = new WOCookie(PRINTER_COOKIE,
selectedPrinter.getName(), "/", null, -1, false);
context().response().addCookie(cookie);
}
/** Commande la génération des fichiers pdf et le retour de ceux-ci
dans la réponse au serveur web. */
public WOComponent preview() {
NSData pdf = pdf();
FileDownloader tf = (FileDownloader)pageWithName("FileDownloader");
tf.setTypeMime(FileDownloader.TYPE_PDF);
tf.setData(pdf);
tf.setFileName("aFileName.pdf");
return tf;
}
public NSArray printerList() {
if(printerList == null) {
printerList = new NSArray(new
IppPrintServiceLookup().getPrintServices(DocFlavor.INPUT_STREAM.PDF,
null));
try {
NSComparator comparateur = new NSComparator() {
public int compare(Object o1, Object o2) {
return
printerName
((PrintService)o1).compareToIgnoreCase(printerName((PrintService)o2));
}
};
printerList = printerList.sortedArrayUsingComparator(comparateur);
} catch(Exception e) {
System.out.println("Exception dans le tri de la liste des
printers : " + e.getMessage());
}
}
return printerList;
}
public NSArray listPaperTray() {
return new NSArray( new Object[]{MediaTray.TOP, MediaTray.MIDDLE,
MediaTray.BOTTOM, MediaTray.ENVELOPE, MediaTray.MANUAL,
MediaTray.LARGE_CAPACITY, MediaTray.MAIN, MediaTray.SIDE } );
}
public String printerItemPrinterInfo() {
return printerName(printerItem);
}
public String printerName(PrintService pi) {
return
String
.valueOf
(pi.getAttribute(javax.print.attribute.standard.PrinterInfo.class));
}
public WOComponent print() throws java.io.FileNotFoundException {
// create a job
CancelablePrintJob job = (CancelablePrintJob)
selectedPrinter.createPrintJob();
// set the job attributes
PrintRequestAttributeSet attributes = new
HashPrintRequestAttributeSet();
// number of copies ?
Copies copies = new Copies(copyNumber);
attributes.add(copies);
// we want duplex printing
// Sides sides = Sides.DUPLEX;
// attributes.add(sides);
// print it on the main media tray
Media media = selectedPaperTray; // MediaTray.MAIN;
// If you have special Mediatrays (like most printers)
// you can use the class LdMediaTray and give a String to the
constructor like
// new LdMediaTray("Optional2");
attributes.add(media);
// Now create a document
NSData pdf = pdf();
InputStream stream = pdf.stream();
Doc doc = new SimpleDoc(stream, DocFlavor.INPUT_STREAM.PDF, new
HashDocAttributeSet());
// finally the print action
try {
// we are setting the doc and the job attributes
job.print(doc, attributes);
System.out.println("printing successfull...");
} catch (PrintException e) {
e.printStackTrace();
}
return null;
}
/** This is where you should be able to return the pdf you want to
print. */
public NSData pdf() {
return ???;
}
public boolean isStateless() {
return true;
}
public boolean synchronizesVariablesWithBindings() {
return false;
}
}
Le 09-10-25 à 21:09, Pascal Robert a écrit :
We have an app that use CUPS to print labels. The framework use
JIPSI :
http://www.lohndirekt.de/software/jipsi_quickstart_drucksoftware.html
Disclaimer : I didn't setup the CUPS server and didn't work on the
app, I just know that we have an app that use it :-) You should look
at the IPP stuff in LEWOStuff from Andrew.
No, that would be crazy, even for me (^_^)
I was just wondering if it were possible to set up a client Mac to
print automatically to a specific printer based on characteristics of
the print job. So, let's say I have a color inkjet printer, a black
and white laser printer, and a star receipt printer. If I print
out a
picture, I want the picture to go to the color printer. If I'm
printing a black and white, text only document, then I want that to
go
to the laser, and if I have a print job that is 7cm wide, it should
go
to the receipt printer. Automatically... or at the very least, pre-
selected in the print dialog so that all I have to do is hit the
enter
key to print. It seems the only options I really have is to select a
specific printer, or default to the last printer used. I'm wondering
if it is possible to select the "most appropriate" printer based on
characteristics of the print job.
CUPS looked pretty neat. I imagined there might be some hidden
configuration magic in it, so I thought I would ask the list to see
if
anyone know of any. (^_^)
Ramsey
On Oct 25, 2009, at 6:39 PM, Andrew Lindesay wrote:
Hi Ramsey;
Do you mean that you would like to change the default printer on a
users' machine from a web page?
cheers.
Are you able to just print directly to the printer from the
application server?
...
Is it possible, if I have control of the client machines, to
select a printer based on the printed page size? Also, is it
possible to bypass the print dialog? I tried searching the Apple
printing list but didn't come up with a lot of answers...
...
I was thinking of something like a star printer hooked to a client
machine. Is there anything in OS X that would let me configure the
OS to automatically send jobs to the right place? I found
___
Andrew Lindesay
www.lindesay.co.nz
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden