Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

Print job goes to print queue and then disappear

Hi Experts,

I have an issue with printing documents thru our web app(Tomcat). The printer works OK from other applications, but it doesn’t print thru our web app even though the print job goes to print queue.(it goes to print queue and then just disappear and nothing prints.) .
I don't see any error message from the log either.

I have asked other code related question before with similar issue and now I have this printing issue.

public void sendPDFToPrinter(byte[] pdf, String printerQ) throws PrintUtilException {
		
		boolean printed = false;
		 //DocFlavor pdfFlavor = DocFlavor.BYTE_ARRAY.PDF;
		//DocFlavor pdfFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
		DocFlavor pdfFlavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
		 Doc pdfDoc = new SimpleDoc(pdf, pdfFlavor, null);
		
		PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
		      
 		if(services != null) {
		
			for(PrintService service : services) {
				 PrintServiceAttributeSet atts = service.getAttributes();
				if(atts == null) {					
					continue;
 				}
				Attribute theAtt = atts.get(javax.print.attribute.standard.PrinterName.class);
				
				if(theAtt.toString().equals(printerQ)) {
					
					DocPrintJob job = service.createPrintJob();
					job.addPrintJobListener(new WTFListener());
					
					PrintRequestAttributeSet psas = new HashPrintRequestAttributeSet();				
					psas.add(Sides.DUPLEX);
					
					try {
						job.print(pdfDoc, psas);
						printed = true;
						break;
					} catch (PrintException pe) {
						pe.printStackTrace();
						throw new PrintUtilException(pe.getMessage());
					} 
				}
			}
		}
...
...

Open in new window


thanks,
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You said you got it working. So what happened?
I believe that Java does not support PDF printing under Windows - unless your printer (what model do you have?) directly supports PDF printing. When printing from other applications, the driver converts the PDF to whatever the printer can understand. Under Java you can access external applications, so you can call Acrobat to do the printing.

Have a look at these links:
https://www.idrsolutions.com/jpedal/print-pdf-files-in-java
https://blog.idrsolutions.com/2010/01/printing-pdf-files-from-java/
Avatar of dkim18
dkim18

ASKER

CEHJ,

The previous question was about invalid flavor and data is not of declared type error message. I resolved that and I thought I was able to print pdf thru the web app.

hdhondt,

I am testing with Samsung M2020 Series printer. I also have Brother HL 5140(this is a bit old model.)

I ran this code:
  public static void main(String args[]){
        PrintService[] allServices = PrintServiceLookup.lookupPrintServices(null, null);
            
        
        String printerQ = ResourceUtils.getEhrPropsResourceValue("printpdf.printerQ");
       // PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
       
        for (PrintService ps : allServices)
        {
        	
        	if(ps.getName().matches(printerQ)){
        		System.out.println(ps + " supports :");
        		DocFlavor[] flavors = ps.getSupportedDocFlavors();
        		for (int i = 0; i < flavors.length; i++)
        		{
        			System.out.println("\t" + flavors[i].getMimeType());

        		}
        		
        		System.out.println(ps + " supports :");
        		
        		try {
        			FileInputStream fis = new FileInputStream("C:/pdf_test.pdf");
        			System.out.println("Total file size to read (in bytes) : "+ fis.available());
        			Doc pdfDoc = new SimpleDoc(fis, null, null);

        			DocPrintJob printJob = ps.createPrintJob();

        			printJob.print(pdfDoc, new HashPrintRequestAttributeSet());

        			fis.close();
        			
					
				} catch (Exception e) {
					System.out.println("Printing error : " + e.getMessage());
				}
        	}
        }
               
	}

Open in new window


and got this result:

Win32 Printer : Samsung M2020 Series#:13 supports :
      image/gif
      image/gif
      image/gif
      image/jpeg
      image/jpeg
      image/jpeg
      image/png
      image/png
      image/png
      application/x-java-jvm-local-objectref
      application/x-java-jvm-local-objectref
      application/octet-stream
      application/octet-stream
      application/octet-stream
Win32 Printer : Samsung M2020 Series#:13 supports :
Total file size to read (in bytes) : 26500
Printing error : null argument(s)

So, the samsung printer does not support pdf printing under windows?
ASKER CERTIFIED SOLUTION
Avatar of hdhondt
hdhondt
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dkim18

ASKER

hm...my development environment is windows. Any other options other than obtaining printer that supports PDF?
The 2 links I gave in my first post give other suggestions. I also mentioned using Acrobat (from Java) to do the printing.
It looks like you'd have to find a way in Java to convert from PDF to GDI if you want to use the Samsung printer
Avatar of dkim18

ASKER

one last question...
what if I obtain a printer that supports those languages(ps, pdf, etc...)? Can you list some inexpensive printers?
Why don't you just use the Brother?
Avatar of dkim18

ASKER

hdhondt said:

Brother HL 5140 supports PCL and PS, but not PDF
what if I obtain a printer that supports those languages(ps, pdf, etc...)?
is what you just asked
Avatar of dkim18

ASKER

If this issue is resolved by getting one of the printers that supports PDF, then I am thinking getting one. If that is the case, I want to get some list of printers.
Quite a difficult one to ascertain, i think, as it's not normally an issue or requirement
Many Xerox and hp printers do. I'll have a look for a low cost model later today (it's 7 am here now)
BTW why not use my suggestion of passing the job to Adobe acrobat (or other software). That will work with any printer
Avatar of dkim18

ASKER

BTW why not use my suggestion of passing the job to Adobe acrobat (or other software). That will work with any printer

>>In our web app, there is an option that check multiple documents and send print queue without opening adobe acrobat.
I cannot see why your script could not send multiple jobs to Acrobat but have a look at the Docuprint 3105. See pages 90-91 of the User  (https://www.google.com.au/url?sa=t&source=web&rct=j&url=https://www.fujixeroxprinters.com.au/downloads/uploaded/DP3105_User_Guide%2520(English)_5ef6.pdf&ved=0CBkQFjAAahUKEwiRuMmh5IbJAhXF36YKHRcCCok&usg=  )
Avatar of dkim18

ASKER


I cannot see why your script could not send multiple jobs to Acrobat....


The above script just doesn't work for my samsung/brother printer.

The Docuprint 3015 is for small-medium business and it is not really popular in US I think.
I was told 'HP LaserJet Pro P1102W' supports PDF.
The P1102W is a GDI printer, like your Samsung. It does not understand PDF or any printer language.

You'll find that most cheap printers are the same. It costs more to build a language into a printer than it does to let Windows do the work. So you'll always pay more for a PDF printer. High-end Laserjets support it, but they usually need some software to send the PDF. For that matter, even the DP 3105 requires at least LPR (part of Windows) to send the job. Can your script use LPR?
Avatar of dkim18

ASKER

My co-worker has the P1102W printer and it supports PDF according to him. I know his dev environment is Linux. Maybe that is why?

My script can use LPR, but it will be only for my dev environment since production box is Linux with tomcat.
Linux may do the conversion from PDF to printer bitmap, but the printer itself does not support it. If you look at the specs, you'll see the language is "host based". That means GDI or equivalent. Or, as I said before, the printer does NOT support ANY language, it lets Windows (or linux) do the job of converting the page into dots on the paper.

Linux also supports lpr (it originally comes from Berkeley Unix, before there was Windows).
You could probably also use Ghostscript which will convert between PDF and Postscript (on both your platforms)
@CEHJ I suggested that before (as well as Acrobat), but dkim18 says that can't be done with the script.
Sorry -  i don't remember that