Java - printing pdf in network printer

Hi Experts,

I am trying to print byte array of PDF in network printer and I am getting 'invalid flavor' or 'data is not of declared type' error depends on DocFlavor class.

Message is: invalid flavor

      at sun.print.Win32PrintJob.print(Unknown Source)
...
...

Or
Message is: data is not of declared type


This is the code
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 for any comments
dkim18Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You will probably find that for that to work, the printer would have to be able to handle raw pdf input. Commonly they are only designed to handle some form of Postscript
0
krakatoaCommented:
What you can do is to read the PDF byte-by-byte, save it to a text file, and then re-save the text file in, as CEHJ said, Postscript format, BUT make the file extension .pdf, in which case Adobe will read it back in.

The naive code to read the original .pdf is something like this :

File file = new File("C:/EE_Q_CODE/User Instructions.pdf");
	File result = new File("C:/EE_Q_CODE/User Instructions Raw Bytes.txt");
	
	FileInputStream fr = new FileInputStream(file);
	FileOutputStream fo = new FileOutputStream(result);
	int b;
	int count=0;
	while((b=fr.read())>-1){count++;fo.write(b);}
	
	System.out.println("count of bytes = "+count);
	fr.close();
	fo.close();

Open in new window


then you have access to the file of raw bytes.
0
CEHJCommented:
http://docs.oracle.com/javase/8/docs/api/javax/print/PrintService.html#getSupportedDocFlavors--

is something you should probably call. Let us know what you get

Indeed, from http://docs.oracle.com/javase/8/docs/api/javax/print/DocFlavor.html :

" Always query the print service to determine what doc flavors it supports."
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

dkim18Author Commented:
while I take a look those links, I want to let you know that the above code used to work with 'DocFlavor pdfFlavor = DocFlavor.BYTE_ARRAY.PDF' and our printers can handle raw PDF and PostScript input.
0
CEHJCommented:
I believe you, but nonetheless, you should do the query
0
dkim18Author Commented:
DocFlavor.BYTE_ARRAY.AUTOSENSE worked.
0
CEHJCommented:
:)
0
krakatoaCommented:
Makes you wonder why they ever needed anything else apart from Autosense really.
0
CEHJCommented:
(As mentioned here)

"You will probably find that for that to work, the printer would have to be able to handle raw pdf input. Commonly they are only designed to handle some form of Postscript "
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.