JPS- No printer services available

R

ruds

Hi,
I want to print document to a specific printer using Java PrintService
API.
When I execute the program I get the output as "No printer services
are available".
My code is:
import java.io.*;
import java.io.File;
import java.io.IOException;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;

public class Test {
public static void main(String[] a) {
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintRequestAttributeSet aset= new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
aset.add(new Copies(1));

/* locate a print service that can handle it
*/
PrintService[] pservices =PrintServiceLookup.lookupPrintServices
(flavor, aset);
if (pservices.length == 0)
System.out.println("no printer service available");
if (pservices.length > 0)
{
System.out.println("selected printer " +pservices[0].getName());
//Appendix A Example: PrintPS.java 43
/* create a print job for the chosen service
*/
DocPrintJob pj = pservices[0].createPrintJob();
try {
/* * Create a Doc object to hold the print data.
* Since the data is postscript located in a disk file,
* an input stream needs to be obtained
* BasicDoc is a useful implementation that will if
* requested close the stream when printing is completed.
*/
FileInputStream fis = new FileInputStream("C:\\1.doc");
Doc doc = new SimpleDoc(fis, flavor, null);
/* print the doc as specified
*/
// pj.print(doc, aset);
} catch (IOException ie) {System.err.println(ie);}
/*catch (PrintException e) {System.err.println(e);}*/
}

}
}
 
R

ruds

ruds said:
PrintService[] pservices =PrintServiceLookup.lookupPrintServices
(flavor, aset);

I tried your code, it does the same for me.  However, if I remove these
constraints that I quoted above (pass null), then I get a list of 5
printers.  Printing out all of the constraints that each printer has
reveals that none advertise their page sizes.  I.e., ISO-A4 isn't an
attribute that any printer claims to support, even though the all print
to regular size paper.

I guess you'll have to just live with out the constraints.

Even if I remone the constraints, I just get one printer name whereas
I have 5-6 printers installed.
Actaully I want to send my dcuments to a PDF driver installed on my
machine.
Can I hard code the printer driver name?How should I call it?
 
M

Mark Space

ruds said:
Even if I remone the constraints, I just get one printer name whereas
I have 5-6 printers installed.
Actaully I want to send my dcuments to a PDF driver installed on my
machine.
Can I hard code the printer driver name?How should I call it?


Beats me. I've never used the thing, I just read the docs and tried a
couple of things. If removing all constraints, just passing null for
both constraints, gives you just one printer back, I'd say that's the
only printer that Java knows about. You might have some sort of
low-level installation problem, or need a jar file that implements an
interface to the printer driver. Either way, I can't guess how to fix
it, other than start digging into all the documentation Sub provides,
and maybe tracing out code, including native code.
 
R

ruds

Ok I was partly succesful in firing the print using JPS, but agian I'm
getting stuck if I call the same function through a servlet. the
PDFCreator is called but no o/p is generated just like in all other
cases before. Also, when I'm calling the PDFCreator printer through
the standalone Java Program the PDF is created but it is not getting
saved by the same file name. I have set in the options of PDFCreator
to autosave the file using the same file name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top