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)

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) {					
				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();				
					try {
						job.print(pdfDoc, psas);
						printed = true;
					} catch (PrintException pe) {
						throw new PrintUtilException(pe.getMessage());

Open in new window

Thanks for any comments
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

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
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;
	System.out.println("count of bytes = "+count);

Open in new window

then you have access to the file of raw bytes.

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

Indeed, from :

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

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
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

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.
I believe you, but nonetheless, you should do the query
dkim18Author Commented:
Makes you wonder why they ever needed anything else apart from Autosense really.
(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 "
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

From novice to tech pro — start learning today.