Java PrintJob works on Windows and Linux but incomplete on Mac

Java application 1.4.1-04 prints a multi-page graphic and text fine on Windows and Linux but produces incomplete output on Mac OSX 10.3.5 (jre 1.4.2-05).  Different bits of the page are missing each time it runs!  No problems on the Windows or Linux boxes so is there something different about the Mac java I'm not aware of?  Looks like the graphic is not completly drawn before the actual print ocurs.

The codJava application 1.4.1-04 prints a multi-page graphic and text fine on Windows and Linux but produces incomplete output on Mac OSX 10.3.5 (jre 1.4.2-05).  Different bits of the page are missing each time it runs!  No problems on the Windows or Linux boxes so is there something different about the Mac java I'm not aware of?  Looks like the graphic is not completly drawn before the actual print ocurs.

The code is basically:

printJob = printJob.getPrinterJob();
printJob.setPrintable(this, pf);
printBook = new Book();
for(int i = 0; i < pages; i++)
{
    printBook.append((Printable)this, pf);
}
printJob.setPageable(printBook);
if(printJob.printDialog())
        printJob.print();

Thankse is basically:

printJob = printJob.getPrinterJob();
printJob.setPrintable(this, pf);
printBook = new Book();
for(int i = 0; i < pages; i++)
{
    printBook.append((Printable)this, pf);
}
printJob.setPageable(printBook);
if(printJob.printDialog())
        printJob.print();

Thanks
John4343Asked:
Who is Participating?
 
VenabiliConnect With a Mentor Commented:
Yes,
In the J2SE 1.4.1 or 1.4.2 on Mac OS X javax.print is not fully implementeed and is not fully working (don't ask me... tok me a week to find a url that proves what I already suspectred.\

Here we go:
http://developer.apple.com/java/faq/issues.html#anchor6
The workaround is to use the old print methods from jawa.awt.print:
              PrintRequestAttributeSet  pras = new HashPrintRequestAttributeSet();
               PrintService service = null;
               java.awt.print.PrinterJob pjob = java.awt.print.PrinterJob.getPrinterJob();
               PrintService printService[] =  pjob.lookupPrintServices();
               if (printService == null || printService.length == 0) {
                   ERROR = 1;
                   return null;
              }
           if ( pjob.printDialog()){
               service = pjob.getPrintService();
               pJob = pjob;
               service = servicePrint;
           }else{
               service = null;
           }
if (servicePrint!=null){
               DocAttributeSet das = new HashDocAttributeSet();
              Doc doc = new SimpleDoc(book, DocFlavor.SERVICE_FORMATTED.PAGEABLE, das);
                DocPrintJob job1 = servicePrint.createPrintJob();
                job1.print(doc, pras);
}


Let me know if you need assitance on this (I am not sure if it compiles as it is as I removed some of the code taht is specific for my application - if there are troubles let me know.

As a whole I just check if it is a MacOS or not and depending on the case use different print code (this one is for MacOS - your is ok for any other)
Checking for MacOS:
             System.getProperty("mrj.version")  is null if you are not on Mac ;)
0
 
Giant2Commented:
I see you use 2 different JRE to execute. Try to use the same JRE on both machine (Linux, Windows, Mac OS).
Do the error remains?
0
 
John4343Author Commented:
I'm not quite certain about this so please bare with me.  This code works in Windows but crashes on the indicated line on the Mac with a null pointer exception!

import java.awt.print.*;

javax.print.attribute.PrintRequestAttributeSet pras = new javax.print.attribute.HashPrintRequestAttributeSet();
javax.print.PrintService service = null;
PrinterJob pjob = PrinterJob.getPrinterJob();
javax.print.PrintService printService[] =  pjob.lookupPrintServices();
if (printService == null || printService.length == 0)
{
    System.out.println ("ERR");
    return;
}
                  
PageFormat pf = pageFormat.convertToPageFormat();
Book book = new Book();
int pages = 3;
for(int i = 0; i < pages; i++)
{
    book.append((Printable)this, pf);
}
pjob.setPageable(book);
if (pjob.printDialog())
{
   service = pjob.getPrintService();
}
else
{
   service = null;
}
if (service!=null)
{
   javax.print.attribute.DocAttributeSet das = new javax.print.attribute.HashDocAttributeSet();
   javax.print.Doc doc = new javax.print.SimpleDoc(book, javax.print.DocFlavor.SERVICE_FORMATTED.PAGEABLE, das);
   javax.print.DocPrintJob job1 = service.createPrintJob();
   try
   {
      job1.print(doc, pras);  <<=== NPE
   }
   catch(Exception ee)
   {
      System.out.println ("Error 1");
      ee.printStackTrace();
   }
}

Thanks
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
VenabiliCommented:
The StackTrace?
0
 
VenabiliCommented:
Oops... I think I need coffee.  This is half the Win code, half the Mac code :(

Sorry

THe actual printing should be:
if (service!=null)
{
   try
   {
       pJob.print();
   }
   catch(Exception ee)
   {
      System.out.println ("Error 1");
      ee.printStackTrace();
   }
}

The null pointer is exactly from the not implemented method I was trying to warn you about :(

Which returns us in the almost initial position...

Venabili
0
 
John4343Author Commented:
Here is the current code.  Works OK on Windows but still incomplete output on Mac.

Thanks for the help.  I'll be out of town till late Friday and will try not to think too hard...

import java.awt.print.*;

javax.print.attribute.PrintRequestAttributeSet pras = new javax.print.attribute.HashPrintRequestAttributeSet();
javax.print.PrintService service = null;
PrinterJob pjob = PrinterJob.getPrinterJob();
javax.print.PrintService printService[] =  pjob.lookupPrintServices();
if (printService == null || printService.length == 0)
{
   System.out.println ("ERR");
   return;
}
PageFormat pf = pageFormat.convertToPageFormat();
Book book = new Book();
int pages = 3;
for(int i = 0; i < pages; i++)
{
   book.append((Printable)this, pf);
}
pjob.setPageable(book);
if (pjob.printDialog())
{
   service = pjob.getPrintService();
}
else
{
   service = null;
}
if (service!=null)
{
   try
   {
      pjob.print();
   }
   catch(Exception ee)
   {
      System.out.println ("Error 1");
      ee.printStackTrace();
   }
}
0
 
John4343Author Commented:
Ah! Got it!!!  

awt.print runs in a separate thread so needed to do a synchronize so the page will be rendered completly.

Thanks
0
 
VenabiliCommented:
If you prefer you can get your points back? After all you found it itself. However - if you think that I had helped, it's ok

Venabili
0
All Courses

From novice to tech pro — start learning today.