Forums > Java

javax.print.*

May 3, 2007 | 6:59 pm

Hello,

I’m trying to print a document using javax.print.*

I get no error message with the following code but nothing is sent to the Printer Setup Utility or whatever. No printout…
Nothing on the console too.
This has to both work on OSX and Windows.

If you have time to have a look… I’d love to know what I’m missing out.

Thanks !

O./

—————————————-
package printer;

import com.cycling74.max.*;
import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.event.*;

public class printer extends MaxObject
{

String filename = null;
FileInputStream fin = null;

printer() {
declareIO(1,2);
createInfoOutlet(false);
post("- printer 0.01: Olivier Pasquet _2007");
}

public void setfile(String args[]) throws Exception {

filename = args[0];

try {
fin = new FileInputStream(filename);
if (fin == null) {
post("? error : printer no file to print");
}
} catch(FileNotFoundException ffne) {
post("? error : printer file not found");
}
}

public void printo() {

if (filename == null) {
post("? error : printer no file to print");
} else {

try {

// Open the image file
InputStream is = new BufferedInputStream(new FileInputStream(filename));

// Find the default service
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
PrintService service = PrintServiceLookup.lookupDefaultPrintService();

// Create the print job
DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(is, flavor, null);

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(5));
aset.add(Sides.DUPLEX);
// etc….

// Monitor print job events; for the implementation of PrintJobWatcher,
// see e702 Determining When a Print Job Has Finished
PrintJobWatcher pjDone = new PrintJobWatcher(job);

// Print it
job.print(doc, aset);

// Wait for the print job to be done
pjDone.waitForDone();

// It is now safe to close the input stream
is.close();
outlet(1, "bang");
} catch (PrintException e) {
} catch (IOException e) {
}
}
}

public void defaultprinter() {
outlet(0, PrintServiceLookup.lookupDefaultPrintService().toString());
}
}

class PrintJobWatcher
{
// true if it is safe to close the print job’s input stream
boolean done = false;

PrintJobWatcher(DocPrintJob job) {
// Add a listener to the print job
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
allDone();
}
public void printJobCompleted(PrintJobEvent pje) {
allDone();
}
public void printJobFailed(PrintJobEvent pje) {
allDone();
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
allDone();
}
void allDone() {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}


May 3, 2007 | 7:13 pm

Hi Olivier,

> I’m trying to print a document using javax.print.*
>
> I get no error message with the following code but nothing is sent to
> the Printer Setup Utility or whatever. No printout…

You seem to be silently swallowing potential exceptions:
> } catch (PrintException e) {
> } catch (IOException e) {
> }
are you sure neither PrintException nor IOException are being thrown?


Owen


May 3, 2007 | 10:56 pm

> PrintException nor IOException
< yea. I'm sure. I get no message from there...

Thank you !

} catch (PrintException e) {
post("Got Printer Exception pe:" +e.getMessage());
post("Cause of pe is " + e.getCause());
} catch (IOException e) {
post("Got Printer Exception pe:" +e.getMessage());
post("Cause of pe is " + e.getCause());


May 3, 2007 | 11:58 pm

Hmmm,

Well, if not that, then have you checked which PrintJobListener method
is being invoked when it finishes? All your implementations do the same
thing, it may be worth checking the PrintJobEvent to see whether it
contains any clues.

olivier pasquet wrote:
> > PrintException nor IOException
> < yea. I'm sure. I get no message from there...
>
> Thank you !
>
> } catch (PrintException e) {
> post("Got Printer Exception pe:" +e.getMessage());
> post("Cause of pe is " + e.getCause());
> } catch (IOException e) {
> post("Got Printer Exception pe:" +e.getMessage());
> post("Cause of pe is " + e.getCause());
>


March 7, 2012 | 8:12 pm

had the same problem. I just stopped using a separate object to wait until the print was done and implemented the PrintJobListener in the applet subclass.. like

protected InputStream is;
public void printDataTransferCompleted(PrintJobEvent pje)
{
try
{
is.close();
}
catch(Exception exp)
{
JOptionPane.showMessageDialog(null, exp.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
public void printJobCanceled(PrintJobEvent pje)
{
testPrinting.setEnabled(true);
try
{
is.close();
}
catch(Exception exp)
{
JOptionPane.showMessageDialog(null, exp.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
public void printJobCompleted(PrintJobEvent pje)
{
try
{
is.close();
}
catch(Exception exp)
{
JOptionPane.showMessageDialog(null, exp.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
public void printJobFailed(PrintJobEvent pje)
{
try
{
is.close();
}
catch(Exception exp)
{
JOptionPane.showMessageDialog(null, exp.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
public void printJobNoMoreEvents(PrintJobEvent pje)
{

try
{
is.close();
}
catch(Exception exp)
{
JOptionPane.showMessageDialog(null, exp.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
public void printJobRequiresAttention(PrintJobEvent pje)
{

try
{
is.close();
}
catch(Exception exp)
{
JOptionPane.showMessageDialog(null, exp.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
}


Viewing 5 posts - 1 through 5 (of 5 total)